mysql存储emoji表情要使用utf8mb4字符集,这是4字节存储,最低支持版本为5.5.3+,若不是,请升级到较新版本。

修改mysql配置文件

Window目录:mysql/my.iniUbuntu16.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 = FALSEcharacter-set-server = utf8mb4collation-server = utf8mb4_unicode_ciinit_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.Driverjdbc.jdbcUrl=jdbc:mysql://localhost:3306/tgb?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUEjdbc.user=rootjdbc.password=jdbc.initialPoolSize=10jdbc.minPoolSize=10jdbc.maxPoolSize=50jdbc.maxIdleTime=3600jdbc.testConnectionOnCheckout=falsejdbc.testConnectionOnCheckin=truejdbc.idleConnectionTestPeriod=3600