2019-05-28 回答
(1.修改database,table,column字符集
alter database database_name character set = utf8mb4 collate = utf8mb4_unicode_ci;
alter table table_name convert to character set utf8mb4 collate utf8mb4_unicode_ci;
alter table table_name change column_name varchar(191) character set utf8mb4 collate utf8mb4_unicode_ci;
(2.修改mysql配置文件my.cnf(window为my.ini)
[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'
(3.用的是java服务器,升级或者确保mysql connection版本高于5.1.13否则仍然不能试用utf8mb4
(4.服务器端的db配置文件
jdbc.driverclassname=com.mysql.jdbc.driver
jdbc.url=jdbc:mysql://localhost:3306/database?useunicode=true&characterencoding=utf8&autoreconnect=true&rewritebatchedstatements=true
jdbc.username=root
jdbc.password=password
如果升级了mysql-connector,其中的characterencoding=utf8可以自动被识别为utf8mb4(兼容原来的utf8),而
autoreconnection(当数据库连接异常中断时,是否自动重新连接?默认为false)强烈建议配上,忽略这个属性,可能导致缓存缘故 ,
没有读取到db最新的配置,导致一直无法试用utf8mb4字符集;