Your site is down, your app logs say ERROR 1040 (HY000): Too many connections, and the obvious fix is a restart. But a restart drops every session, interrupts transactions in flight, and on a busy WordPress site it only buys a few minutes before the pool fills again. You can fix MySQL too many connections without restarting the server at all. MySQL keeps one spare connection aside for an admin user, and from that session you can see what is connected, kill the sessions that are stuck, and raise the limit on the spot. This guide walks through that sequence on a typical VPS running MySQL 8.0 or MariaDB, with commands you can run right now.
What the “too many connections” error actually means
MySQL allows a fixed number of simultaneous sessions, controlled by the max_connections variable. The default is 151. Every session counts toward that number, including idle ones: a connection that finished its query and is just sitting there still occupies a slot. When a new connection arrives and the count is already at the limit, MySQL refuses it with error 1040.
That is why the error often appears at the worst moment. Traffic spikes, a backup job starts, or a plugin opens connections faster than it closes them, and the pool fills with sessions doing nothing. Restarting clears the pool, but it clears the evidence too. Before you touch anything, it helps to know the usual causes, and our troubleshooting guide for the same error on a VPS walks through them in detail.
Fix MySQL too many connections by clearing stuck sessions
First, get in. MySQL reserves one extra connection beyond max_connections for accounts with the CONNECTION_ADMIN privilege (or SUPER on older versions and MariaDB). That is the escape hatch: even when the pool is full, root can usually still log in.
mysql -u root -p
If root logs in through the Unix socket (sudo mysql on many Ubuntu and Debian installs), that works too. Once inside, look at what is actually connected:
SHOW PROCESSLIST;
The output shows every session with its user, host, database, command, and how long it has been in its current state. The pattern to look for is rows with Command: Sleep and a large Time value. Those are connections that finished their work and never closed.
To kill MySQL sleeping connections safely, target the old sleepers and leave everything else alone. Generate the kill statements first, so you can review them before running anything:
SELECT CONCAT('KILL ', id, ';')
FROM INFORMATION_SCHEMA.PROCESSLIST
WHERE COMMAND = 'Sleep' AND TIME > 300;
Run the resulting KILL statements for the idle sessions, a handful at a time, then check the count again with SHOW STATUS LIKE 'Threads_connected';. Two warnings matter here. Never kill threads owned by the system user; those are replication threads, and killing them can break replication. And do not kill sessions in Query state unless you know what the query is; killing a long-running ALTER TABLE or a backup mid-flight creates a bigger problem than the one you are solving.
Find out what is opening all those connections
Clearing sleepers gets the site back up, but if you stop there the pool fills again. Group the sessions by user and host to see where the load comes from:
SELECT USER, HOST, COUNT(*) AS n
FROM INFORMATION_SCHEMA.PROCESSLIST
GROUP BY USER, HOST
ORDER BY n DESC;
One application user with 120 of your 151 slots points at that app. A spread of hosts points at traffic or a misbehaving plugin. Two status values tell you how close to the edge you normally run:
SHOW STATUS LIKE 'Threads_connected';
SHOW STATUS LIKE 'Max_used_connections';
If Max_used_connections sits near the limit every day, your limit is too small for your real workload. If it spikes only during backups or a traffic burst, the cause is a specific event. Long-running queries also hold connections open while they run, so check the slow query log next. Our guide on the MySQL slow query log and how to find and fix slow queries shows which queries are worth fixing first.
Raise the limit live with the mysql max_connections setting
Once you have breathing room, raise the ceiling. The mysql max_connections setting can change at runtime, with no restart and no dropped sessions:
SHOW VARIABLES LIKE 'max_connections';
SET GLOBAL max_connections = 300;
New connections can now use the higher limit immediately. Existing sessions are untouched. Verify the change took effect:
SHOW VARIABLES LIKE 'max_connections';
One catch: SET GLOBAL lasts only until the next restart. To make the new limit permanent, add it under [mysqld] in your MySQL config file, usually /etc/mysql/my.cnf on Debian and Ubuntu or /etc/my.cnf.d/server.cnf on AlmaLinux and Rocky Linux:
[mysqld]
max_connections = 300
Pick a number that fits the server. Doubling from 151 to 300 is a reasonable first step on a small VPS. Jumping to 1000 is not; the next section explains why.
Stop the flood at the source: timeouts and app pools
Sleeping connections pile up because MySQL waits a long time before closing them. The wait_timeout variable defaults to 28800 seconds, which is 8 hours. A connection that an app forgot to close can sit in the pool for a full working day. Cut it down:
SHOW VARIABLES LIKE 'wait_timeout';
SET GLOBAL wait_timeout = 300;
Like max_connections, this takes effect for new sessions right away and needs the matching line in your config file to survive a restart. A 300-second timeout is safe for most web apps, which finish a request in seconds. If your app holds long-lived connections on purpose, check with the developer before lowering it, and set interactive_timeout to match so interactive clients behave the same way.
Then look at the application side. On a PHP-FPM setup, the worst case is roughly pm.max_children multiplied by the number of pools, because each child can hold a database connection. A WordPress site does not use persistent connections by default, but some plugins open a fresh connection per request and never close it, and a cron job that spawns workers can open dozens in a few seconds. Fix the app or the pool size, and the sleeping sessions stop appearing. For the queries that hold connections while they run, turn on slow query logging properly so the next offender leaves evidence: our guide on how to set up MySQL slow query logging on a VPS without slowing it down covers the settings that keep the overhead low.
When raising the limit is the wrong move
Every connection costs memory. Each one reserves thread stack space and can allocate per-connection buffers such as sort and join buffers; in MySQL 8.0 the defaults for sort_buffer_size and join_buffer_size are 256 KB each, and they add up across hundreds of sessions. A mysql connection limit increase on a 2 GB VPS can push the server into swapping or invite the OOM killer to take MySQL down entirely, which is a worse outage than error 1040.
So treat the higher limit as breathing room, not a cure. If Threads_connected climbs straight to the new ceiling again, the app is leaking connections or a query is too slow, and no limit will hold. Fix the leak, fix the query, or add a connection pooler such as ProxySQL between the app and the database. And if the server is genuinely too small for the workload, the honest answer is a bigger one: our guide on how to move a MySQL database to a bigger VPS without downtime covers a migration that keeps the site online while it happens.
A checklist for the next time it happens
- Log in as root through the reserved admin connection.
- Run
SHOW PROCESSLISTand kill sleeping sessions older than a few minutes. - Raise the ceiling with
SET GLOBAL max_connections = 300;and confirm it withSHOW VARIABLES. - Lower
wait_timeoutso idle connections close themselves. - Find the source: group sessions by user and host, and check the slow query log.
- Write the new values into the config file under
[mysqld]so they survive a restart.
Do those six steps in order and the site is back in minutes, with the cause identified instead of hidden behind a restart.