Fix "Too many connections" database errors
ERROR 1040: Too many connections (MySQL) or remaining connection slots are reserved (PostgreSQL) means your app opened more database connections than the
server allows. Raising the limit is the last fix — first stop leaking
connections.
Why it happens
A database allows a fixed number of simultaneous connections. You hit the ceiling when:
- Connections aren’t released — the app opens a new one per request and never closes it (the #1 cause).
- No connection pooling — every request/worker makes its own direct connection.
- Too many app workers/processes each holding connections.
- A traffic spike genuinely needs more than the limit allows.
Diagnose
MySQL:
SHOW STATUS LIKE 'Threads_connected';SHOW PROCESSLIST; -- who's connected, and are they idle?PostgreSQL:
SELECT count(*) FROM pg_stat_activity;SELECT state, count(*) FROM pg_stat_activity GROUP BY state;Lots of idle connections = a leak or missing pooling.
Fix it properly (in order)
- Use a connection pool. Almost every framework supports one — a small pool (e.g. 5–10) reused across requests beats one-per-request. This fixes most cases.
- Close connections you open manually, including on error paths.
- Right-size your workers.
workers × pool-sizemust stay under the DB limit. Twenty workers each with a pool of 10 = 200 connections — plan for it. - Then, if genuinely needed, raise
max_connections— but more connections use more memory, so pooling is almost always the better lever.
Related
- A connection spike often rides along with high CPU/memory.
- Reduce total queries with Redis caching.
Running OrbixPanel? Local databases keep latency low so pooled connections stay snappy. OrbixPanel.com → — the free, self-hosted, AI-powered hosting control panel.