ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 104/111
ERROR 2013 (HY000): Lost connection to MySQL server during query
特徵:
mysql 連接正常,查詢正常。STOP SLAVE; 隨機中斷。max_allowed_packet、wait_timeout、net_read_timeout 均正常。MySQL 預設啟用 基於主機名的權限驗證:
mysql.user 表匹配 Host 列。ERROR 2013。交互式客戶端可能緩存了 DNS 結果,腳本批量執行並發觸發 DNS 超時,導致必現。
[mysqld]
skip-name-resolve
效果:權限匹配直接用 IP,跳過 DNS,連接建立延遲從秒級降至毫秒級。
副作用:mysql.user 表 Host 列不能再用主機名 (如 'user'@'app-server'),必須改為 IP 或 %。
/etc/hosts 映射常用客戶端 IP。resolv.conf 選項 options timeout:1 attempts:1 降低超時重試。[mysqld]
net_read_timeout = 60
net_write_timeout = 60
connect_timeout = 10
僅緩解症狀,不解決 DNS 根因。
# 1. 測試反向解析
dig -x @
# 2. MySQL 內部狀態
SHOW GLOBAL STATUS LIKE 'Aborted_connects'; -- 異常增長即為 DNS/網絡問題
# 3. 慢查詢日誌無記錄,錯誤日誌出現 "Host name could not be resolved"
ERROR 2013 高發於「交互式正常、腳本必現」場景,90% 為 DNS 反向解析超時。生產環境**強制開啟 skip-name-resolve**,同時清理 mysql.user 主機名賬號,是最高性價比的永久治愈方案。