MySQL5.x版本亂碼問題解決方案
阿新 • • 發佈:2020-09-24
MySQL是一款常用的開源資料庫軟體,但是對於初次使用者好像並不是太友好,MySQL5.x的版本中預設字符集是latin1也就是我們所知道的ISO-8859-1字符集,這個字符集編碼並沒有包含漢字,所以我們在使用的時候漢字會出現亂碼情況,通過修改資料庫預設字符集即可解決。
進入MySQL命令列:
mysql> show variables like '%colla%'; +----------------------+-------------------+ | Variable_name | Value | +----------------------+-------------------+ | collation_connection | gbk_chinese_ci | | collation_database | latin1_swedish_ci | | collation_server | latin1_swedish_ci | +----------------------+-------------------+ 3 rows in set,1 warning (0.00 sec) mysql> show variables like '%char%'; +--------------------------+---------------------------------------------------------+ | Variable_name | Value | +--------------------------+---------------------------------------------------------+ | character_set_client | gbk | | character_set_connection | gbk | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | gbk | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | C:\Program Files\MySQL\MySQL Server 5.7\share\charsets\ | +--------------------------+---------------------------------------------------------+ 8 rows in set,1 warning (0.00 sec)
可以看到現在預設的字符集是latin1
找到MySQL的配置檔案,windows中的配置檔案是my.ini,我的電腦位於C:\ProgramData\MySQL\MySQL Server 5.7,Linux中需要修改的是my.conf檔案,具體路徑根據你的實際安裝位置,分別修改以下節點中的配置:
我使用的5.7中[client]節點的配置需要新增,其餘兩個節點[mysql]、[mysql]需要開啟註釋並修改為utf8.
[client] default-character-set=utf8 [mysql] default-character-set=utf8 [mysql] character_set_server=utf8
然後重啟MySQL
net stop mysql
net start mysql
再次檢視字符集,都已經被修改為utf8字符集
mysql> show variables like '%colla%'; +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | gbk_chinese_ci | | collation_database | utf8_general_ci | | collation_server | utf8_general_ci | +----------------------+-----------------+ 3 rows in set,1 warning (0.00 sec) mysql> show variables like '%char%'; +--------------------------+---------------------------------------------------------+ | Variable_name | Value | +--------------------------+---------------------------------------------------------+ | character_set_client | gbk | | character_set_connection | gbk | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | gbk | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | C:\Program Files\MySQL\MySQL Server 5.7\share\charsets\ | +--------------------------+---------------------------------------------------------+ 8 rows in set,1 warning (0.00 sec)
為保證編碼一致性還可以在建立資料庫或者表的時候指定字符集,並在連線串後面指定連線引數:
?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。