Home / Databases / MySQL

DatabasesTroubleshootingBeginner

MySQL 'Host is blocked because of many connection errors' (error 1129): unblock it and find the client that caused it

Unblock a host locked out by max_connect_errors, use performance_schema.host_cache to find which client keeps breaking handshakes, and fix the health check or network fault behind it.

Deepak Bhardwaj7 min read

Applies toMySQL 8.0 and 8.4 LTS, Amazon RDS/Aurora MySQL, Azure Database for MySQL Flexible Server

The symptom

An application server suddenly can't connect, while other hosts connect fine:

ERROR 1129 (HY000): Host '10.20.4.17' is blocked because of many connection errors;
unblock with 'mysqladmin flush-hosts'

Restarting the application doesn't help. The block is held by the MySQL server, not the client, and it lasts until someone clears it or the server restarts.

Why it happens

MySQL keeps a host cache for non-localhost TCP clients. For each host it counts connection attempts that fail during the protocol handshake. When a host reaches max_connect_errors (default 100) of these in a row without a successful connection, the server refuses every new connection from that IP address.

Wrong passwords usually don't trigger it. Authentication failures go to a separate counter. What counts is a client that opens a TCP connection and doesn't finish the handshake. Common culprits:

Cause What it looks like Fix
Load balancer or monitoring tool doing TCP-only health checks Errors from the LB's own IP, at a regular interval Use a MySQL-protocol check, or point the check somewhere else
Port scanner or security agent Bursts from one IP, often outside business hours Exclude the database port, or allow-list the scanner and raise the limit
Network drops or firewall idle timeouts during connect Errors from app servers, matching network incidents Fix the network path, review connect_timeout
TLS handshake failures (client/server TLS mismatch) COUNT_SSL_ERRORS increasing Align TLS versions and certificates
Client killed mid-connect (pool shutdown, container OOM) Errors during deploys or restarts Shut pools down gracefully

When the IP is a load balancer or NAT gateway, one blocked IP can take out every client behind it. That is how a small problem turns into an outage.

Step 1: Unblock the host now

Connect from a host that isn't blocked (or from localhost on the server) with an account that has the right privileges.

The supported way on MySQL 8.0.23 and later is to truncate the host cache table:

TRUNCATE TABLE performance_schema.host_cache;

That clears the in-memory cache and unblocks every blocked host. It needs the DROP privilege on that table. From the shell, this does the same:

mysqladmin -h db01.internal -u dba -p flush-hosts

Warning

FLUSH HOSTS was deprecated in MySQL 8.0.23 in favour of truncating performance_schema.host_cache, and it isn't available in newer releases. Use TRUNCATE TABLE in runbooks and scripts so they keep working after an upgrade.

On Amazon RDS and Aurora MySQL, run the same statement as the master user. If your managed service won't let you run it, check the provider's documentation for a stored procedure or a portal action, or raise max_connect_errors in the parameter group or server parameters (see Step 4).

Caution

Unblocking without finding the cause buys you minutes, not hours. If a health check is causing the errors, the host will be blocked again after the next 100 bad attempts.

Step 2: Find which hosts are failing

Before you truncate the table (or soon after, once it fills up again), check which hosts are close to the limit and what kind of errors they produce:

SELECT IP,
       HOST,
       SUM_CONNECT_ERRORS,
       COUNT_HOST_BLOCKED_ERRORS,
       COUNT_HANDSHAKE_ERRORS,
       COUNT_SSL_ERRORS,
       COUNT_AUTHENTICATION_ERRORS,
       FIRST_ERROR_SEEN,
       LAST_ERROR_SEEN
FROM performance_schema.host_cache
WHERE SUM_CONNECT_ERRORS > 0
   OR COUNT_HOST_BLOCKED_ERRORS > 0
ORDER BY SUM_CONNECT_ERRORS DESC;

