转自: http://blog.itpub.net/27099995/viewspace-1775442/
MySQL: 5.6.25
OS: CentOS6.5
刚刚在做查询优化的时候,自己创建表,但是没有注意到字符集的问题,当我向默认情况下创建的表中插入数据的时候,死活都插不进去,中文的不能显示出来。
过程如下:
创建表:
create table t1 (
id int(10) PRIMARY KEY,
name varchar(20),
class varchar(20));
插入数据:
insert into t1 values(1,'zhangyi','一班');
insert into t1 values(2,'zhanger','二班');
结果显示:
- mysql> insert into t1 values(1,'zhangyi','');
Query OK, 1 row affected (0.01 sec)
mysql> insert into t1 values(2,'zhanger','');
Query OK, 1 row affected (0.01 sec)
- mysql> select * from t1;
- +----+---------+--------+
- | id | name | class |
- +----+---------+--------+
- | 1 | zhangyi | |
- | 2 | zhanger | |
- +----+---------+--------+
- 2 rows in set (0.00 sec)
去图形界面用navicat插入,也是不成功。然后就去查看字符集,
- mysql> show variables like '%character%';
- +--------------------------+----------------------------+
- | Variable_name | Value |
- +--------------------------+----------------------------+
- | character_set_client | utf8 |
- | character_set_connection | utf8 |
- | character_set_database | latin1 |
- | character_set_filesystem | binary |
- | character_set_results | utf8 |
- | character_set_server | latin1 |
- | character_set_system | utf8 |
- | character_sets_dir | /usr/share/mysql/charsets/ |
- +--------------------------+----------------------------+
原因:
我的MySQL5.6.25是通过rpm包安装的,应该是MySQL默认的字符集是 latin1。如果是编译安装可以再cmake的时候指定字符集。
解决办法:
我先把表的字符集修改为utf8,图形界面和命令行都一样
ALTER TABLE t1 DEFAULT CHARACTER SET=utf8;
再查看的时候就可以显示了。
为了以后的表都可以正常显示中文,我直接把MySQL数据库实例的字符集修改为utf8
5.6之前的版本是
default-character-set=utf8
5.6版本修改字符集的参数是
character-set-server=utf8
直接vi /etc/my.cnf 文件添加到[mysqld] 下面,重启MySQL即可
- [mysqld]
- socket = /mysqldata/mysql/mysql.sock
- character-set-server=utf8
-
- [mysqld]
- character-set-server=utf8
- mysql> show variables like '%character%';
- +--------------------------+----------------------------+
- | Variable_name | Value |
- +--------------------------+----------------------------+
- | character_set_client | utf8 |
- | character_set_connection | utf8 |
- | character_set_database | utf8 |
- | character_set_filesystem | binary |
- | character_set_results | utf8 |
- | character_set_server | utf8 |
- | character_set_system | utf8 |
- | character_sets_dir | /usr/share/mysql/charsets/ |
- +--------------------------+----------------------------+
- mysql> select * from t1;
- +----+---------+--------+
- | id | name | class |
- +----+---------+--------+
- | 1 | zhangyi | 一班 |
- | 2 | zhanger | 二班 |
- | 4 | zhangsi | 四班 |
- +----+---------+--------+
- 3 rows in set (0.00 sec)
- [mysqld]
- socket = /mysqldata/mysql/mysql.sock
- character-set-server=utf8