mysql默认字符集问题
最近在使用mysql的时候出现了奇怪的乱码问题,最开始发现mysql的字符集的确存在一些问题。
经过修改配置文件/etc/my.cnf
[mysqld]
character-set-server=utf8
> 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中utf8字符集,我们国内默认选择:utf8_general_ci而不是utf8_unicode_ci,但是终端的utf8是utf8_unicode_ci,
所以这就造成了冲突,于是又修改配置文件。
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
修改后运行程序还是没有解决乱码的问题。
经过观察后又发现,虽然设置了配置文件,但是每次启动mysql后字符集还是会发生奇怪的变化。
于是再次修改配置文件。
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-character-set-client-handshake 跳过字符设置客户端握手。
文档解释如下
--character-set-client-handshake
Don't ignore character set information sent by the client.
To ignore client information and use the default server character set,
use --skip-character-set-client-handshake; this makes MySQL behave like MySQL 4.0