# ERROR 1781 (HY000): 当 @_MODE = ON 时，@_NEXT 不能设置为 UUID:NUMBER。

- **ID:** `database/mysql-gtid-purge-failure`
- **领域:** database
- **类别:** runtime_error
- **错误码:** `1781`
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

在启用 GTID 模式时尝试手动将 gtid_next 设置为特定 GTID，这是不允许的，因为 GTID 由服务器自动生成。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| MySQL 5.7 | active | — | — |
| MySQL 8.0 | active | — | — |
| MySQL 8.4 | active | — | — |

## 解决方案

1. ```
   首先将 gtid_next 设置为 'AUTOMATIC'：`SET @@SESSION.GTID_NEXT = 'AUTOMATIC';` 然后让服务器自动生成 GTID。如果需要注入特定 GTID（例如，用于复制恢复），只有在确保 GTID_MODE 为 OFF 或使用不同会话时，才使用 `SET GTID_NEXT = 'UUID:NUMBER'`。
   ```
2. ```
   如果需要跳过 GTID（例如，副本上发生重复键错误后），使用 `SET GTID_NEXT = 'CONSISTENCY';` 然后 `BEGIN; COMMIT;` 然后重置为 AUTOMATIC：`SET @@SESSION.GTID_NEXT = 'AUTOMATIC';`
   ```

## 无效尝试

- **Set gtid_next to 'AUTOMATIC' before setting it to a specific value** — This is the default state; the error occurs because you're trying to set gtid_next to a specific UUID:NUMBER, which is only allowed in manual mode or when GTID_MODE is OFF (90% 失败率)
- **Disable GTID mode globally with SET GLOBAL gtid_mode = OFF** — Changing gtid_mode requires a specific sequence (OFF -> OFF_PERMISSIVE -> ON_PERMISSIVE -> ON) and may break replication if GTIDs are in use (70% 失败率)
- **Ignore the error and proceed with the transaction** — The transaction will fail to commit because gtid_next is in an invalid state (100% 失败率)
