mysql: Too many connections: max_connections tuning
Fixes MySQL 'Too many connections' by finding connection leaks, tuning max_connections safely, and adding pooling. It covers the memory cost of raising the limit. Not for authentication errors.
TL;DR
The server hit max_connections: find the leak (sleeping connections piling up) with SHOW PROCESSLIST, fix the app to close or pool connections, and only then raise max_connections (each connection costs RAM). The error stops once usage fits the limit.
Error
mysql: Too many connectionsSteps
- Inspect the connection list: run
SHOW FULL PROCESSLISTand count by state and user. Expected: you see whether connections are sleeping (leak) or actively working (genuine load). - Find the leak: sleeping connections from one app host point at missing close calls or no pooling. Expected: the offending client identified.
- Fix the client: add connection pooling (or fix the close logic) so connections are reused. Expected: the sleeping pileup stops growing.
- Tune the server: raise
max_connectionsmodestly, accounting for per-connection memory, and consider loweringwait_timeoutto reap idle ones faster. Expected: headroom for legitimate peaks. - Add a proxy (like ProxySQL) if connection counts keep growing with traffic. Expected: the database sees a bounded pool regardless of app instances.
When to use
- MySQL refuses connections with "Too many connections"
- After adding app instances or workers
- Auditing connection hygiene
When not to use
- Access denied for user (authentication)
- The server is up but slow (performance tuning)
Tool compatibility
- MySQL
max_connections,wait_timeout,SHOW PROCESSLIST; connection poolers
Variant phrasings
mysql sleeping connections pile up
The app is not closing them; add pooling or fix the close path.
raise max_connections safely
Budget RAM per connection and lower wait_timeout; the limit is not free.
Why it happens
Each MySQL connection consumes server memory and a thread, so the server caps them. Applications that open a connection per request without pooling exhaust the cap under load, and the fix is architectural (pooling) more than numerical (a bigger cap).
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_GVOWZISLxc6E41-8ej9LOQ