
Your WordPress site suddenly shows database errors and the logs say ERROR 1040 (HY000): Too many connections. A MySQL error 1040 too many connections fix is urgent — every new connection is being rejected until the pressure drops. This 2026 guide explains why the connection pool fills up and how to recover immediately, then fix it properly on shared hosting and VPS.
Here is what is happening. MySQL allows only max_connections simultaneous client connections (default 151; shared hosts often set 25–50). Each PHP request that talks to the database holds one connection. When traffic spikes, a plugin runs slow queries, or connections leak without closing, the pool fills up — and every further connection attempt fails with error 1040. WordPress then shows “Error establishing a database connection” to visitors.
What Causes MySQL Error 1040
- Traffic spikes: more concurrent PHP requests than the connection limit allows.
- Slow or unoptimised queries: long-running queries hold connections open far longer than necessary.
- Connection leaks: application code opening connections without closing them.
- Sleeping connections: idle connections stuck in Sleep state, occupying slots.
- Undersized max_connections: the limit is simply too low for the site’s real traffic.
- Shared hosting neighbours: on shared MySQL, other accounts can consume the shared pool.
Immediate Recovery: Get the Site Back Online
Step 1: Restart MySQL (fastest relief)
On a VPS, run sudo systemctl restart mysql. This drops every connection instantly and buys you breathing room. On shared hosting, ask support to restart the MySQL instance or kill your sleeping processes.
Step 2: Kill sleeping connections
If you have admin access, run SHOW PROCESSLIST;, note the IDs stuck in Sleep, and kill them with KILL <id>;. This frees slots without a full restart.

Step 3: Raise the limit temporarily
As root, run SET GLOBAL max_connections = 300; — no restart needed, takes effect immediately. This is temporary (resets on restart) but stabilises the site while you fix the root cause. Make sure the server has enough RAM first.
Step 4: Enable caching to cut database load
Turn on full-page caching and, if available, object caching (Redis/Memcached). Cached pages skip the database entirely, which slashes concurrent connections during traffic spikes. See our WordPress critical error guide for related recovery steps.
Fixing Error 1040 Properly
Step 5: Make the higher limit permanent
In my.cnf under [mysqld], set max_connections = 300 (sized to your RAM — roughly: max_connections = (available RAM − global buffers) / per-thread buffers), plus wait_timeout = 60 so idle connections die faster. Restart MySQL.
Step 6: Find and fix the query hogs
Raising the limit without fixing slow queries just postpones the crash. Enable the MySQL slow query log, and on WordPress install Query Monitor to find plugins running heavy or repeated queries. Optimise or replace the offenders — related-posts plugins and uncached widgets are classic culprits.
Step 7: Fix connection leaks in custom code
Every script must close what it opens: $mysqli->close(); at the end, or use persistent-connection pooling properly. Audit custom themes and plugins for connections opened in loops or never released.
How to Prevent Error 1040
- Monitor
SHOW STATUS LIKE 'Threads_connected';againstmax_connectionsand alert before it fills. - Keep
wait_timeoutlow (60s) so idle connections cannot accumulate. - Run object caching on any site with real traffic.
- Load-test before expected traffic spikes.
- On shared hosting, know your plan’s connection limit and upgrade before traffic forces the issue.
Conclusion
MySQL error 1040 means demand exceeded the connection pool: restart or kill idle connections for instant relief, raise max_connections to stabilise, then fix the slow queries and leaks causing it. Treat the limit increase as breathing room, not the cure. For more database troubleshooting, browse the KamiWeb blog.
