1040
MySQLERRORCommonConnectionHIGH confidence

Too many connections

Production Risk

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
  1. 1max_connections server variable is set too low for the workload
  2. 2Application is not releasing connections back to a pool (connection leak)
  3. 3A slow query storm is keeping threads open longer than normal
  4. 4Connection pooler (ProxySQL, PgBouncer) is misconfigured or absent
How to reproduce

A new connection attempt when all thread slots are full.

trigger — this will error
trigger — this will error
-- 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.

Temporarily raise max_connections
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.

Identify and close leaking connections
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.

Introduce a connection pool
-- 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

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 documentation ↗

MariaDB Server error code 1040 / ER_CON_COUNT_ERROR

MariaDB max_connectionsMariaDB Thread Pool

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MySQL errors