What the Error Means
ERROR 1040 (HY000): Too many connections
Each client connection takes one slot, and max_connections 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_ADMIN 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_used_connections is the highest number reached since the server started. If it equals the limit, the server has been full at least once. Threads_connected is the number right now.
Step 3: Fix the Real Cause
- Slow queries: enable the slow query log (
slow_query_logandlong_query_time), 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. - Too many application workers: each worker can hold a connection. A PHP-FPM pool with a high
pm.max_childrenon several sites adds up quickly; keep the total below the limit. - 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_timeoutcloses 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.
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 = 300
On MySQL 8.0 and later, SET PERSIST max_connections = 300; 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_connected 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.