Guides

MySQL "Too many connections": causes and fixes

In short

The error means every connection slot allowed by max_connections is taken. Run SHOW PROCESSLIST to see who holds them and compare Threads_connected with Max_used_connections to tell a leak from a burst. Raising max_connections only helps once you know which of the two you have.

The symptom

Applications fail with ERROR 1040 (HY000): Too many connections, or time out opening a connection. MySQL keeps one extra slot for an account with the CONNECTION_ADMIN privilege, so you can usually still log in as an administrator to look.

Diagnose it in two commands

The first shows the limit and the current use, the second who holds the connections:

SHOW VARIABLES LIKE 'max_connections';
SHOW STATUS LIKE 'Threads_connected';
SHOW STATUS LIKE 'Max_used_connections';
SHOW PROCESSLIST;
max_connections       151
Threads_connected     3
Max_used_connections  7

Id    User         Host                db    Command  Time   State
5     event_scheduler localhost         NULL  Daemon   9216   Waiting on empty queue
1584  wt15_monitor 172.16.1.55:38952    NULL  Sleep    13
3652  root         localhost            NULL  Query    0      init

Max_used_connections is the high-water mark since the server started. Read the Command and Time columns of the process list: many connections in Sleep with a large Time are idle connections that were never closed, and many in Query with a large Time are slow statements holding connections open.

Causes and how to tell them apart

Cause How to tell Fix
A connection leak in the application Threads_connected climbs steadily and many connections sit in Sleep with a large Time, from the same Host Close connections in the application, and set wait_timeout so the server drops idle ones
A burst of traffic Max_used_connections reaches max_connections only at peaks, and the connections are mostly in Query Queue or limit work in the application, add a pool, or raise max_connections with memory in mind
Pools sized too large Total pool size across all app instances exceeds max_connections Size each pool so that instances times pool size stays below the limit, with headroom
Slow statements holding connections Many connections in Query with a large Time and the same statement Find and fix the statement: the connections are a symptom

Fix it

Fix the cause first. Then size the limit: add up the largest pool size of every application instance, plus the connections of monitoring, backups and administrators, and set max_connections above that. Each connection uses memory, so raise it by what the host can hold, not just to make the error go away.

SET GLOBAL max_connections = 300;

Put the same value in the server’s configuration file so it survives a restart. To drop connections that have sat idle, lower wait_timeout the same way.

Stop it coming back

Alert on connections as a percentage of max_connections, well before they run out. Size pools from the limit, not the other way round, and re-check after each deploy that adds application instances.

What WatchTower15 shows

WatchTower15 collects the connections against max_connections and the threads running for MySQL and MariaDB, and raises the database alarm Database connections high, read as a percentage of max_connections. It never reads your data and opens no connection into your network: the agent collects from inside your own network. See MySQL and MariaDB for what is collected and Alarms & alerting for the database alarms.

Sources