- 工信部備案號 滇ICP備05000110號-1
- 滇公安備案 滇53010302000111
- 增值電信業(yè)務(wù)經(jīng)營許可證 B1.B2-20181647、滇B1.B2-20190004
- 云南互聯(lián)網(wǎng)協(xié)會理事單位
- 安全聯(lián)盟認(rèn)證網(wǎng)站身份V標(biāo)記
- 域名注冊服務(wù)機(jī)構(gòu)許可:滇D3-20230001
- 代理域名注冊服務(wù)機(jī)構(gòu):新網(wǎng)數(shù)碼
show variables like 'character%';
# 或者
show variables like '%char%';
MySQL5.7中執(zhí)行如下:
MySQL 5.7 默認(rèn)的客戶端和服務(wù)器都用了 latin1 ,不支持中文,保存中文會報錯。示例如下:
ysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb |
+--------------------+
5 rows in set (0.00 sec)
mysql> create database dbdemo1;
Query OK, 1 row affected (0.00 sec)
mysql> use dbdemo1;
Database changed
mysql> create table t_emp(id int,name varchar(15));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t_emp(id,name) values(1001,'ZhangSan');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t_emp(id,name) values(1002,'李四');
ERROR 1366 (HY000): Incorrect string value: '\\\\\\\\\\\\\\\\xE6\\\\\\\\\\\\\\\\x9D\\\\\\\\\\\\\\\\x8E\\\\\\\\\\\\\\\\xE5\\\\\\\\\\\\\\\\x9B\\\\\\\\\\\\\\\\x9B' for column 'name' at row 1
因為默認(rèn)情況下,創(chuàng)建表使用的是 latin1
mysql> show create table t_emp;
+-------+----------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------+
| t_emp | CREATE TABLE `t_emp` (
`id` int(11) DEFAULT NULL,
`name` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------+
1 row in set (0.00 sec)
1、編輯配置文件,在mysqld
模塊下添加如下內(nèi)容
# vim /etc/my.cnf
[mysqld]
...
character_set_server=utf8
collation-server=utf8_general_ci
2、重啟數(shù)據(jù)庫
# systemctl restart mysqld
注意:已存在的原庫、原表的設(shè)定不會發(fā)生變化,參數(shù)修改只是對新建的數(shù)據(jù)庫生效。
3、修改已有庫&表字符集的變更
-- 修改已創(chuàng)建數(shù)據(jù)庫的字符集
alter database dbtest1 character set 'utf8';
-- 修改已創(chuàng)建數(shù)據(jù)表的字符集
alter table t_emp convert to character set 'utf8';
范例:
# 修改已經(jīng)創(chuàng)建數(shù)據(jù)庫的字符集
-- 修改之前查看字符集
mysql> show create database dbdemo1;
+----------+--------------------------------------------------------------------+
| Database | Create Database |
+----------+--------------------------------------------------------------------+
| dbdemo1 | CREATE DATABASE `dbdemo1` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+--------------------------------------------------------------------+
1 row in set (0.01 sec)
-- 進(jìn)行修改
mysql> alter database dbdemo1 character set 'utf8';
Query OK, 1 row affected (0.00 sec)
-- 修改之后查看字符集
mysql> show create database dbdemo1;
+----------+------------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------------+
| dbdemo1 | CREATE DATABASE `dbdemo1` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+------------------------------------------------------------------+
1 row in set (0.00 sec)
# 修改已經(jīng)創(chuàng)建數(shù)據(jù)表的字符集
-- 修改之前查看字符集
mysql> show create table t_emp;
+-------+------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------+
| t_emp | CREATE TABLE `t_emp` (
`id` int(11) DEFAULT NULL,
`name` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
-- 進(jìn)行修改
mysql> alter table t_emp convert to character set 'utf8';
Query OK, 1 row affected (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 0
-- 修改之后查看字符集
mysql> show create table t_emp;
+-------+----------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------+
| t_emp | CREATE TABLE `t_emp` (
`id` int(11) DEFAULT NULL,
`name` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
注意:原有的數(shù)據(jù)如果是用非'utf8'編碼的話,數(shù)據(jù)本身編碼不會發(fā)生改變。已有數(shù)據(jù)需要導(dǎo)出或刪除,然后重新插入。
藍(lán)隊云官網(wǎng)上擁有完善的技術(shù)支持庫可供參考,大家可自行查閱,更多技術(shù)問題,可以直接咨詢。同時,藍(lán)隊云整理了運(yùn)維必備的工具包免費(fèi)分享給大家使用,需要的朋友可以直接咨詢。
更多技術(shù)知識,藍(lán)隊云期待與你一起探索。
提交成功!非常感謝您的反饋,我們會繼續(xù)努力做到更好!
這條文檔是否有幫助解決問題?
售前咨詢
售后咨詢
備案咨詢
二維碼
TOP