今天开发反应两样的程序往一个库里面插入数据正常,往另外一个库里面插入数据有乱码。第一反应就是两个数据库关于字符集的配置不一样。
在两个库分别查看参数:
show variables like "%char%";
+--------------------------+------------------------------------------+
| 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 | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+--------------------------+------------------------------------------+
>show variables like "%char%";
+--------------------------+------------------------------------------+
| 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/local/mysql/share/charsets/ |
+--------------------------+------------------------------------------+
可以看到character_set_server的参数配置不一样,于是将有问题的那个库配置成utf8,开发再次测试的时候表示还有乱码问题,这时想起来校验字符集没有改,于是分别查看两边校验字符集的差异:
>show variables like "%coll%";
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
>show variables like "%coll%";
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
可以看到collation_server 参数设置不一致,于是把这个参数也改成了utf8,再次测试的时候数据显示正常。
mysql支持多个层次的字符集设置:
服务层(server)、数据库层(database)、数据表(table)、字段(column)、连接(connection)、结果集(result)
优先级:server > database > table > column
为了避免出现因字符不一致导致的中文乱码的问题,最好就是将字符集全部设置成一样的。
另外提一下skip-character-set-client-handshake这个参数,可以通过开启这个参数来过滤客户端设置的字符集
本文转自 emma_cql 51CTO博客,原文链接:http://blog.51cto.com/chenql/1975678