mysql error: Too many connections - how to find whats leaking connections
Fixes MySQL 'Too many connections' by finding what is leaking connections via the processlist and performance_schema. Use when new connections are refused. Key trigger: the exact 'Too many connections' error.
mysql error: Too many connections - how to find whats leaking connections
TL;DR
The server hit max_connections and is refusing new ones. Something is opening connections and not closing them. List the processlist grouped by user and host, find the idle pile, then fix the leaky client: pool it, time it out, or close it.
ERROR 1040 (HY000): Too many connectionsSteps
Get a connection census while it is happening: SELECT user, host, COUNT(*), SUM(command='Sleep') AS sleeping FROM information_schema.processlist GROUP BY user, host. Expected: One user/host pair owns most of the connections, mostly in Sleep.
Check how long they have been idle: SELECT id, user, host, db, time, state FROM information_schema.processlist WHERE command='Sleep' ORDER BY time DESC LIMIT 20. Expected: You see connections idle for minutes or hours.
Trace the idle pile to a client: match the host to an app server, then check that app's connection handling for missing close calls or an unbounded pool. Expected: You find the service that never releases connections.
Free capacity now: KILL the oldest idle connections, or raise max_connections slightly as a bridge (it only buys time). Expected: New connections succeed again.
Fix the leak: use a real pool with max size and idle timeout, set waittimeout lower (e.g. 300s) so the server reaps orphans, and add a dashboard alert on Threadsconnected. Expected: Connection count stays flat under load instead of climbing forever.
When to use
- the error is ERROR 1040 too many connections
- Threads_connected climbs until it hits the ceiling
- most connections sit in Sleep state
When not to use
- connections are refused but Threadsconnected is low (check maxconnect_errors or the OS file limit)
- the error is 'Host is blocked' (that is the host cache, not the connection count)
- a single query eats all memory (that is a workload problem, not a leak)
Tool compatibility
- MySQL 5.7 and 8.0, MariaDB 10.5+
- no special instrumentation needed
Variant phrasings
- "mysql 1040 too many connections find leak"
- "mysql connections keep growing who is leaking"
- "Threads_connected maxed out"
Why it happens
Every connection costs memory and a thread. Application code that opens connections without closing them, pools with no max size, or long-lived idle sessions accumulate until max_connections is reached. The server then refuses everyone, including your admin client if you did not reserve a slot.
Edge cases
- leave one admin slot: the SUPER privilege bypasses max_connections by +1, so keep a privileged user for exactly this moment
- raising max_connections increases memory per connection; on small instances it can OOM the server
- proxies like ProxySQL multiplexing can hide the real client behind one host; check the proxy's own stats
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_VJxZQJ2-ehWMS92pRbAtuA