小編給大家分享一下MySQL字符集亂碼怎么辦,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)公司長期為上1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為社旗企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè),社旗網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
前言
字符集是一套符號和編碼的規(guī)則,不論是在oracle數(shù)據(jù)庫還是在mysql數(shù)據(jù)庫,都存在字符集的選擇問題,而且如果在數(shù)據(jù)庫創(chuàng)建階段沒有正確選擇字符集,那么可能在后期需要更換字符集,而字符集的更換是代價比較高的操作,也存在一定的風(fēng)險,所以,我們推薦在應(yīng)用開始階段,就按照需求正確的選擇合適的字符集,避免后期不必要的調(diào)整。
實(shí)戰(zhàn)
1、安裝MySQL數(shù)據(jù)庫
2、亂碼演示
mysql> show variables like 'character_set%'; +--------------------------+----------------------------------+ | 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/local/mysql/share/charsets/ | +--------------------------+----------------------------------+ 8 rows in set (0.00 sec) mysql> insert into test.table10 values (1,'云中鶴'); Query OK, 1 row affected (0.00 sec) mysql> select * from test.table10; +------+-----------+ | id | names | +------+-----------+ | 1 | 云中鶴 | +------+-----------+ 1 row in set (0.00 sec) mysql> set names latin1; Query OK, 0 rows affected (0.00 sec) mysql> select * from test.table10; +------+-------+ | id | names | +------+-------+ | 1 | ??? | +------+-------+ 1 row in set (0.00 sec) mysql> show variables like 'character_set%'; +--------------------------+----------------------------------+ | Variable_name | Value | +--------------------------+----------------------------------+ | character_set_client | latin1 #客戶端來源數(shù)據(jù)使用的字符集 | character_set_connection | latin1 # 連接層字符集 | character_set_database | utf8 #當(dāng)前選中數(shù)據(jù)庫的默認(rèn)字符集 | character_set_filesystem | binary | character_set_results | latin1 #查詢結(jié)果字符集 | | character_set_server | utf8 #默認(rèn)的內(nèi)部操作字符集 | | character_set_system | utf8 #系統(tǒng)元數(shù)據(jù)(字段名等)字符集 | character_sets_dir | /usr/local/mysql/share/charsets/ | +--------------------------+----------------------------------+ 8 rows in set (0.00 sec)
亂碼解決辦法
從上面可以看出,set names latin1;改變了三個參數(shù).. 只要做到客戶端,MySQL character-set-client,table charset三個字符集完全一致就可以保證一定不會有亂碼出現(xiàn)。
方式:
1、在mysql命令行模式下執(zhí)行set names xxx;
mysql> set names utf8; Query OK, 0 rows affected (0.01 sec)
2、登錄mysql的時候指定默認(rèn)字符集
[root@node1 ~]# mysql -S /tmp/mysql.sock4 --defaults-character-set=utf8 #-S 指定多實(shí)例mysql的套接字文件, 使用--defaults-character-set 指定默認(rèn)字符集。
3、 修改/etc/sysconfig/i18n 文件,在my.cnf不指定默認(rèn)字符集的情況下。
vim /etc/sysconfig/i18n LANG='zh_CN.UTF-8' #如果my.cnf不指定,默認(rèn)使用系統(tǒng)字符集
4、 修改my.cnf 文件,下面兩個字段在任意一個字段都是可以的。
[client] default-character-set=latin1
[mysql] default-character-set=latin1
前兩種1,2是臨時解決方案,后面兩種3,4是永久有效的方案
查看字符集
1、查看系統(tǒng)當(dāng)前字符集設(shè)置,修改之后登陸mysql執(zhí)行。show variables lile 'character_set%';
mysql> show variables like 'character_set%';#我修改的為utf8,所以客戶端的三個參數(shù)都是utf8.你可以自行設(shè)置字符集。 +--------------------------+----------------------------------+ | 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/local/mysql/share/charsets/ |
2、mysql常用字符集。執(zhí)行命令show character set; 查看系統(tǒng)支持字符集。
mysql> show character set; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 | | dec8 | DEC West European | dec8_swedish_ci | 1 | | cp850 | DOS West European | cp850_general_ci | 1 | | hp8 | HP West European | hp8_english_ci | 1 | | koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 | | latin1 | cp1252 West European | latin1_swedish_ci | 1 | #常用 | latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 | | swe7 | 7bit Swedish | swe7_swedish_ci | 1 | | ascii | US ASCII | ascii_general_ci | 1 | | ujis | EUC-JP Japanese | ujis_japanese_ci | 3 | | sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 | | hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 | | tis620 | TIS620 Thai | tis620_thai_ci | 1 | | euckr | EUC-KR Korean | euckr_korean_ci | 2 | | koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 | | gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 | | greek | ISO 8859-7 Greek | greek_general_ci | 1 | | cp1250 | Windows Central European | cp1250_general_ci | 1 | | gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 | #常用 | latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 | | armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 |#常用 | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | cp866 | DOS Russian | cp866_general_ci | 1 | | keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 | | macce | Mac Central European | macce_general_ci | 1 | | macroman | Mac West European | macroman_general_ci | 1 | | cp852 | DOS Central European | cp852_general_ci | 1 | | latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 | | utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 | #常用 | cp1251 | Windows Cyrillic | cp1251_general_ci | 1 | | utf16 | UTF-16 Unicode | utf16_general_ci | 4 | | cp1256 | Windows Arabic | cp1256_general_ci | 1 | | cp1257 | Windows Baltic | cp1257_general_ci | 1 | | utf32 | UTF-32 Unicode | utf32_general_ci | 4 | | binary | Binary pseudo charset | binary | 1 | | geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 | | cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 | | eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 | +----------+-----------------------------+---------------------+--------+ 39 rows in set (0.00 sec)
字符集選擇
1、如果處理各種各樣的文字,發(fā)布到不同語言國家地區(qū),選擇Unicode。,對mysql對號utf-8。
2、只需中文,數(shù)據(jù)量很大,性能要求也高,選擇gbk.。
3、處理移動物聯(lián)網(wǎng)業(yè)務(wù),選utf8mb4
建議在能夠完全滿足應(yīng)用的前提下,盡量使用小的字符集。因?yàn)楦〉淖址馕吨軌蚬?jié)省空間、減少網(wǎng)絡(luò)傳輸字節(jié)數(shù),同時由于存儲空間的較小間接的提高了系統(tǒng)的性能。
服務(wù)器字符集設(shè)置
[mysqld] ... character-set-server=utf8 #添加這條語句,可設(shè)置服務(wù)器端字符集。
mysql服務(wù)端重新啟動后,這兩個參數(shù)會改變?yōu)樵O(shè)定值。
| character_set_server | utf8 | character_set_database | utf8
切換字符集
將一種編碼的數(shù)據(jù)庫轉(zhuǎn)換為另一種編碼的數(shù)據(jù)。
alter database dbname character set xxx; #只能對以后的數(shù)據(jù)有效,對之前的數(shù)據(jù)無效?;静皇褂?/pre>常用轉(zhuǎn)換數(shù)據(jù)庫字符集方案
mysqldump -S /tmp/mysql.sock4 --default-character-set=utf8 -d test > /data/test-`date +%F`.sql #1、導(dǎo)出表結(jié)構(gòu)而不導(dǎo)出數(shù)據(jù) vim /data/test-`date +%F`.sql DROP TABLE IF EXISTS `table10`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `table10` ( `id` int(11) DEFAULT NULL, `names` char(20) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; #2、這里的CHARSET改為你想要修改的字符集 mysqldump -S /tmp/mysql.sock4 --extended-insert --no-create-info test > /data/test-data-`date +%F`.sql #3、導(dǎo)出數(shù)據(jù)內(nèi)容 vim /data/test-data-`date +%F`.sql LOCK TABLES `table10` WRITE; set names utf8; #4、這一行是多加的,自己指定想要轉(zhuǎn)換的字符集 INSERT INTO `table10` VALUES (1,'云中鶴'); UNLOCK TABLES; mysql> create database if not exists test; #5、創(chuàng)建數(shù)據(jù)庫,存儲轉(zhuǎn)換后的數(shù)據(jù) Query OK, 1 row affected, 0 warning (0.00 sec) mysql -S /tmp/mysql.sock4 test < /data/test-`date +%F`.sql #6、導(dǎo)入表結(jié)構(gòu) mysql -S /tmp/mysql.sock4 test < /data/test-data-2015-09-25.sql #7、導(dǎo)入表數(shù)據(jù)以上是“MySQL字符集亂碼怎么辦”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站標(biāo)題:MySQL字符集亂碼怎么辦
網(wǎng)站網(wǎng)址:http://weahome.cn/article/pigisg.html