只是为了添加到@deceze 的答案,我推荐一个配置良好的 MySQL 服务器(对我来说,在/etc/mysql/mysql.conf.d/mysqld.cnf
)。以下是确保您正在使用的配置选项utfmb4
,尽管我确实建议您检查每个 MySQL 配置选项,尽管令人生畏,但有很多默认值非常不理想。
[client]
default-character-set = utf8mb4
[mysql]
default_character_set = utf8mb4
[mysqld]
init-connect = "SET NAMES utf8mb4"
character-set-client-handshake = FALSE
character-set-server = "utf8mb4"
collation-server = "utf8mb4_unicode_ci"
autocommit = 1
block_encryption_mode = "aes-256-cbc"
最后一个只是应该默认的。此外,init-connect
处理不必每次都执行的问题。保持代码干净。现在运行:
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 |
+--------------------------+--------------------+
看起来你已经这样做了,但明确定义表创建并没有什么坏处:
CREATE TABLE `mysql_table` (
`mysql_column` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`mysql_column`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
希望这可以帮助某人。