Too many connections
Production Risk
HIGH — clients see connection failures; application may cascade-fail.
Error 1040 is returned when a new client tries to connect but the server has already reached its max_connections limit. Every thread slot is occupied and the server refuses new connections until an existing one is released.
- 1max_connections server variable is set too low for the workload
- 2Application is not releasing connections back to a pool (connection leak)
- 3A slow query storm is keeping threads open longer than normal
- 4Connection pooler (ProxySQL, PgBouncer) is misconfigured or absent
A new connection attempt when all thread slots are full.
-- From shell when server is saturated mysql -u root -p -- or in app: mysqli_connect() / PDO::__construct()
expected output
ERROR 1040 (08004): Too many connections
Fix 1
Temporarily raise max_connections
WHEN As an emergency measure while investigating the root cause.
SET GLOBAL max_connections = 500; -- Make it permanent in my.cnf / server.cnf: -- [mysqld] -- max_connections = 500
Why this works
Increases the thread-slot pool. Each additional connection costs ~1 MB of RAM, so raise proportionally to available memory.
Fix 2
Identify and close leaking connections
WHEN When connections are not being released after use.
SHOW STATUS LIKE 'Threads_connected'; SHOW PROCESSLIST; -- Kill long-running idle connections: KILL CONNECTION <id>;
Why this works
SHOW PROCESSLIST lists all active threads. Idle threads that have been open for a long time indicate a connection leak in the application.
Fix 3
Introduce a connection pool
WHEN When the application opens one connection per request.
-- No SQL change; configure application pooling. -- Example: PDO with persistent connections $pdo = new PDO($dsn, $user, $pass, [PDO::ATTR_PERSISTENT => true]);
Why this works
A connection pool reuses a fixed set of connections across many application requests, dramatically reducing peak thread count.
✕ Set max_connections to 10 000+ without checking RAM
Each connection reserves memory for its thread stack; over-provisioning causes OOM kills.
thread_pool_size can reduce context-switch overhead at high connection counts.
MariaDB Server error code 1040 / ER_CON_COUNT_ERROR
MariaDB max_connections ↗MariaDB Thread Pool ↗Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev