最近在使用mysql的時候出現了奇怪的亂碼問題,最開始發現mysql的字符集的確存在一些問題。mysql
通過修改配置文件/etc/my.cnfsql
[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/ |
+--------------------------+----------------------------+this
可是發現並無解決亂碼的問題,又查找了相關的資料發現了,其實mysql中utf8字符集,咱們國內默認選擇:utf8_general_ci而不是utf8_unicode_ci,可是終端的utf8是utf8_unicode_ci,
因此這就形成了衝突,因而又修改配置文件。code
[mysqld] init_connect='SET collation_connection = utf8_unicode_ci' init_connect='SET NAMES utf8' character-set-server=utf8 collation-server=utf8_unicode_ci
修改後運行程序仍是沒有解決亂碼的問題。
通過觀察後又發現,雖然設置了配置文件,可是每次啓動mysql後字符集仍是會發生奇怪的變化。
因而再次修改配置文件。orm
[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 跳過字符設置客戶端握手。
文檔解釋以下server
--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