MySQL5.x版本亂碼問題解決方案
MySQL是一款常用的開源數(shù)據(jù)庫(kù)軟件,但是對(duì)于初次使用者好像并不是太友好,MySQL5.x的版本中默認(rèn)字符集是latin1也就是我們所知道的ISO-8859-1字符集,這個(gè)字符集編碼并沒有包含漢字,所以我們?cè)谑褂玫臅r(shí)候漢字會(huì)出現(xiàn)亂碼情況,通過修改數(shù)據(jù)庫(kù)默認(rèn)字符集即可解決。
進(jìn)入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 FilesMySQLMySQL Server 5.7sharecharsets |+--------------------------+---------------------------------------------------------+8 rows in set, 1 warning (0.00 sec)
可以看到現(xiàn)在默認(rèn)的字符集是latin1
找到MySQL的配置文件,windows中的配置文件是my.ini,我的電腦位于C:ProgramDataMySQLMySQL Server 5.7,Linux中需要修改的是my.conf文件,具體路徑根據(jù)你的實(shí)際安裝位置,分別修改以下節(jié)點(diǎn)中的配置:
我使用的5.7中[client]節(jié)點(diǎn)的配置需要新增,其余兩個(gè)節(jié)點(diǎn)[mysql]、[mysql]需要打開注釋并修改為utf8.
[client]default-character-set=utf8[mysql]default-character-set=utf8[mysql]character_set_server=utf8
然后重啟MySQL
net stop mysqlnet start mysql
再次查看字符集,都已經(jīng)被修改為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 FilesMySQLMySQL Server 5.7sharecharsets |+--------------------------+---------------------------------------------------------+8 rows in set, 1 warning (0.00 sec)
為保證編碼一致性還可以在創(chuàng)建數(shù)據(jù)庫(kù)或者表的時(shí)候指定字符集,并在連接串后面指定連接參數(shù):
?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Oracle817 版本 不同字符集之間的數(shù)據(jù)庫(kù)導(dǎo)入2. MySQL全文搜索之布爾搜索3. sql索引失效的情況以及超詳細(xì)解決方法4. 個(gè)人經(jīng)驗(yàn)總結(jié):DB2數(shù)據(jù)庫(kù)邏輯卷的復(fù)制6. mysql 判斷是否為子集的方法步驟7. 詳解MySQL alter ignore 語(yǔ)法8. 精細(xì)分析Oracle分布式系統(tǒng)數(shù)據(jù)復(fù)制技術(shù)9. Oracle的PDB數(shù)據(jù)庫(kù)創(chuàng)建DIRECTORY時(shí)遇到ORA-65254問題及解決方法10. Microsoft Office Access設(shè)置字體顏色的方法
