MySQL error 1040: Too many connections
The server already has max_connections client connections open. Close idle ones, size connection pools properly, or raise the limit.
- Error number
1040- Name
ER_CON_COUNT_ERROR- SQLSTATE
08004- Category
- Server limits and settings
What it means
MySQL refuses a new connection once max_connections clients are connected. It keeps one extra slot for an account with the CONNECTION_ADMIN (or SUPER) privilege, so an administrator can still get in to see what's happening.
Common causes
- Application connection pools that together open more connections than the server allows, for example many app instances each with a large pool.
- Connections leaked by code that never closes them.
- Slow queries or lock waits that keep connections busy until new requests pile up.
- A low
max_connectionsfor the workload (the default is 151).
How to fix it
-
See how many connections there are and who holds them.
SHOW STATUS WHERE Variable_name IN ('Threads_connected', 'Max_used_connections'); SELECT user, host, db, command, time FROM information_schema.processlist ORDER BY time DESC; -
Kill idle sessions that are stuck, then fix whatever leaks them.
KILL 12345; -- the Id from the process list
-
Raise the limit if the server has memory to spare, and persist it.
SET PERSIST max_connections = 500;
-
Cap pool sizes so that pool size times the number of app instances stays below
max_connections.
Related errors
1203User has more than max_user_connections1205Lock wait timeout exceeded701SQL Server: Insufficient memory in resource poolORA-00020Oracle: maximum number of processes exceededORA-00018Oracle: maximum number of sessions exceeded53300PostgreSQL: too many connections
More server limits and settings errors
1153Got a packet bigger than max_allowed_packet1175Safe update mode blocked the statement1290Server option prevents this statement1114The table is full
The code and message text are from the MySQL 8.4 Error Message Reference. Browse the full MySQL error list or search every database error.
Work on MySQL in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for MySQL and the other databases you use.