MySQL字符集亂碼與解決方案

字符集是一套符號和編碼的規則,不管是在oracle數據庫仍是在mysql數據庫,都存在字符集的選擇問題,並且若是在數據庫建立階段沒有正確選擇字符集,那麼可能在後期須要更換字符集,而字符集的更換是代價比較高的操做,也存在必定的風險,因此,咱們推薦在應用開始階段,就按照需求正確的選擇合適的字符集,避免後期沒必要要的調整。node

實戰

一、安裝MySQL數據庫mysql

二、亂碼演示sql

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    #客戶端來源數據使用的字符集 
| character_set_connection | latin1    # 鏈接層字符集
| character_set_database   | utf8       #當前選中數據庫的默認字符集 
| character_set_filesystem | binary                  
| character_set_results    | latin1    #查詢結果字符集 |
| character_set_server     | utf8       #默認的內部操做字符集 |
| character_set_system     | utf8      #系統元數據(字段名等)字符集 
| character_sets_dir       | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)
複製代碼

亂碼解決辦法

從上面能夠看出,set names latin1;改變了三個參數.. 只要作到客戶端,MySQL character-set-client,table charset三個字符集徹底一致就能夠保證必定不會有亂碼出現。數據庫

方式:vim

​ 一、在mysql命令行模式下執行set names xxx;bash

mysql> set names utf8;
       Query OK, 0 rows affected (0.01 sec)
複製代碼

​ 二、登陸mysql的時候指定默認字符集服務器

[root@node1 ~]# mysql -S /tmp/mysql.sock4 --defaults-character-set=utf8 #-S 指定多實例mysql的套接字文件,
       使用--defaults-character-set 指定默認字符集。
複製代碼

​ 三、 修改/etc/sysconfig/i18n 文件,在my.cnf不指定默認字符集的狀況下。網絡

vim /etc/sysconfig/i18n
       LANG='zh_CN.UTF-8'   #若是my.cnf不指定,默認使用系統字符集
複製代碼

​ 四、 修改my.cnf 文件,下面兩個字段在任意一個字段都是能夠的。oracle

[client]
       default-character-set=latin1
複製代碼
[mysql]
       default-character-set=latin1
複製代碼

​ 前兩種1,2是臨時解決方案,後面兩種3,4是永久有效的方案性能

查看字符集

一、查看系統當前字符集設置,修改以後登錄mysql執行。show variables lile 'character_set%';

mysql> show variables like 'character_set%';#我修改的爲utf8,因此客戶端的三個參數都是utf8.你能夠自行設置字符集。
+--------------------------+----------------------------------+
| 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/ |
複製代碼

二、mysql經常使用字符集。執行命令show character set; 查看系統支持字符集。

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)
複製代碼

字符集選擇

​ 一、若是處理各類各樣的文字,發佈到不一樣語言國家地區,選擇Unicode。,對mysql對號utf-8。

​ 二、只需中文,數據量很大,性能要求也高,選擇gbk.。

​ 三、處理移動物聯網業務,選utf8mb4

​ 建議在可以徹底知足應用的前提下,儘可能使用小的字符集。由於更小的字符集意味着可以節省空間、減小網絡傳輸字節數,同時因爲存儲空間的較小間接的提升了系統的性能。

服務器字符集設置

[mysqld]
...
character-set-server=utf8   #添加這條語句,可設置服務器端字符集。
複製代碼

mysql服務端從新啓動後,這兩個參數會改變爲設定值。

| character_set_server     | utf8    
| character_set_database   | utf8
複製代碼

切換字符集

  1. 將一種編碼的數據庫轉換爲另外一種編碼的數據。
alter database dbname character set xxx; #只能對之後的數據有效,對以前的數據無效。基本不使用
複製代碼
  1. 經常使用轉換數據庫字符集方案
mysqldump -S /tmp/mysql.sock4 --default-character-set=utf8 -d test > /data/test-`date +%F`.sql #一、導出表結構而不導出數據

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;  #二、這裏的CHARSET改成你想要修改的字符集

mysqldump -S /tmp/mysql.sock4 --extended-insert --no-create-info test > /data/test-data-`date +%F`.sql  #三、導出數據內容

vim /data/test-data-`date +%F`.sql
LOCK TABLES `table10` WRITE;
set names utf8;  #四、這一行是多加的,本身指定想要轉換的字符集
INSERT INTO `table10` VALUES (1,'雲中鶴');
UNLOCK TABLES;

mysql> create database if not exists test;  #五、建立數據庫,存儲轉換後的數據
Query OK, 1 row affected, 0 warning (0.00 sec)

mysql -S /tmp/mysql.sock4 test < /data/test-`date +%F`.sql  #六、導入表結構

mysql -S /tmp/mysql.sock4 test < /data/test-data-2015-09-25.sql   #七、導入表數據
複製代碼

最後

這是在15年的時候,本身學習MySQL的筆記,今天再拿出來分享下

相關文章
相關標籤/搜索