MySQL

Diagnosing MySQL Connection Saturation: Too Many Connections Fix

Systematically diagnose and fix MySQL 'Too many connections' errors. Learn to identify connection leaks, tune wait_timeout, and implement ProxySQL pooling.

JusDB Team
February 7, 2025
5 min read
152 views

When MySQL hits max_connections, new connections receive Too many connections errors and your application grinds to a halt. Here is how to diagnose and fix connection saturation.

Symptoms

  • ERROR 1040 (HY000): Too many connections
  • Application timeouts spiking
  • MySQL thread count pegged at max_connections

Immediate Diagnosis

sql
-- How many connections are open right now?
SHOW STATUS LIKE 'Threads_connected';

-- What is the configured limit?
SHOW VARIABLES LIKE 'max_connections';

-- Who is using connections?
SELECT user, host, db, command, time, state
FROM information_schema.processlist
ORDER BY time DESC
LIMIT 50;

Find Connection Hogs

sql
-- Count by user and host
SELECT user, host, count(*) AS cnt
FROM information_schema.processlist
GROUP BY user, host
ORDER BY cnt DESC;

-- Long-running sleeping connections (connection leaks)
SELECT id, user, host, time
FROM information_schema.processlist
WHERE command = 'Sleep' AND time > 300
ORDER BY time DESC;
Note: Long-sleeping connections are usually connection leaks in the application. Check that your ORM or connection pool is properly releasing connections.

Tuning max_connections

sql
-- Temporarily raise limit (requires SUPER)
SET GLOBAL max_connections = 500;

-- Check peak ever reached
SHOW STATUS LIKE 'Max_used_connections';
Warning: Each MySQL connection consumes ~1 MB RAM. Setting max_connections = 2000 on a 4 GB server will cause OOM kills. Use a connection pooler (ProxySQL, PgBouncer) instead of simply raising the limit.

wait_timeout and interactive_timeout

sql
-- Reduce idle connection lifetime
SET GLOBAL wait_timeout = 60;
SET GLOBAL interactive_timeout = 60;

Long-Term Fix: ProxySQL Connection Pooling

The real fix for connection saturation is a connection pooler in front of MySQL. ProxySQL multiplexes thousands of app connections onto a smaller pool of MySQL connections, keeping Threads_connected low regardless of application concurrency.

Key Takeaways

  • Check information_schema.processlist immediately when connections spike
  • Long-sleeping connections signal application connection leaks
  • Lower wait_timeout to reclaim idle connections faster
  • Deploy ProxySQL as the permanent fix — don't just raise max_connections

JusDB Can Help

Connection saturation incidents require both immediate triage and long-term architecture changes. JusDB engineers can diagnose your connection patterns and right-size your pooling layer.

Share this article

JusDB Team

Official JusDB content team