【Mysql】關於mysql存入emoji表情的問題

小亮520cl發表於2016-08-22
1.測試插入指令碼
  1. <?php
  2. $conn = mysql_connect("127.0.0.1","root","ESBecs00!@#") or die ("wrong!");
  3. $sel=mysql_select_db("test",$conn);
  4. $contents = <<<EOT
  5. "\ud83d\udca5"
  6. EOT;

  7. $contents = json_decode($contents). "hello";
  8. echo $contents, "\t\n";

  9. $sql="insert into testtab (`contents`) VALUES('$contents')";


  10. for($i = 0;$i<1;$i++)
  11. {
  12.         $que=mysql_query($sql,$conn);
  13.         while ($row = mysql_fetch_assoc($que)) {
  14.                 var_dump($row);
  15.         }

  16. }


  17. mysql_close($conn);


  18. ?>


在utf8的背景下
  1. 資料庫端字符集為
    1. mysql> SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
    2. +--------------------------+-----------------+
    3. | Variable_name | Value |
    4. +--------------------------+-----------------+
    5. | character_set_client | utf8 |
    6. | character_set_connection | utf8 |
    7. | character_set_database | utf8 |
    8. | character_set_filesystem | binary |
    9. | character_set_results | utf8 |
    10. | character_set_server | utf8 |
    11. | character_set_system | utf8 |
    12. | collation_connection | utf8_general_ci |
    13. | collation_database | utf8_unicode_ci |
    14. | collation_server | utf8_unicode_ci |
    15. +--------------------------+-----------------+

  2. 表字符集編碼為
    1. mysql> show create table testtab\G;
    2. *************************** 1. row ***************************
    3.        Table: testtab
    4. Create Table: CREATE TABLE `testtab` (
    5.   `contents` text
    6. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    7. 1 row in set (0.00 sec)


    執行指令碼插入資料
    1. [root@host-192-168-1-56 ~]# php test.php
    2. hello

  3. 檢查資料
    1. MySQL [test]> select * from testtab;                                                                             
      +----------+
      | contents |
      +----------+
      |          |
      |          |
      +----------+
    2. 空字串 有時會報這種錯: Incorrect string value: '\xF0\x9F\x92\x94' for column 'name' at row 1 



若線上修改字符集
  1. mysql> ALTER TABLE testtab CONVERT TO CHARACTER SET utf8mb4;
  2. Query OK, 0 rows affected (0.34 sec)
  3. Records: 0 Duplicates: 0 Warnings: 0

  4. mysql> alter table testtab modify column `contents` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
  5. Query OK, 0 rows affected (0.36 sec)
  6. Records: 0 Duplicates: 0 Warnings: 0

  mysql>  show create table testtab\G;
*************************** 1. row ***************************
       Table: testtab
Create Table: CREATE TABLE `testtab` (
  `contents` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)

 執行插入指令碼
 檢查資料
MySQL [test]> select * from testtab;
+-----------+
| contents  |
+-----------+
|           |
|           |
| ????hello | --插進去,因為沒有前端測試,不知道是亂碼還是正常的
+-----------+
3 rows in set (0.00 sec)


修改配置檔案重啟資料庫
  1. [client]
  2. port=3306
  3. socket=/var/lib/mysql/mysql.sock
  4. default-character-set=utf8mb4
  5. [mysqld]
  6. character_set_server = utf8mb4
  7. collation-server = utf8mb4_unicode_ci

  1. MySQL [test]> SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
    +--------------------------+--------------------+
    | Variable_name            | Value              |
    +--------------------------+--------------------+
    | character_set_client     | utf8               |
    | character_set_connection | utf8               |
    | character_set_database   | utf8mb4            |
    | character_set_filesystem | binary             |
    | character_set_results    | utf8               |
    | character_set_server     | utf8mb4            |
    | character_set_system     | utf8               |
    | collation_connection     | utf8_general_ci    |
    | collation_database       | utf8mb4_unicode_ci |
    | collation_server         | utf8mb4_unicode_ci |
    +--------------------------+--------------------+


再轉換表
  1. mysql> ALTER TABLE testtab CONVERT TO CHARACTER SET utf8mb4;
  2. Query OK, 0 rows affected (0.34 sec)
  3. Records: 0 Duplicates: 0 Warnings: 0

  4. mysql> alter table testtab modify column `contents` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
  5. Query OK, 0 rows affected (0.36 sec)
  6. Records: 0 Duplicates: 0 Warnings: 0

執行插入指令碼
檢視資料
mysql> select * from testtab;
+-----------+
| contents  |
+-----------+
|           |
|           |
| ????hello |
| ?hello    |   ---這條應該是正常的,四個?的應該是亂碼
+-----------+



注意:
這樣是對的。
說下前提,utf8mb4和utf8的區別就是utf8才用3位位元組碼儲存資料,而utf8mb4用4位位元組碼儲存資料。
emoji表情是4位位元組碼透過前段轉義出來的,它本身是不可讀的字元,所以在資料庫客戶端中會顯示“?”。
在前端支援emoji的地方就會顯示emoji表情


如果想在utf8的背景下插入emoji表情,得修改配置檔案,重啟資料庫啊!
參考:

http://blog.csdn.net/dqchouyang/article/details/51473544

            -----姜成堯
https://mp.weixin.qq.com/s/VWVKy16gMJ_kFeICsyMiVw   ---老葉茶館
http://mp.weixin.qq.com/s/HjzVoWe1fky0VfzhHi8uCg  ----ACUMG mysql使用utf8mb4 經驗吐血總結


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-2123781/,如需轉載,請註明出處,否則將追究法律責任。

相關文章