1040ERRORTier 1 — Safe✅ HIGH confidenceToo many connections
🔴 Production Risk Error
HIGH — clients see connection failures; application may cascade-fail.
What this means
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.
Why it happens
- 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
How to reproduce
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()Fix 1: Temporarily raise max_connections
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 = 500Why 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 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 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.
What not to do
Set max_connections to 10 000+ without checking RAM
Why it's wrong: Each connection reserves memory for its thread stack; over-provisioning causes OOM kills.
Version notes
MariaDB 10.3+thread_pool_size can reduce context-switch overhead at high connection counts.Sources
📚 Official docs: https://mariadb.com/kb/en/server-system-variables/#max_connections
🔧 Source ref: MariaDB Server error code 1040 / ER_CON_COUNT_ERROR
📖 Further reading: MariaDB max_connections
📖 Further reading: MariaDB Thread Pool
Confidence assessment
✅ HIGH confidence
Stable. Connection limit behaviour is unchanged across all versions.
See also
🔗 Related errors
📄 Reference pages