MySQL錯誤 Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'問題解決
錯誤:Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'
原因:
同一個ip在短時間內產生太多(超過mysql資料庫max_connection_errors的最大值)中斷的資料庫連線而導致的阻塞;
max_connect_errors是一個MySQL中與安全有關的計數器值,它負責阻止過多嘗試失敗的客戶端以防止暴力破解密碼的情況。
max_connect_errors的值與效能並無太大關係,預設是10。意味著如果某一客戶端嘗試連線此MySQL伺服器,但是失敗(如密碼錯誤等等)10次 ,則MySQL會無條件強制阻止此客戶端連線。
如果希望重置此計數器的值,則必須重啟MySQL伺服器或者執行mysql> flush hosts; 命令。當這一客戶端成功連線一次MySQL伺服器後,針對此客戶端的max_connect_errors會清零。
如果max_connect_errors設定過小,則網頁可能提示無法連線資料庫伺服器。
解決方法:
1. 最簡單快捷(治標不治本):
# 登入mysql
mysql -u root -p
# 執行
flush hosts;
或者
在查詢到的目錄下使用命令修改:mysqladmin -u xxx -p flush-hosts
重新整理快取,發現錯誤已經解決,但是當max_connection_errors值再次變大後,需重新操作此方法。
備註:配置有master/slave主從資料庫的要把主庫和從庫都修改一遍的;
使用mysqladmin flush-hosts 命令清理一下hosts檔案(不知道mysqladmin在哪個目錄下可以使用命令查詢:whereis mysqladmin);
2. 提高允許的max_connection_errors數量
進入Mysql資料庫檢視max_connection_errors: show variables like '%max_connection_errors%';
show variables like '%max_connection_errors%';
修改max_connection_errors的數量為1000: set global max_connect_errors = 1000;
set global max_connect_errors = 1000;
檢視是否修改成功:show variables like '%max_connection_errors%';
show variables like '%max_connection_errors%';
3. 重啟mysqld
也可以在重啟之前,在配置檔案中將該引數調大。
# vi /etc/my.cnf
max_connect_errors = 100