1. 程式人生 > 實用技巧 >"Host 'XXXXX' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'"【mysql8】

"Host 'XXXXX' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'"【mysql8】

報錯

2020-09-24 17:10:38,630 [C3P0PooledConnectionPoolManager
[identityToken->1hge0yrac1h3dzkqrkiu17|44c03695]-HelperThread-#1] 
WARN  c.m.v.resourcepool.BasicResourcePool 223 - com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@60c3133c -- 
Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire 
a needed new resource, we failed to succeed more than the maximum number of 
allowed acquisition attempts (10). Last acquisition attempt exception: 
java.sql.SQLException: null,  message from server: "Host 'XXXXX' 
is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'"

原因

同一個ip在短時間內產生太多(超過mysql資料庫max_connection_errors的最大值)中斷的資料庫連線而導致的阻塞;

解決

  • 步驟1:
  • 步驟2:
# mysql -uroot -p
mysql> flush hosts;

或者按照報錯日誌提示進行操作:

mysqladmin flush-hosts -hxxx -P3306 -uroot -ppwd;

後記

提高允許的max_connection_errors數量

mysql> use mysql;
mysql> show variables like '%max_connect_errors%';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 100   |
+--------------------+-------+
1 row in set (0.00 sec)

mysql> set global max_connect_errors = 1000; 

mysql> flush hosts;
  • max_connect_errors
    是一個MySQL中與安全有關的計數器值,它負責阻止過多嘗試失敗的客戶端以防止暴力破解密碼的情況。max_connect_errors的值與效能並無太大關係。預設情況下,my.cnf檔案中可能沒有此行,如果需要設定此數值,手動新增即可。
# vi /etc/my.cnf
max_connect_errors = 1000
  • 配置說明:
    當此值設定為10時,意味著如果某一客戶端嘗試連線此MySQL伺服器,但是失敗(如密碼錯誤等等)10次,則MySQL會無條件強制阻止此客戶端連線。如果希望重置此計數器的值,則必須重啟MySQL伺服器或者執行 mysql> flush hosts; 命令。 當這一客戶端成功連線一次MySQL伺服器後,針對此客戶端的max_connect_errors會清零。

  • 影響與錯誤形式:
    如果max_connect_errors的設定過小,則網頁可能提示無法連線資料庫伺服器;
    而通過SSH的mysql命令連線資料庫,則會返回 ERROR 1129 (00000): Host 'gateway' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 錯誤。

  • 功能與作用:
    一般來說建議資料庫伺服器不監聽來自網路的連線,僅僅通過sock連線,這樣可以防止絕大多數針對mysql的攻擊;如果必須要開啟mysql的網路連線,則最好設定此值,以防止窮舉密碼的攻擊手段。