MySQL “Too many connections”: kill the idle ones, fix pooling
Find the idle connections with SHOW PROCESSLIST, kill the strays, then fix pooling.
Check SHOW FULL PROCESSLIST for connections stuck in Sleep and kill the strays. Fix the app to use a connection pool — only raise max_connections in my.cnf after the leak is fixed, since each connection costs server memory.
Try these fixes in order
List the connections
Look for rows in Sleep with high Time values; those are the slots being wasted.
mysql -u root -p -e "SHOW FULL PROCESSLIST;"Check the current limit
Check the current ceiling to see whether the load is legitimate or a leak.
mysql -u root -p -e "SHOW VARIABLES LIKE 'max_connections';"Kill idle offenders
Replace 123 with the idle connection id from the processlist. Repeat for the worst Sleep offenders.
mysql -u root -p -e "KILL 123;"Raise the limit if truly needed
Only after the leak is fixed, set a sane higher limit persistently.
mysql -u root -p -e "SET PERSIST max_connections = 300;"Interpret what you see
Many rows in Sleep state
What it suggests: Connections stuck in Sleep for a long time are leaked or idle-pooled connections holding slots.
Next check: List the processlist ordered by idle time and kill the worst offenders.
Legitimate traffic genuinely needs more
What it suggests: The default max_connections (151) is small for busy apps; the setting lives in the server config.
Next check: Check the current value before deciding whether a raise is warranted.
Connections grow with traffic and never shrink
What it suggests: Apps that connect per query without a pool eventually exhaust the limit under load.
Next check: Add connection pooling in the app; raising the limit only delays the failure.
Why this happens
- The server hit max_connections; idle or leaked connections are holding the slots.
- An application opens a new connection per request without pooling or closing.
Still not working?
Do not just crank max_connections to thousands; each connection costs memory and threads, and a leak will exhaust any limit. Find and fix the connections that never close first.
Official sources
These steps are based on the linked documentation and general diagnostics. BugMedic has not independently tested every product and system configuration. Review commands before running them.