MySQL 錯誤代碼為數字(1xxx 系列),快速對照能直指問題核心。以下彙整高頻錯誤碼、含義與初步排查方向。
| 代碼 | 訊息 | 排查方向 |
|---|---|---|
| 1040 | Too many connections | 調大 max_connections、檢查連線洩漏、啟用連線池 |
| 1042 | Bad host name | DNS 解析失敗,檢查 /etc/hosts、skip-name-resolve |
| 1043 | Bad handshake | 客戶端/伺服器協定版本不匹配 |
| 1044 | Access denied for user | 檢查 GRANT、Host 匹配、FLUSH PRIVILEGES |
| 1045 | Access denied (password) | 密碼錯誤、密碼插件不匹配(caching_sha2_password vs mysql_native_password) |
| 1130 | Host not allowed to connect | 使用者 Host 欄位不允許該 IP,需 GRANT ... TO 'user'@'%' |
| 1133 | User not found | 使用者不存在,檢查拼寫、Host |
| 1227 | Access denied (SUPER privilege) | 缺少 SUPER/SHUTDOWN 等管理權限 |
| 代碼 | 訊息 | 排查方向 |
|---|---|---|
| 1022 | Duplicate key | 唯一索引/主鍵衝突,檢查業務邏輯或用 INSERT IGNORE/ON DUPLICATE KEY UPDATE |
| 1048 | Column cannot be null | NOT NULL 欄位未給值,檢查 DEFAULT 或應用層驗證 |
| 1062 | Duplicate entry | 同 1022,唯一鍵值重複 |
| 1169 | Duplicate entry on UPDATE | 更新導致唯一鍵衝突 |
| 1216 | FK constraint fails (child) | 子表新增/修改找不到父表對應值 |
| 1217 | FK constraint fails (parent) | 刪除/修改父表時子表仍有引用,需先處理子表 |
| 代碼 | 訊息 | 排查方向 |
|---|---|---|
| 1010 | Can't remove data directory | 目錄非空或權限不足 |
| 1011 | Can't delete database file | 檔案鎖定/權限/磁碟滿 |
| 1021 | Disk full | 清理空間、遷移資料目錄、擴充磁碟 |
| 1037 | Out of memory | 調整 innodb_buffer_pool_size、檢查記憶體洩漏 |
| 1038 | Out of sort memory | 調大 sort_buffer_size/max_heap_table_size |
| 1114 | Table is full | MyISAM 檔案達 2GB/4GB 上限,轉 InnoDB 或分表 |
| 1116 | Too many open tables | 調大 table_open_cache、檢查表數量 |
| 1158/1159 | Network read error/timeout | 網路不穩、防火牆、net_read_timeout |
| 1160/1161 | Network write error/timeout | 同上,檢查 net_write_timeout |
| 1203 | User max connections reached | 單用戶連線數達 max_user_connections,調大或限制應用連線池 |
| 1205 | Lock wait timeout | 鎖等待超過 innodb_lock_wait_timeout(預設 50s),排查長交易/熱點行 |
| 代碼 | 訊息 | 排查方向 |
|---|---|---|
| 1049 | Unknown database | 資料庫不存在,檢查拼寫、大小寫敏感度(Linux) |
| 1050 | Table already exists | 建表前先 DROP IF EXISTS 或 CREATE IF NOT EXISTS |
| 1051 | Unknown table | 表不存在 |
| 1054 | Unknown column | 欄位不存在,檢查拼寫、ALTER TABLE 是否生效 |
| 1065 | Empty query | 傳送空 SQL 字串,檢查應用組裝邏輯 |
| 1081 | Can't create socket | Socket 檔案路徑權限、mysqld.sock 遺失 |
| 1149 | Syntax error | SQL 語法錯誤,對照保留字、引號、逗號 |
| 1235 | Version too old | 功能需較新版本(如 CTE 需 8.0+、窗口函數需 8.0+) |
perror 1062(命令列工具)可直接顯示錯誤說明。