1.操作系统:CentOS6.6
2.数据库系统: Mysql
3.任务:解决数据乱码的问题
1)更改客户端的字符集gbk
客户端字符集设置,set names gbk;这样可以确保插入后的中文,不出现乱码,对执行set names gbk,前插入的中文无效。
说明:通常下面的几个字符集和数据库的字符集相同的和character_set_database,才能确保写入数据可以正确输出。
mysql> set names gbk; #更改客户端的字符集gbk
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like ‘character_set%‘;
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | gbk #客户端字符集 |
| character_set_connection | gbk #连接字符集 |
| character_set_database |gbk #数据库字符集 |
| character_set_filesystem | binary |
| character_set_results | gbk #返回结果字符集 |
| character_set_server | gbk #服务器字符集 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
2)更改服务端字符集的配置:
在my.cnf配置文件里[mysqld]模块下添加字任集配置,生效后,创建数据库和表默认都是这个设置的字符集。
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
default-character-set=gbk #添加一行,是更改服务端的字符集
[[email protected] /]# service mysqld restart
停止 mysqld: [确定]
正在启动 mysqld:
mysql> show variables like ‘character_set%‘;
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | gbk | #更改成了gbk字符集
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | gbk | #更改成了gbk字符集
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
3)或者登录的时候指定字符集:
[[email protected] mysql]# Mysql –uroot –p –default-character-set=gbk
[[email protected] mysql]# mysql -uroot -p -S /var/lib/mysql/mysql.sock --default-character-set=gbk;