What the Error Means
ERROR 1040 (HY000): Too many connectionsEach client connection takes one slot, and max_ sets how many there are. The default is 151 on current MySQL and MariaDB releases. MySQL keeps one extra slot for an account with the CONNECTION_ or SUPER privilege, so an administrator can often still log in with sudo mysql while the application is refused. MariaDB reserves one for SUPER or CONNECTION ADMIN in the same way; use sudo mariadb there.
Step 1: See Who Is Connected
SHOW FULL PROCESSLIST;Each row is one connection, with its user, host, database, state, and how many seconds it has been in that state. Group them to see where the connections come from:
SELECT user, SUBSTRING_INDEX(host, ':', 1) AS client, command, COUNT(*) AS n
FROM information_schema.processlist
GROUP BY user, client, command
ORDER BY n DESC;- Many rows in state Query with a high time: slow queries hold connections and new requests pile up behind them.
- Many rows in Sleep: connections that are open but idle, often from an application that does not close or reuse them.
- Many rows waiting for a lock: one long transaction blocks the others.
Step 2: Compare the Limit with Peak Use
SHOW VARIABLES LIKE 'max_connections';
SHOW GLOBAL STATUS LIKE 'Max_used_connections';
SHOW GLOBAL STATUS LIKE 'Threads_connected';Max_ is the highest number reached since the server started. If it equals the limit, the server has been full at least once. Threads_ is the number right now.
Step 3: Fix the Real Cause
- Slow queries: enable the slow query log (
slow_andquery_ log long_), find the queries, and add the missing indexes. A query that takes 10 seconds instead of 10 milliseconds holds its connection a thousand times longer.query_ time - Too many application workers: each worker can hold a connection. A PHP-FPM pool with a high
pm.max_on several sites adds up quickly; keep the total below the limit.children - No pooling: applications that open a connection per request and forget to close it leak slots. Use the framework's connection pool and set its maximum size.
- Sleeping connections:
wait_closes idle connections after 28800 seconds (8 hours) by default. A lower value, such as 300, releases leaked slots sooner; test that the application reconnects cleanly.timeout
End a Stuck Connection
KILL 1234;
KILL QUERY 1234;Use the ID from the process list. KILL QUERY stops only the running statement and keeps the connection; prefer it. Killing a connection in the middle of a large write rolls the transaction back, which can take a while.
Why Raising the Limit Can Hurt
Every connection uses memory for its own buffers, on top of the shared InnoDB buffer pool. A limit far above what the memory can serve turns a connection error into swapping or an out-of-memory kill of the whole server, as described in OOM killer. Check memory use under load first, with the tools in how to check memory usage in Linux, and raise the limit in steps.
Step 4: Change the Limit
At runtime, effective at once and lost at the next restart:
SET GLOBAL max_connections = 300;To keep it, add it under [mysqld] in the server configuration: /etc/mysql/mysql.conf.d/mysqld.cnf for MySQL on Ubuntu, or /etc/mysql/mariadb.conf.d/50-server.cnf for MariaDB on Debian and Ubuntu:
[mysqld]
max_connections = 300On MySQL 8.0 and later, SET PERSIST max_ does both in one step and stores the value in mysqld-auto.cnf in the data directory. MariaDB has no SET PERSIST; use the configuration file.
Check Again
Watch Threads_ during the next busy period. If it climbs steadily without load, connections are leaking, and the application, not the limit, needs the fix. If the application then reports a login error instead, see MySQL access denied for user.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations