# 错误 1273 (HY000): 未知的排序规则：'utf8mb4_0900_ai_ci'

- **ID:** `database/mysql-unknown-collation`
- **领域:** database
- **类别:** encoding_error
- **错误码:** `1273`
- **验证级别:** ai_generated
- **修复率:** 83%

## 根因

MySQL 数据库或表使用了当前服务器版本不支持的排序规则（utf8mb4_0900_ai_ci），通常是因为该排序规则是在 MySQL 8.0 中引入的，但服务器运行在较旧版本（例如 5.7 或 5.6）上。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| MySQL 5.7.44 | active | — | — |
| MySQL 5.6.51 | active | — | — |
| MariaDB 10.11.8 | active | — | — |

## 解决方案

1. ```
   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.
   ```
2. ```
   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.
   ```
3. ```
   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** — 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. (80% 失败率)
- **Upgrading MySQL to 8.0 without testing compatibility** — While upgrading fixes the collation issue, it may introduce other incompatibilities (e.g., SQL syntax changes, deprecated features) that break the application. (75% 失败率)
- **Ignoring the error and continuing with the import** — The error stops the import process; ignoring it will result in an incomplete or failed database restore. (100% 失败率)
