排除 MySQL 技術疑難問題: Difference between revisions

Tag: wikieditor
 
Line 532: Line 532:
INSERT IGNORE INTO `target`.`table` SELECT * FROM `source`.`table` LIMIT 0, 10000;
INSERT IGNORE INTO `target`.`table` SELECT * FROM `source`.`table` LIMIT 0, 10000;
</pre>
</pre>
=== 解決 Host 'xx.xx.xxx.xxx' is blocked because of many connection errors ===
錯誤訊息
<pre>
Host 'xx.xx.xxx.xxx' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'
</pre>
了解問題狀況
(1) 查詢門檻值
MySQL 的機制是:同一個來源主機(依 IP 或 hostname 判斷),累積達到 `max_connect_errors` 設定的次數後,就會自動把這個來源封鎖,拒絕它繼續連線。
<pre>
SHOW VARIABLES LIKE 'max_connect_errors';
</pre>
查到的門檻值是 100。
(2) 查詢目前被記錄/封鎖的 host
<pre>
SELECT IP, HOST, SUM_CONNECT_ERRORS, COUNT_HOST_BLOCKED_ERRORS
FROM performance_schema.host_cache
ORDER BY SUM_CONNECT_ERRORS DESC;
</pre>
確認唯一被鎖的 IP 就是 `xx.xx.xxx.xxx`,`SUM_CONNECT_ERRORS` 剛好等於門檻值 100,且沒有其他 IP 受影響。
解決問題
⚠️ 提醒:`flush-hosts` / `FLUSH HOSTS` 會清空整個 host cache,不是只清單一 IP。如果查詢結果中還有其他 host 的記錄,執行後會一併重置。若為共用正式環境,執行前建議先跟其他管理者確認。
(1) 到終端機或命令提示字元,輸入指令
<pre>
mysqladmin -u root -p flush-hosts
</pre>
<pre>
mysql -u root -p
進入 MySQL SHELL 後再輸入指令:FLUSH HOSTS;
</pre>
補充:`FLUSH HOSTS` 語法自 MySQL 8.0.23 起已標示為 deprecated,8.4 版本中移除<ref>[https://dev.mysql.com/doc/refman/8.4/en/performance-schema-host-cache-table.html MySQL :: MySQL 8.4 Reference Manual :: 29.12.22.3 The host_cache Table]</ref><ref>[https://oneuptime.com/blog/post/2026-03-31-mysql-fix-max-connect-errors-blocked-hosts/view How to Fix max_connect_errors and Blocked Hosts in MySQL]</ref>。若環境版本較新,可改用官方建議的替代語法:
<pre>
TRUNCATE TABLE performance_schema.host_cache;
</pre>
(2) 執行後驗證
<pre>
SELECT IP, HOST, SUM_CONNECT_ERRORS, COUNT_HOST_BLOCKED_ERRORS
FROM performance_schema.host_cache
ORDER BY SUM_CONNECT_ERRORS DESC;
</pre>
結果:沒有資料行(0 rows),代表 host_cache 已清空,先前的連線錯誤紀錄與封鎖狀態都已重設,該 IP 應可正常連線。


=== 從 MySQL 資料轉移至 MSSQL ===
=== 從 MySQL 資料轉移至 MSSQL ===