1.mysql Server收到哀求时将哀求数据从 character_set_client 转换为 character_set_connection
2.进行内部操作前将哀求数据从 character_set_connection 转换为内部操作字符集,步调如下
A. 使用每个数据字段的 CHARACTER SET 设定值;
B. 若上述值不存在,则使用对应数据表的字符集设定值
C. 若上述值不存在,则使用对应数据库的字符集设定值;
D. 若上述值不存在,则使用 character_set_server 设定值。
3.最后将操作结果从内部操作字符集转换为 character_set_results
临时修改配置
上面的配置都可以通过命令临时修改:
SET character_set_client = utf8mb4;
SET character_set_connection = utf8mb4;
SET character_set_database = utf8mb4;
SET character_set_results = utf8mb4;
SET character_set_server = utf8mb4;
SET collation_connection = utf8mb4_unicode_ci;
SET collation_database = utf8mb4_unicode_ci;
SET collation_server = utf8mb4_unicode_ci;
复制代码
固然,也可以通过修改 my.ini 配置文件。
修改 mysql 服务器配置文件
比如 windows 下个人的 mysql 安装目次为:D:\tools\mysql\mysql-5.7.24-winx64
那就在下面创建 my.ini(如果没有的话)。
内容如下:
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4
[mysqld]
# 设置3306端口
port=3306
# 允许最大连接数
max_connections=20
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
collation-server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
character-set-client-handshake = FALSE
explicit_defaults_for_timestamp=true
[client]
default-character-set=utf8mb4
复制代码
修改完成后需要重启 mysql 服务。
可以在 bin 下执行 mysqld restart。这个实践下来只初始化了部分编码。
个人实在 windows services(服务) 下,把 mysql 服务进行了重新启动。
jdbc 配置
For Connector/J 8.0.12 and earlier: In order to use the utf8mb4 character set for the connection, the server MUST be configured with character_set_server=utf8mb4; if that is not the case, when UTF-8 is used for characterEncoding in the connection string, it will map to the MySQL character set name utf8, which is an alias for utf8mb3.
For Connector/J 8.0.13 and later:
When UTF-8 is used for characterEncoding in the connection string, it maps to the MySQL character set name utf8mb4.
If the connection option connectionCollation is also set alongside characterEncoding and is incompatible with it, characterEncoding will be overridden with the encoding corresponding to connectionCollation.
Because there is no Java-style character set name for utfmb3 that you can use with the connection option charaterEncoding, the only way to use utf8mb3 as your connection character set is to use a utf8mb3 collation (for example, utf8_general_ci) for the connection option connectionCollation, which forces a utf8mb3 character set to be used, as explained in the last bullet.
Warning
Do not issue the query SET NAMES with Connector/J, as the driver will not detect that the character set has been changed by the query, and will continue to use the character set configured when the connection was first set up.