ERROR 1040 (HY000): Too many connections means MySQL or MariaDB has reached max_connections and refuses new clients, so websites show database connection errors. Raising the limit is sometimes right, but usually connections pile up because queries are slow or apps do not close them.
Before you start
- Root access to the server. MySQL keeps one extra connection for a user with the
SUPER/CONNECTION_ADMINprivilege, sosudo mysqlusually still works when the limit is hit.
See current usage
SHOW VARIABLES LIKE 'max_connections';
SHOW GLOBAL STATUS LIKE 'Threads_connected';
SHOW GLOBAL STATUS LIKE 'Max_used_connections';Max_used_connections close to max_connections confirms the limit is being hit.
Find who holds the connections
SHOW FULL PROCESSLIST;Group by user and state for a clearer picture:
SELECT user, host, command, state, COUNT(*) AS n FROM information_schema.processlist GROUP BY user, host, command, state ORDER BY n DESC;| What you see | Likely cause |
|---|---|
Many Sleep connections from one app | Connections not closed or persistent connections from too many PHP workers |
Many queries in Waiting for table metadata lock or Locked | A long query or ALTER blocks the rest |
Many identical slow Query rows | Missing index or traffic spike |
| Connections from unknown hosts | Exposed database port; restrict it |
Immediate relief
Kill a stuck long-running query by its Id:
KILL 12345;Raise the limit temporarily (resets on restart):
SET GLOBAL max_connections = 300;Long-term fixes
- Speed up slow queries: enable the slow query log and add indexes; fast queries release connections sooner.
- Close idle connections: lower
wait_timeout(e.g. 300 seconds) so abandonedSleepconnections are dropped. - Match PHP workers: each PHP-FPM worker may hold a connection;
pm.max_childrenacross all pools should fit withinmax_connections. Avoid persistent connections (mysqli.allow_persistent, PDO persistent) unless you size for them. - Use connection pools in Node.js and Python apps with a sensible pool size, not a new connection per request.
- Cache: an object cache (Redis) and page cache cut database traffic dramatically.
- Block bots: aggressive crawlers can multiply load; rate-limit them.
Set max_connections permanently
In the [mysqld] section of your config:
max_connections = 200
wait_timeout = 300Each connection can use several MB for per-thread buffers. Make sure buffer pool plus max_connections × per-connection memory fits in RAM, or the OOM killer may take MySQL down instead. Restart MySQL to apply.
Check the result
Monitor Threads_connected and Max_used_connections over a busy day; the peak should stay well below the limit.
Troubleshooting
- Cannot even connect as root: use
sudo mysqlon the socket; if that fails, restart MySQL during a quiet moment. - Errors return right after raising the limit: the root cause (slow queries, leaks) is still there.
- Host is blocked because of many connection errors (error 1129): a different issue; run
FLUSH HOSTS;(ormysqladmin flush-hosts) and raisemax_connect_errors.
MySQL connections FAQ
What is a good max_connections value?
Enough for your PHP workers and apps with headroom, typically 100–300 on a VPS. Thousands are rarely needed without a pooler.
Does this happen on cPanel hosting?
On shared servers each account has a per-user connection limit; errors there mean your site opens too many at once. Caching usually solves it.
Should I restart MySQL?
It clears connections but also the buffer pool cache; fix the cause instead when possible.
