閱讀推薦:
閉館修煉21日,《啃》283頁pdf,我終于在第4頁跳字節(jié)。
肺炎在家“閉館”,阿里發(fā)視頻面試,第四面順利贏了offer。
字符集是Oracle數(shù)據(jù)庫或MySQL數(shù)據(jù)庫中字符集選擇問題的符號和編碼規(guī)則集。如果在數(shù)據(jù)庫創(chuàng)建步驟中未正確選擇字符集,則以后可能需要替換字符集,替換字符集是一項代價高昂的任務(wù),存在風(fēng)險,因此建議您在應(yīng)用開始步驟中根據(jù)需要正確選擇相應(yīng)的字符集。
實戰(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 sec)
mysql> insert into values (1,'云中鶴');
Query OK, 1 row affected sec)
mysql> select * from ;
+------+-----------+
| id | names |
+------+-----------+
| 1 | 云中鶴 |
+------+-----------+
1 row in set sec)
mysql> set names latin1;
Query OK, 0 rows affected sec)
mysql> select * from ;
+------+-------+
| id | names |
+------+-------+
| 1 | ??? |
+------+-------+
1 row in set 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ù)庫的默認字符集
| character_set_filesystem | binary
| character_set_results | latin1 #查詢結(jié)果字符集 |
| character_set_server | utf8 #默認的內(nèi)部操作字符集 |
| character_set_system | utf8 #系統(tǒng)元數(shù)據(jù)(字段名等)字符集
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set 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 sec)
? 2、登錄mysql的時候指定默認字符集
[root@node1 ~]# mysql -S /tm --defaults-character-set=utf8 #-S 指定多實例mysql的套接字文件,
使用--defaults-character-set 指定默認字符集。
? 3、 修改/etc/sysconfig/i18n 文件,在my.cnf不指定默認字符集的情況下。
vim /etc/sysconfig/i18n
LANG='z; #如果my.cnf不指定,默認使用系統(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 sec)
字符集選擇
? 1、如果處理各種各樣的文字,發(fā)布到不同語言國家地區(qū),選擇Unicode。,對mysql對號utf-8。
? 2、只需中文,數(shù)據(jù)量很大,性能要求也高,選擇gbk.。
? 3、處理移動物聯(lián)網(wǎng)業(yè)務(wù),選utf8mb4
? 建議在能夠完全滿足應(yīng)用的前提下,盡量使用小的字符集。因為更小的字符集意味著能夠節(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ù)無效?;静皇褂?/code>- 常用轉(zhuǎn)換數(shù)據(jù)庫字符集方案
mysqldump -S /tm --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 /tm --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 sec)
mysql -S /tm test < /data/test-`date +%F`.sql #6、導(dǎo)入表結(jié)構(gòu)
mysql -S /tm test < /data #7、導(dǎo)入表數(shù)據(jù)
最后
這是在15年的時候,自己學(xué)習(xí)MySQL的筆記,今天再拿出來分享下
作者:Real_man
鏈接:
來源:掘金
1.《mysql數(shù)據(jù)庫有亂碼怎么解決方法?終于找到答案了安利給你,關(guān)于MySQL字符集亂碼與解決方案》援引自互聯(lián)網(wǎng),旨在傳遞更多網(wǎng)絡(luò)信息知識,僅代表作者本人觀點,與本網(wǎng)站無關(guān),侵刪請聯(lián)系頁腳下方聯(lián)系方式。
2.《mysql數(shù)據(jù)庫有亂碼怎么解決方法?終于找到答案了安利給你,關(guān)于MySQL字符集亂碼與解決方案》僅供讀者參考,本網(wǎng)站未對該內(nèi)容進行證實,對其原創(chuàng)性、真實性、完整性、及時性不作任何保證。
3.文章轉(zhuǎn)載時請保留本站內(nèi)容來源地址,http://f99ss.com/gl/3047115.html