1.mysql配置文件地址
/etc/mysql/my.cnf
2.在[mysqld]在下方添加以下代碼
[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
3.重啟mysql服務
sudo service mysql restart
4.檢測字符集是否更新成utf8.
進入mysql,mysql -u root -p,輸入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/ |
+--------------------------+----------------------------+
注意事項:在修改字符集之前已經建立的數據庫,character_set_database值不會發生改變,往數據庫中插入中文數據仍然會顯示亂碼,所以最好在安裝完MySQL後就將字符集改成utf8,否則後續修改會較麻煩。