MySQL 表不是 UTF-8,即使我指示 Hibernate 使用 UTF-8 创建它们

MySQL tables are not UTF-8 even though I instructed Hibernate to do create them with UTF-8

我正在使用 Spring 引导应用程序,即使我指示 Hibernate 在 UTF-8 中创建新表,所有新表都在 latin1.

spring.jpa.properties.hibernate.connection.characterEncoding=utf-8
spring.jpa.properties.hibernate.connection.CharSet=utf-8
spring.jpa.properties.hibernate.connection.useUnicode=true

我正在使用 Hikari。

我的数据库有 latin1 编码:

SELECT @@character_set_database, @@collation_database;

首先修复数据库:

foreach table: ALTER TABLE @table CONVERT TO CHARACTER SET utf8;
ALTER DATABASE javabg CHARACTER SET utf8 COLLATE utf8_unicode_ci;

确保所有连接都是 utf8 - 编辑 my.cnf:

[mysqld]
#mist: set default encoding to utf-8
init-connect='SET NAMES utf8'
character-set-server = utf8
collation-server = utf8_general_ci

注意:当您连接为 root 时,init-connect 将被忽略。

ALTER TABLE @table CONVERT TO CHARACTER SET utf8; 仅当您现在在 table 中对数据进行适当的 latin1 编码时才适用。如果您在 latin1 字段中放置了非西欧文本,则解决方案是其他的。请

SELECT col, HEX(col) ...

看看里面现在有什么。

如果要在网页上输出,您需要检查元标记是否指定 utf8。