Mysql升级过程的问题

升级安装5.6版本mysql

linux环境下的yum默认mysql版本是5.1的,由于项目需要保存表情等4个字节的数据,版本受限,需要升级到5.6版本支持utf8mb4格式的编码。

升级过程大概就是备份数据库,完全卸载旧版本,重新安装5.6版本,原先5.1版本下的数据库需要根据自己情况调整设置

安装成功后,测试保存emoji表情符号

修改数据库编码格式

例如:

ALTER DATABASE Test CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

修改表的编码格式

例如:

ALTER TABLE TABLE_NAME CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; 

 测试提交emoji表情,仍然返回不能保存这个字符

java.sql.SQLException: Incorrect string value: ‘xF0x9Fx92x94’ for column ‘name’ at row 1

检查数据库变量

SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';
Variable_nameValue
character_set_client utf8
character_set_connection utf8
character_set_database utf8
character_set_filesystem binary
character_set_results utf8
character_set_server utf8
character_set_system utf8
collation_connection utf8_unicode_ci
collation_database utf8_unicode_ci
collation_server utf8_unicode_ci

发现数据库的系统变量仍然是utf8编码格式的

以下几个一定要设置成utf8mb4格式

系统变量描述
character_set_client (客户端来源数据使用的字符集)
character_set_connection (连接层字符集)
character_set_database (当前选中数据库的默认字符集)
character_set_results (查询结果字符集)
character_set_server (默认的内部操作字符集)

数据库连接参数中: 
characterEncoding=utf8会被自动识别为utf8mb4,也可以不加这个参数,会自动检测。 
而autoReconnect=true是必须加上的。

修改my.conf文件

下面的这个是mysql的配置文件,尤为重要,如果不指定启动的编码格式,还是会默认安装utf8格式启动,就算数据库和表字段都设置成utf8mb4编码格式依然会不能保存4个字节的数据

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

[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了,使用这个配置重启服务即可

service mysqld stop// 关闭服务
service mysqld start// 开启服务

重启完成后我们再次查看系统变量,输出如下

Variable_nameValue
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

再次测试自己的接口,保存成功

原文地址:https://www.cnblogs.com/MrSong97/p/10500130.html