數據庫支持emoji表情

從MySQL5.5.3開始,MySQL 支持一種utf8mb4的字符集,這個字符集可以支持4字節的UTF8編碼的字符。utf8mb4字符集可以完美地兼容utf8字符串。在數據存儲方面,當一個普通中文字符存入數據庫時仍然佔用3個字節,在存入一個Unified Emoji表情的時個它會自動佔用4個字節。因此在輸入輸出時都不會存在亂碼的問題了。mysql

因爲utf8mb4是utf8的超集,從utf8升級到utf8mb4不會有任何的問題,直接升級便可;若是從別的字符集如gb2312或者gbk轉化而來,必定要先備份數據庫。而後,修改MySQL的配置文件/etc/my.cnf,修改鏈接默認字符集爲utf8mb4,而後在鏈接數據庫之後首先執行一句SQL:SET NAMES utf8mb4;sql

修改以下:數據庫

# cat /etc/my.cnf 
# update 2015-3-30

[mysql]
# CLIENT #
port                           = 3306
socket                         = /data1/dbdata/mysql.sock
default-character-set          = utf8mb4 #關鍵點1

[mysqld]
# GENERAL #
user                           = mysql
default-storage-engine         = InnoDB
character-set-server           = utf8mb4 #關鍵點2

 查看能夠查看編碼是否已經修改爲功:socket

mysql> show variables like 'character%';
+--------------------------+---------+
| 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     | utf8mb4 |
| character_set_system     | utf8    |
| character_sets_dir       |         |
+--------------------------+---------+
8 rows in set (0.00 sec)

 

utf8 轉utf8mb4就能夠支持emoji表情了編碼

當重啓mysql後,再次登陸的時候,出現下面的狀況:spa

[root@db228 ~]# mysql
mysql: Character set 'utf8mb4' is not a compiled character set and is not specified in the '/usr/share/mysql/charsets/Index.xml' file

http://blog.csdn.net/amao1/article/details/34851533.net

還須要修改一個修數:code

+ +--427 lines: <?xml version='1.0' encoding="utf-8"?>------|+ +--427 lines: <?xml version='1.0' encoding="utf-8"?>-----
    <description>ARMSCII-8 Armenian</description>           |    <description>ARMSCII-8 Armenian</description>
    <alias>armscii-8</alias>                                |    <alias>armscii-8</alias>
    <collation name="armscii8_general_ci" id="32" order="Arm|    <collation name="armscii8_general_ci" id="32" order="Ar
    <collation name="armscii8_bin"    id="64" order="Binary"|    <collation name="armscii8_bin"    id="64" order="Binary
  </charset>                                                |  </charset>
                                                            |  
  <charset name="utf8mb4">#只修改這裏(修改後)                |  <charset name="utf8">                                    
    <family>Unicode</family>                                |    <family>Unicode</family>
    <description>UTF-8 Unicode</description>                |    <description>UTF-8 Unicode</description>
    <alias>utf-8</alias>                                    |    <alias>utf-8</alias>
    <collation name="utf8_general_ci" id="33">              |    <collation name="utf8_general_ci" id="33">
     <flag>primary</flag>                                   |     <flag>primary</flag>
     <flag>compiled</flag>                                  |     <flag>compiled</flag>
+ +--158 lines: </collation>--------------------------------|+ +--158 lines: </collation>-------------------------------
  ~                                                         |  ~                                                        
  ~                                                         |  ~                                                        
  ~                                                         |  ~                                                        
  ~                                                         |  ~                                                        
  ~                                                         |  ~                                                        
  ~                                                         |  ~                                                        
  ~                                                         |  ~                                                        
  ~                                                         |  ~                                                        
  ~                                                         |  ~                                                        
  ~                                                         |  ~                                                        
  ~                                                         |  ~                                                        
  ~                                                         |  ~                                                        
  ~                                                         |  ~                                                        
/usr/share/mysql/charsets/Index.xml       436,1          All /usr/share/mysql/charsets/Index.xml.bak  434,1          All

 修改表的字符集:server

mysql> alter table table_name convert to character set utf8mb4 collate utf8mb4_bin;

 打印機不能打印"emoji表情"!!!!!!!!因此最好不要存emoji表情。xml

相關文章
相關標籤/搜索