1.修改my.cnf
[client]
default-character-set = utf8mb4
[mysqld]
collation-server = utf8mb4_unicode_ci
character-set-server = utf8mb4
重启mysql,检查字符集是否已经更改,除了character_set_system
和character_set_filesystem
之外,其他的字符集都需要变成utf8mb4类型。
查看编码如下。
mysql> show variables like 'char%';
+--------------------------+------------------------------------------------------+
| Variable_name | Value |
+--------------------------+------------------------------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/Cellar/mysql/5.5.10/share/mysql/charsets/ |
+--------------------------+------------------------------------------------------+
2.为了支持之前用utf8创建的表同样支持emoji,你还需要将之前的表修改未utf8mb4字符集
alter table posts convert to character set utf8mb4 collate utf8mb4_unicode_ci;
3.如果处理完成之后,插入还是失败,那么在db的URL参数上不要加characterEncoding参数。 不加这个参数时,默认值就时autodetect。
4.mysql支持emoji有版本约束,建议mysql升级至5.5,mysql-connetion升级至5.1.30。