萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 資料庫 >> MySQL 教學

MySQL 錯誤代碼完整對照表 (1005-1235):含義、原因、解決方案

MySQL 官方錯誤代碼速查,涵蓋 1005-1235 常見錯誤,附帶中文含義、典型原因、解決建議,適合開發/運維快速診斷。

資料庫/資料表操作類 (1005-1011)

代碼訊息常見原因解決
1005Can't create table外鍵定義錯誤、磁碟滿、權限檢查 FK 參考表/欄位、磁碟空間、目錄權限
1006Can't create database權限不足、磁碟滿、目錄存在檢查 datadir 權限、空間
1007Database exists重複 CREATE DATABASE加 IF NOT EXISTS 或先 DROP
1008Database doesn't existDROP/USE 不存在的庫確認名稱拼寫
1009Error dropping database (can't remove file)檔案被佔用/權限停止服務、檢查鎖定
1010Error dropping database (can't remove dir)目錄非空/權限手動清理後再 DROP
1011Error dropping database (can't remove file)同 1009同上

資料操作與約束類 (1020-1062)

代碼訊息常見原因解決
1020Record changed by another user並發更新衝突樂觀鎖/重試邏輯
1021Disk full磁碟空間不足清理/擴容、檢查 tmpdir
1022Duplicate key唯一鍵/主鍵衝突檢查業務邏輯、用 INSERT IGNORE/ON DUPLICATE KEY
1032Record not foundUPDATE/DELETE 無匹配行確認 WHERE 條件
1036Table is read onlyMyISAM 表損壞/唯讀屬性REPAIR TABLE、檢查檔案屬性
1037Out of memory記憶體不足重啟服務、調大 innodb_buffer_pool
1040Too many connections連線數達上限調大 max_connections、檢查洩漏
1044Access denied for user權限不足GRANT 權限、確認 host 匹配
1045Access denied (user/pwd)帳號密碼錯誤重置密碼、檢查 skip-grant-tables
1048Column cannot be nullNOT NULL 欄位插入 NULL提供預設值或修改結構
1049Unknown database庫不存在建庫或修正連線字串
1050Table already exists重複 CREATE TABLE加 IF NOT EXISTS
1051Unknown table表不存在確認名稱、大小寫敏感度
1054Unknown column欄位不存在檢查拼寫、ALTER TABLE ADD
1062Duplicate entry for key唯一鍵衝突 (含主鍵)同 1022

網路與連線類 (1081, 1158-1161)

代碼訊息方向排查
1081Can't create socketClient→Server防火牆、端口、skip-networking、bind-address
1158Network read error雙向網路抖動、MTU、驅動程式
1159Network read timeoutClient→Server調大 net_read_timeout、檢查長查詢
1160Network write errorServer→Client結果集過大、網路中斷
1161Network write timeoutServer→Client調大 net_write_timeout

權限與資源類 (1130-1235)

代碼訊息解決
1130Host not allowed to connectGRANT ...@'%' 或具體 IP、FLUSH PRIVILEGES
1133User doesn't existCREATE USER、確認 host 部分
1142Command denied to userGRANT 具體權限 ON db.*
1146Table doesn't exist同 1051
1149Syntax error檢查 SQL、保留字需反引號
1180Commit failed檢查事務邏輯、磁碟空間
1181Rollback failed同上
1205Lock wait timeout調大 innodb_lock_wait_timeout、優化長事務
1216FK constraint fails (child)先插父表、檢查 FK 定義
1217FK constraint fails (parent)先刪子表、SET FOREIGN_KEY_CHECKS=0
1226User exceeded resource limitsMAX_QUERIES_PER_HOUR 等限制
1227Access denied (SUPER/ADMIN)需更高權限帳號
1235MySQL version too old升級版本或避免用新功能

快速查詢技巧

-- 官方文件線上查詢
https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html

-- 客戶端顯示錯誤代碼
mysql --show-warnings -u root -p

建議將高頻錯誤代碼整合至監控告警規則 (如 1040, 1062, 1205, 1216/1217)。

copyright © 萬盛學電腦網 all rights reserved