Linux下MySQL 5.6的修改字符集編碼爲UTF8(完全解決中文亂碼問題)

1、登陸MySQL查看用SHOW VARIABLES LIKE ‘character%’;下字符集,顯示以下:

+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

character_set_database和character_set_server的默認字符集仍是latin1。mysql

2、最簡單的完美修改方法,修改mysql的my.cnf文件中的字符集鍵值(注意配置的字段細節):

一、在[client]字段里加入default-character-set=utf8,以下:

[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
default-character-set=utf8

二、在[mysqld]字段里加入character-set-server=utf8,以下:

[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
character-set-server=utf8

三、在[mysql]字段里加入default-character-set=utf8,以下:

[mysql]
no-auto-rehash
default-character-set=utf8

修改完成後,service mysql restart重啓mysql服務就生效。注意:[mysqld]字段與[mysql]字段是有區別的。這點在網上沒人反饋過。web

使用SHOW VARIABLES LIKE ‘character%’;查看,發現數據庫編碼全已改爲utf8。

+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
sql

相關文章
相關標籤/搜索