Troubleshooting: MySQL Too Many Connections Error on a VPS
Your site loads fine one minute and shows a database error the next. The mysql too many connections error means the server has reached its connection limit, so it refuses new sessions until an old one closes. It looks like a MySQL problem, but on a small VPS the cause is often an app that opens connections and never gives them back, a traffic spike, or a setting that no longer fits the workload.
Find out which of those three it is before you change anything. Raising the limit without checking can trade a clear error for a server that runs out of RAM. This guide walks through the checks in order, from confirming the error to fixing the app, then tuning the server only as far as your memory allows.
What the MySQL too many connections error actually means
MySQL allows a fixed number of simultaneous client connections. That limit is max_connections, and the default on many installs is 151. Each connection uses memory for its own buffers, so the limit protects the server from running out of RAM. When the count hits the limit, MySQL returns error 1040, and the app that tried to connect shows the “too many connections” message.
One extra connection is reserved for a user with the SUPER or CONNECTION_ADMIN privilege, so you can still log in as root while normal users are locked out. Use that first. Do not restart MySQL first. A restart clears the evidence, and the same connections will pile up again if the cause is still there.
The error can also appear when MySQL is healthy but one app hogs the pool. A plugin that opens a fresh connection per request or a job that loops without closing its handle can fill the limit on its own. That is why the fix starts with diagnosis, not with a bigger number.
Confirm the error and check how close you are to the limit
Log in as root and check the current state. SHOW STATUS tells you the peak your server has reached since it started, while SHOW VARIABLES tells you the limit in force right now.
mysql -u root -p -e "SHOW VARIABLES LIKE 'max_connections'; SHOW STATUS LIKE 'Max_used_connections'; SHOW STATUS LIKE 'Threads_connected';"
If Max_used_connections is close to max_connections, the limit is the bottleneck. If it is far below the limit but users still see the error, check whether the error comes from a different database host, a stale config file, or a proxy in front of MySQL. Also check that MySQL is the service actually failing, not PHP-FPM or Nginx. The diagnosing a slow VPS walkthrough shows how to tell a database problem apart from CPU or disk pressure.
Check the error log too, usually /var/log/mysql/error.log on Debian and Ubuntu. A run of aborted-connection warnings points at clients that drop without a clean close, a different problem from a pool that simply fills up.
Use SHOW PROCESSLIST to see what is holding connections open
Run SHOW FULL PROCESSLIST; as root. It lists every live connection, the user and host behind it, how long it has been idle or running, and the query it is running. A show processlist mysql check like this takes seconds and usually names the culprit.
Look for three patterns. First, many connections in the Sleep state from the same user and host: idle connections the app opened and never closed. Second, a few connections stuck in a long-running state such as Sending data or Copying to tmp table, which points at slow queries. Third, connections from an unexpected host, which can mean a forgotten script or a staging copy pointing at production.
Count connections by user to make the pattern obvious:
SELECT USER, HOST, COUNT(*) AS connections FROM INFORMATION_SCHEMA.PROCESSLIST GROUP BY USER, HOST ORDER BY connections DESC;
If one application user owns most of the connections, the fix belongs in that app. If the connections spread evenly and most are active, the server may genuinely need a higher limit, or the workload may need caching so fewer requests reach the database at all.
Fix the application side first
Most recurring cases are a mysql connection leak, not a limit set too low. A leak happens when code opens a connection, finishes its work, and never calls close, or when a connection pool grows but never shrinks. PHP is usually safe here because each request ends and its connections close, but long-running workers, cron jobs, and queue consumers are not. Check any script that runs in a loop and holds a database handle between iterations.
For WordPress, the usual suspects are a plugin that runs a heavy query on every page load, a cron job that fires too often, or a PHP-FPM pool sized for traffic the VPS cannot handle. Each PHP-FPM child can hold a database connection while it serves a request, so a pool of 50 children can open 50 connections at once. The guide on how to tune PHP-FPM pool settings for a 2 GB VPS explains how to size the pool against the memory you actually have.
Check these items before you touch MySQL settings: close database connections at the end of long-running scripts and test them under load, set a sensible pool size in your app instead of leaving it at the default, and look for slow queries in the slow query log. One query that takes 30 seconds holds its connection for 30 seconds, and a handful of those can fill the pool during a traffic spike. A Redis object cache for WordPress can cut database hits per page load, which lowers the peak connection count as well.
If the list shows mostly idle Sleep connections from your own app, the app is the problem. Raising the limit only buys time until the leak fills the new limit too.
Raise mysql max_connections only as far as RAM allows
When the app side is clean and the server still hits the limit during real peaks, raising the limit is reasonable. The setting is max_connections in the [mysqld] section of your MySQL config file, usually /etc/mysql/my.cnf or a file under /etc/mysql/mysql.conf.d/. Edit it, then restart MySQL so the change takes effect.
[mysqld]
max_connections = 200
You can also test a change without a restart by running SET GLOBAL max_connections = 200; as root. That applies until the next restart, so write it into the config file as well, or the fix will vanish the next time the service restarts.
Do not raise the number in large jumps. Each connection reserves memory for per-connection buffers such as sort_buffer_size and join_buffer_size. Those buffers are allocated per connection, not shared, so a high limit combined with large buffers can exhaust RAM fast. The MySQL 8.4 tuning guide for a 2 GB VPS shows how to balance the buffer pool against connection memory on a small server, and the MariaDB tuning guide for a low-memory VPS covers the same trade-off if you run MariaDB instead.
Raise the limit in small steps, watch Max_used_connections over a full day of traffic, and stop when the peak sits comfortably below the limit. If the peak keeps climbing after each raise, the app still leaks, and a bigger number is only hiding it.
Lower wait_timeout so idle connections do not pile up
MySQL closes an idle connection after wait_timeout seconds. The default is 8 hours, which is far too long for a web app that opens short-lived connections. An idle connection that sits for hours still counts against the limit, so a burst of traffic can leave dozens of Sleep connections behind long after the visitors have gone.
For a typical WordPress or PHP site, a wait_timeout mysql value of 60 to 300 seconds is a sensible starting point. Interactive sessions from the MySQL client use interactive_timeout instead, so set that one too if you work in the client for long sessions.
[mysqld]
wait_timeout = 180
interactive_timeout = 180
Test it with SET GLOBAL wait_timeout = 180; first, then watch the process list over the next hour. Do not set the timeout to a few seconds: an app that reuses a pooled connection may find it closed between requests and log errors of its own. The timeout is a safety valve, not a substitute for closing connections properly in the app.
Prevent it from coming back
Once the site is stable, check Max_used_connections weekly, or set up an alert that warns you when the count passes a threshold you choose. Keep an eye on the slow query log after plugin updates, and review the PHP-FPM pool size whenever traffic grows.
The routine is short: confirm the error, list the connections, find the owner of the pile-up, fix the app or the slow query, and only then tune the limit. That order keeps the server inside its memory budget and stops the same error from returning the next time traffic peaks.