1. 程式人生 > >MySQL修改預設編碼

MySQL修改預設編碼

首先進入MySQL檢視當前預設編碼:

mysql> SHOW VARIABLES LIKE 'character%';
+--------------------------+-----------------------------------------------+
| Variable_name            | Value                                         |
+--------------------------+-----------------------------------------------+
| character_set_client     | latin1                                        |
| character_set_connection | latin1                                        |
| character_set_database   | latin1                                        |
| character_set_filesystem | binary                                        |
| character_set_results    | latin1                                        |
| character_set_server     | latin1                                        |
| character_set_system     | utf8                                          |
| character_sets_dir       | /rdsdbbin/oscar-5.6.10a.16.21/share/charsets/ |
+--------------------------+-----------------------------------------------+
8 rows in set (0.02 sec)

能夠看到大部分都是latin1,現在想把預設編碼改成uft8

可以直接執行set命令:

SET character_set_client = utf8;
SET character_set_connection = utf8;
SET character_set_database = utf8;
SET character_set_results = utf8;
SET character_set_server = utf8;

然後 mysql> SHOW VARIABLES LIKE 'character%'; 可以看到全變為utf8


但是,這只是一種假象。此種方式只在當前狀態下有效,當重啟資料庫服務後失效。

所以想要不出現亂碼只有修改MySQL配置檔案my.cnf(windows下my.ini)。
找到配置檔案(Linux下一般是/etc/my.cnf
開啟檔案,標籤下沒有的要新增,有的就修改:

[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
default-character-set=utf8

重新連線資料庫,再做查詢:

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 | /rdsdbbin/oscar-5.6.10a.16.21/share/charsets/ | +--------------------------+-----------------------------------------------+