Skip to content

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

Key points

  • Count connections by user, host and state before raising limits.
  • Sleeping connections from oversized pools are the common culprit.
  • instances × pool size must fit under max_connections.
  • A proxy (ProxySQL, RDS Proxy) multiplexes many clients.

See who is connected

sql
SHOW VARIABLES LIKE 'max_connections';
SHOW STATUS LIKE 'Threads_connected';
SHOW STATUS LIKE 'Max_used_connections';

SELECT user, host, command, COUNT(*) AS n, MAX(time) AS max_idle_s
FROM information_schema.processlist
GROUP BY user, host, command
ORDER BY n DESC;

Fixes

FindingFix
Hundreds of Sleep connections from the appShrink pool sizes; set pool idle timeouts
Connections from old instancesLower wait_timeout so dead clients are cleaned up
Many instances × pool size > max_connectionsPooler or proxy; fewer, larger instances
Genuinely high concurrent queriesFaster queries first; then raise max_connections with memory in mind
Connection leak in codeRelease in finally blocks; enable pool leak detection

Raising maxconnections safely

Each connection uses memory for buffers (sort, join, read buffers and thread stack). Raising max_connections without checking memory can make the server swap or crash under load. Change it in the configuration file, and on managed services through the parameter group.

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.

Frequently asked questions

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.

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.