从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 -- [mysql]
# CLIENT #
port =
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 | |
+--------------------------+---------+
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
还需要修改一个修数:
+ +-- lines: <?xml version='1.0' encoding="utf-8"?>------|+ +-- lines: <?xml version='1.0' encoding="utf-8"?>-----
<description>ARMSCII- Armenian</description> | <description>ARMSCII- Armenian</description>
<alias>armscii-</alias> | <alias>armscii-</alias>
<collation name="armscii8_general_ci" id="" order="Arm| <collation name="armscii8_general_ci" id="" order="Ar
<collation name="armscii8_bin" id="" order="Binary"| <collation name="armscii8_bin" id="" order="Binary
</charset> | </charset>
|
<charset name="utf8mb4">#只修改这里(修改后) | <charset name="utf8">
<family>Unicode</family> | <family>Unicode</family>
<description>UTF- Unicode</description> | <description>UTF- Unicode</description>
<alias>utf-</alias> | <alias>utf-</alias>
<collation name="utf8_general_ci" id=""> | <collation name="utf8_general_ci" id="">
<flag>primary</flag> | <flag>primary</flag>
<flag>compiled</flag> | <flag>compiled</flag>
+ +-- lines: </collation>--------------------------------|+ +-- lines: </collation>-------------------------------
~ | ~
~ | ~
~ | ~
~ | ~
~ | ~
~ | ~
~ | ~
~ | ~
~ | ~
~ | ~
~ | ~
~ | ~
~ | ~
/usr/share/mysql/charsets/Index.xml , All /usr/share/mysql/charsets/Index.xml.bak , All
修改表的字符集:
mysql> alter table table_name convert to character set utf8mb4 collate utf8mb4_bin;
打印机不能打印"emoji表情"!!!!!!!!所以最好不要存emoji表情。