1.改 Mysql 配置文件 my.cnf(默认位置/etc/my.cnf)
[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'
2.改已有的 database、table 和 column 字符集
msql> ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
msql> ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
msql> ALTER TABLE table_name CHANGE column column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
其中的 database_name、table_name、column_name 要替换成你对应的数据库名、表名、列名。VARCHAR 的长度值也是。
3.重启 Mysql 并检查更改后的字符集设置
service mysql restart
查看字符集设置:
msql> 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)
4.使用 Druid 数据源配置,添加了一个配置:
5.jdbc.url=jdbc:mysql://localhost:3306/demo?autoReconnect=true&failOverReadOnly=false
去掉characterEncoding=utf8