错误 1273 (HY000): 未知的排序规则:'utf8mb4_0900_ai_ci'
ERROR 1273 (HY000): Unknown collation: 'utf8mb4_0900_ai_ci'
ID: database/mysql-unknown-collation
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| MySQL 5.7.44 | active | — | — | — |
| MySQL 5.6.51 | active | — | — | — |
| MariaDB 10.11.8 | active | — | — | — |
根因分析
MySQL 数据库或表使用了当前服务器版本不支持的排序规则(utf8mb4_0900_ai_ci),通常是因为该排序规则是在 MySQL 8.0 中引入的,但服务器运行在较旧版本(例如 5.7 或 5.6)上。
English
A MySQL database or table uses a collation (utf8mb4_0900_ai_ci) that is not supported by the current server version, typically because the collation was introduced in MySQL 8.0 but the server is on an older version (e.g., 5.7 or 5.6).
官方文档
https://dev.mysql.com/doc/refman/8.0/en/charset-unicode-utf8mb4.html解决方案
-
Replace the collation in the SQL dump file using sed: sed -i 's/utf8mb4_0900_ai_ci/utf8mb4_general_ci/g' dump.sql, then import the modified dump: mysql -u root -p mydb < dump.sql.
-
If the dump is from a MySQL 8.0 database, use mysqldump with the --compatible=mysql56 flag to generate a dump compatible with older versions: mysqldump --compatible=mysql56 -u root -p mydb > dump.sql.
-
Manually alter the collation of the affected tables after import (if the import partially succeeds): ALTER TABLE my_table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
无效尝试
常见但无效的做法:
-
Changing the collation to a random one like 'utf8mb4_general_ci' without checking table definitions
80% 失败
This may work for some tables but can cause data inconsistency if the original collation had specific requirements (e.g., case sensitivity); also, it requires altering each table individually.
-
Upgrading MySQL to 8.0 without testing compatibility
75% 失败
While upgrading fixes the collation issue, it may introduce other incompatibilities (e.g., SQL syntax changes, deprecated features) that break the application.
-
Ignoring the error and continuing with the import
100% 失败
The error stops the import process; ignoring it will result in an incomplete or failed database restore.