讓MySQL支援Emoji表情

來源:互聯網
上載者:User

讓MySQL支援Emoji表情

讓MySQL支援Emoji表情,涉及無線相關的 MySQL 資料庫建議都提前採用 utf8mb4 字元集。

utf8mb4和utf8到底有什麼區別呢?原來以往的mysql的utf8一個字元最多3位元組,而utf8mb4則擴充到一個字元最多能有4位元組,所以能支援更多的字元集。

解決方案:將Mysql的編碼從utf8轉換成utf8mb4。

需要 >= MySQL 5.5.3版本、從庫也必須是5.5的了、低版本不支援這個字元集、複製報錯

停止MySQL Server服務

修改 my.cnf或者mysql.ini

[client]
 default-character-set = utf8mb4

[mysql]
 default-character-set = utf8mb4

[mysqld]
 character-set-client-handshake = FALSE
 character-set-server = utf8mb4
 collation-server = utf8mb4_unicode_ci
 init_connect='SET NAMES utf8mb4'

重啟 MySQL Server、檢查字元集

mysql> SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';

 

修改資料庫字元集:

ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

 

修改表的字元集:

ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

 

修改欄位的字元集:

ALTER TABLE table_name CHANGE column_name column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

 

如果只是某個欄位需要 只需要修改那個欄位的字元集就可以了

另外伺服器串連資料庫 Connector/J的串連參數中,不要加characterEncoding參數。 不加這個參數時,預設值就時autodetect。

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.