What the Too Many Connections Error Means
MySQL rejects a new connection with a Too many connections error when the number of active sessions reaches the max_connections limit. The server reserves one extra connection for a user with the SUPER privilege, so an administrator can usually log in and investigate even when regular clients are already being refused.
The error rarely means the site truly needs more connections: most often it is a symptom of a leak — the application opens connections faster than it closes them.
How to Check the Current Limit and Load
Before changing the limit, it helps to understand how many connections are actually open and who is holding them.
SHOW VARIABLES LIKE 'max_connections';
SHOW STATUS LIKE 'Threads_connected';
SHOW STATUS LIKE 'Max_used_connections';
SHOW PROCESSLIST;The Time column in SHOW PROCESSLIST shows how many seconds a session has been in its current state. If many rows show state Sleep with Time in the hundreds of seconds, that is almost always a connection leak on the application side.
Key Connection Limit Parameters
| Parameter | Typical Value | What It Controls |
|---|---|---|
| max_connections | 151-500 | maximum simultaneous connections to the server |
| wait_timeout | 28800 | seconds of idle time before the server closes an inactive session |
| interactive_timeout | 28800 | the same setting for interactive clients, such as the mysql CLI |
Lowering wait_timeout to 60-120 seconds for web workloads helps close stuck connections faster instead of waiting the standard eight hours. Tuning the remaining parameters is covered in the article on my.cnf configuration.
Where Connection Leaks Come From in Applications
- A script opens a database connection and exits with an error before calling close — the connection hangs until wait_timeout expires.
- A connection pool is configured with a larger size than the application actually needs and keeps extra sessions open as a reserve.
- A long SELECT without LIMIT blocks a thread for minutes, and new requests open new connections on top of the old ones.
- Several copies of the application (workers, cron jobs) keep their own pools, and together they exceed the server's limit.
How to Configure a Connection Pool Correctly
A connection pool solves the problem only when its size is matched to the server's limit, not picked at random.
# Example for an application with several workers:
# 4 workers x pool_size 20 = 80 connections
# max_connections on the server should be noticeably higher than 80
max_connections = 200The rule is simple: the total size of all pools across all application workers must be lower than max_connections, with room to spare for service connections — monitoring, backups, and manual admin sessions.
Emergency Measures When the Limit Is Exceeded
If the Too many connections error is already happening in production, act quickly and in order.
- Log in with an account that has the SUPER privilege — it has a reserved connection slot.
- Use SHOW PROCESSLIST to find sessions with the largest Time in state Sleep and end them with KILL.
- Temporarily raise max_connections with SET GLOBAL without waiting for a server restart.
- Check the slow query log to find the query holding connections the longest.
SET GLOBAL max_connections = 300;
KILL 12345;Summary: Too Many Connections Checklist
- Check the current max_connections and the actual connection count via Threads_connected.
- Find stuck sessions in SHOW PROCESSLIST by a large Time value.
- Lower wait_timeout for web workloads if sessions pile up in Sleep.
- Match the total size of application pools to the server's limit.
- When scaling read load, consider ProxySQL instead of endlessly raising max_connections.