ERROR 1040 (HY000): Too many connections
SQLSTATE[HY000] [1040] Too many connections

MySQL refuses new connections when the number of open connections reaches max_connections (151 by default). The site shows database errors, and you may not even be able to log in to investigate. Raising the limit gets you back online — but usually the real problem is that connections are being held too long.

1. Get in

MySQL reserves one extra connection for an account with the CONNECTION_ADMIN (or SUPER) privilege — so an administrator can usually still log in:

sudo mysql

MySQL 8.0.14+ also offers a dedicated administrative port (admin_address/admin_port) for exactly this situation if configured.

2. Look at the connections

SHOW VARIABLES LIKE 'max_connections';
SHOW GLOBAL STATUS LIKE 'Threads_connected';
SHOW GLOBAL STATUS LIKE 'Max_used_connections';
SHOW FULL PROCESSLIST;

Group them:

SELECT user, host, db, command, COUNT(*) AS n, MAX(time) AS max_seconds
FROM information_schema.processlist
GROUP BY user, host, db, command
ORDER BY n DESC;

What to look for:

  • Many Query connections with high time — slow queries piling up. Each waiting request keeps a connection.
  • Many Sleep connections — clients connect and don’t disconnect (or persistent connections that are never reused).
  • Locked / Waiting for … lock — one long transaction or table lock blocking everyone else.
  • One user or host dominating — a runaway script, a misconfigured app server, or a bot hammering one endpoint.

3. Relieve the pressure

Kill long-running or blocking queries:

KILL QUERY 12345;     -- cancel the query
KILL 12345;           -- close the connection

Generate kill statements for idle connections older than 5 minutes:

SELECT CONCAT('KILL ', id, ';') FROM information_schema.processlist
WHERE command = 'Sleep' AND time > 300;

Lower the idle timeout so abandoned connections close sooner:

SET GLOBAL wait_timeout = 300;
SET GLOBAL interactive_timeout = 300;

4. Raise max_connections — carefully

SET GLOBAL max_connections = 300;

Make it permanent in the config (max_connections = 300 under [mysqld]).

Each connection uses memory (thread stack plus per-session buffers such as sort_buffer_size, join_buffer_size, and temporary tables). Too many connections on a small server can trigger the OOM killer, which is worse than error 1040. Rough sizing: keep max_connections comfortably above your real peak (Max_used_connections), not at an arbitrary high number.

5. Fix the real cause

Slow queries — the most common cause. When queries take seconds instead of milliseconds, concurrent requests stack up. Find them with the slow query log and fix indexes.

Too many application workers — PHP-FPM with pm.max_children = 200 can open 200 connections at once. The total of all app workers (across all servers) should stay below max_connections. Tune workers to your CPU and RAM — see PHP-FPM tuning.

Traffic spikes and bots — page caching keeps most requests away from PHP and MySQL entirely; rate limiting stops abusive crawlers. See Nginx FastCGI cache and rate limiting.

Connection leaks — app code opening connections without closing them, or persistent connections misconfigured. Use a connection pool with a fixed size in long-running apps (Node, Python, Java).

Long transactions and locks — a migration or report holding locks makes every other query wait. Run heavy jobs off-peak or on a replica.

Many clients, few queries — consider a proxy like ProxySQL to pool connections. See database connection pooling.

6. Monitor

Track Threads_connected, Threads_running and Max_used_connections over time (Netdata, Prometheus mysqld_exporter). Threads_running (actively executing) staying high is a stronger warning sign than many sleeping connections.

  • 1203 “User … already has more than ‘max_user_connections’ active connections” — a per-user limit (max_user_connections or the account’s MAX_USER_CONNECTIONS).
  • “Error establishing a database connection” in WordPress can be caused by this — see the WordPress fix.

Related: MySQL performance tuning.

  • MySQL
  • MariaDB
  • Troubleshooting