从MySQL5.5.3开始,MySQL 支持一种utf8mb4的字符集,这个字符集能够支持4字节的UTF8编码的字符。utf8mb4字符集能够完美地兼容utf8字符串。在数据存储方面,当一个普通中文字符存入数据库时仍然占用3个字节,在存入一个Unified Emoji表情的时个它会自动占用4个字节。所以在输入输出时都不会存在乱码的问题了。

由于utf8mb4是utf8的超集,从utf8升级到utf8mb4不会有任何的问题,直接升级即可;如果从别的字符集如gb2312或者gbk转化而来,一定要先备份数据库。然后,修改MySQL的配置文件/etc/my.cnf,修改连接默认字符集为utf8mb4,然后在连接数据库以后首先执行一句SQL:SET NAMES utf8mb4;

修改如下:

# 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

查看可以查看编码是否已经修改成功:

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后,再次登录的时候,出现下面的情况:

[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

还需要修改一个修数:

+ +--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

修改表的字符集:

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

打印机不能打印"emoji表情"!!!!!!!!所以最好不要存emoji表情。

联系方式QQ:326528263 EMAIL:clnking@163.com 网名:bass 分享技术 突破难点 创新思维