Database Administration — Sun Apr 05

← Home | ← database-admin

MySQL - DBA Toolkit - Connection Usage and Limits

Sun Apr 05 — Check current connection counts, max_connections limits, and identify who is consuming connections before you hit the wall.
#mysql #dba #monitoring #connections #performance #tool-kit

“Too Many Connections” Is Never a Surprise — Until It Is

MySQL has a hard ceiling on connections. When you hit it, new connection attempts are rejected immediately with:

ERROR 1040 (HY000): Too many connections

The problem is that by the time you see that error, you are already past the limit. The application is failing. The on-call phone is ringing.

This script lets you check where you are before that happens.

ConnectionUsageandLimits.sql
    Loading…
  

Reading the Results

Query 1 — Snapshot

ColumnWhat It Tells You
max_connectionsThe hard ceiling configured on this server
current_connectionsHow many connections are open right now
peak_connectionsThe highest concurrent count since the server started
pct_usedIf this is above 80%, review your connection pooling

If peak_connections is within 20% of max_connections, you have been close to the limit at some point.

Query 4 — Sleep Accumulation

Sleeping connections are not free — they still hold a slot against max_connections. Applications that do not close connections properly will accumulate hundreds of sleepers. A high longest_sleep_seconds means connections are being left open far too long.

Query 5 — Variables

VariableWhat It Controls
wait_timeoutHow long to keep an idle non-interactive connection
interactive_timeoutSame, for interactive sessions (MySQL clients)
thread_cache_sizeCached threads to reuse — reduces new connection overhead
max_user_connectionsPer-user connection cap (0 = no per-user limit)

What to Watch For

SignalWhat It Means
pct_used > 80%You are close — review connection pooling configuration
peak_connections near max_connectionsYou have hit close to the ceiling historically
Large sleeping connection countApplication not closing connections; reduce wait_timeout
One user dominating the connection listConnection pool misconfigured or app server count too high
max_connections very high (e.g. 5000+)Memory pressure risk — each connection uses ~1MB RAM

Gareth Winterman