mysql存储emoji表情要使用utf8mb4字符集,这是4字节存储,最低支持版本为5.5.3+,若不是,请升级到较新版本。
修改mysql配置文件
Window目录:
mysql/my.ini
Ubuntu16.04目录:
/etc/mysql/conf.d/mysql.cnf 配置Client
/etc/mysql/mysql.conf.d/mysql.cnf 配置Server
其他目录:
一般在etc/mysql/my.cnf
在mysql配置文件添加以下三部分内容:
[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,检查字符集
# mysql -uroot -p
# mysql > SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';
结果
+--------------------------+--------------------+
| 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 |
| collation_connection | utf8mb4_unicode_ci |
| collation_database | utf8mb4_unicode_ci |
| collation_server | utf8mb4_unicode_ci |
+--------------------------+--------------------+
rows in set (0.00 sec)
Java链接配置
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/tgb?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE
jdbc.user=root
jdbc.password=
jdbc.initialPoolSize=10
jdbc.minPoolSize=10
jdbc.maxPoolSize=50
jdbc.maxIdleTime=3600
jdbc.testConnectionOnCheckout=false
jdbc.testConnectionOnCheckin=true
jdbc.idleConnectionTestPeriod=3600