Error explained
MySQL: Too many connections
MySQL refused a new connection because max_connections was reached. Usually many idle connections from application pools are the reason, not real load.
3 min read9 sectionsUpdated
On this page
| Finding | Fix |
|---|---|
| Hundreds of Sleep connections from the app | Shrink pool sizes; set pool idle timeouts |
| Connections from old instances | Lower wait_timeout so dead clients are cleaned up |
| Many instances × pool size > max_connections | Pooler or proxy; fewer, larger instances |
| Genuinely high concurrent queries | Faster queries first; then raise max_connections with memory in mind |
| Connection leak in code | Release in finally blocks; enable pool leak detection |
Catching it in production
Alert on Threads_connected above 80% of max_connections, and watch application pool pending counts. A sudden jump after a deploy usually means a new code path leaks connections.
VitalRix error tracking groups exceptions recorded on OpenTelemetry spans into issues with counts, first and last seen, affected endpoints and versions, and marks an issue as regressed if it comes back after being resolved. Alert rules on error rate per service or endpoint route the problem to the right team before customers report it.
A worked example
Twelve application pods each run a pool of 25 connections: up to 300 connections, against max_connections = 151 (the default). At normal load each pod uses 6, so nothing happens; during a traffic spike pools grow and new connections fail with 1040. Shrinking pools to 10 per pod (120 total) and adding a short idle timeout fixed it without touching the database, because the queries were fast enough that 10 per pod was plenty.
Is there a reserved connection for admins?
MySQL allows one extra connection for a user with the CONNECTION_ADMIN (or SUPER) privilege, so an administrator can still log in to investigate.
What is a good wait_timeout?
Lower than the default 8 hours for most web apps, for example a few minutes, as long as your pools validate connections before use.
Related reading
- PostgreSQL "too many clients already": how to fix it: Fix PostgreSQL FATAL: sorry, too many clients already. See who holds connections, stop leaks, add PgBouncer and size max_connections the right way.
- 429 Too Many Requests: what it means and how to fix it: 429 Too Many Requests explained: rate limits, the Retry-After header, backoff with jitter for clients, and how to set fair rate limits on your own API.
- Nginx "worker_connections are not enough": how to fix it: Fix Nginx worker_connections are not enough: why proxying uses two connections per client, how to size limits, raise descriptors and add upstream keepalive.
- Too many TIME_WAIT connections: what to do: Understand Linux TIME_WAIT sockets, when they cause ephemeral port exhaustion, why keep-alive is the real fix and which kernel settings not to touch.
- WordPress "Error establishing a database connection": fix: Fix WordPress Error establishing a database connection: check wp-config credentials, MySQL status, connection limits and damaged tables with safe commands.
- 413 Request Entity Too Large: how to raise the limit safely: Fix 413 Request Entity Too Large in Nginx, Apache, Node.js, PHP and Kubernetes ingress: find which layer sets the limit and raise it safely for uploads.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.