How to read it:

  • SUM_CONNECT_ERRORS is the counter that's compared with max_connect_errors. A host at or above that value is blocked.
  • COUNT_HOST_BLOCKED_ERRORS counts connections refused because the host was already blocked. If it keeps rising, the client is still retrying.
  • COUNT_HANDSHAKE_ERRORS rising with no TLS or auth errors usually means something connects and hangs up without speaking MySQL. That points to TCP health checks or scanners.
  • COUNT_SSL_ERRORS points to a TLS mismatch.
  • FIRST_ERROR_SEEN and LAST_ERROR_SEEN give you a time window to match against deploys, LB logs or firewall logs.

Check the current limit and the server-wide counters too:

SHOW GLOBAL VARIABLES LIKE 'max_connect_errors';
SHOW GLOBAL VARIABLES LIKE 'connect_timeout';
SHOW GLOBAL STATUS LIKE 'Aborted_connects';
SHOW GLOBAL STATUS LIKE 'Connection_errors%';

A steadily rising Aborted_connects along with rising SUM_CONNECT_ERRORS for one IP confirms that a single client is the source.

Step 3: Identify the client behind the IP

If the IP is a load balancer, the database only sees the balancer. Look at its health check configuration:

  • HAProxy: option tcp-check with no MySQL-aware check opens and closes connections. Use option mysql-check user <haproxy_check_user> with a dedicated passwordless user that can only connect from the HAProxy hosts, or use an HTTP check against an agent that queries MySQL.
  • Cloud network load balancers with TCP health checks do the same thing. Lengthen the interval, use a different health check target, or put a MySQL-aware proxy (such as ProxySQL or MySQL Router) behind them.
  • Kubernetes tcpSocket liveness or readiness probes against port 3306 behave the same way. Replace them with an exec probe that runs mysqladmin ping.

On the database host, see who is opening connections right now:

sudo ss -tn state established '( sport = :3306 )' | awk '{print $4}' | cut -d: -f1 | sort | uniq -c | sort -rn | head

For short-lived connections that don't show up in ss, capture a few seconds of SYN packets for the IP you suspect:

sudo tcpdump -nn -i any 'tcp[tcpflags] & tcp-syn != 0 and port 3306 and host 10.20.4.17' -c 50

A SYN every few seconds at a fixed interval almost always means a health check.

Step 4: Fix it and set a sensible limit

Fix the client first. Then decide on max_connect_errors. The default of 100 is low for busy environments behind shared NAT or load balancer IPs. Raising it is a reasonable defence while you fix things properly:

SET PERSIST max_connect_errors = 10000;

SET PERSIST writes the value to mysqld-auto.cnf so it survives a restart. On RDS, Aurora and Azure Flexible Server, change it in the parameter group or server parameters instead, where the service exposes it.

Tip

Don't set max_connect_errors to its maximum to "turn the feature off". It protects the server from brute-force handshake floods. A high but finite value plus a fixed health check is the better trade-off.

If clients sometimes fail to connect on slow or congested links, check connect_timeout (seconds the server waits for the handshake packet). Raising it a little can reduce spurious handshake errors. It won't fix a client that never sends the packet.

Verify

After the fix, the error counter for the host should stay at zero:

SELECT IP, SUM_CONNECT_ERRORS, COUNT_HANDSHAKE_ERRORS, LAST_ERROR_SEEN
FROM performance_schema.host_cache
WHERE IP = '10.20.4.17';

Check again after a few health-check intervals. If SUM_CONNECT_ERRORS is still rising, the source is still active.

Prevent it next time

  • Alert on performance_schema.host_cache rows where SUM_CONNECT_ERRORS is above 50% of max_connect_errors. That gives you a warning before the host is blocked.
  • Graph Aborted_connects and Connection_errors_* with your usual MySQL exporter and alert when they jump.
  • Use MySQL-aware health checks in load balancers, proxies and Kubernetes probes. Record them in your platform standards.
  • Set max_connect_errors explicitly in your configuration baseline instead of relying on the default.
  • Put the TRUNCATE TABLE performance_schema.host_cache; command in your on-call runbook, with a note to find the source before closing the incident.