Home/Blog/How to Fix MySQL ‘Too Many Connections’ Error (2026)

How to Fix MySQL ‘Too Many Connections’ Error (2026)

MySQL too many connections error fix WordPress 2026

When WordPress shows database errors mentioning connections, you need a MySQL Too Many Connections fix fast โ€” your site is down until the database accepts connections again. The two related errors โ€” “Too many connections” (error 1040) and “MySQL server has gone away” (error 2006) โ€” both mean WordPress cannot talk to its database. This guide explains what each error means and how to fix them on shared hosting and VPS.

What Causes “Too Many Connections” and “Server Has Gone Away”?

  • Too many connections (1040): the MySQL server hit its max_connections limit. On shared hosting, accounts are typically limited to 25โ€“50 concurrent connections. Traffic spikes, heavy WooCommerce reports, or plugins running unoptimized queries exhaust the pool, and new connections get rejected.
  • MySQL server has gone away (2006): the server closed the connection โ€” usually because it timed out an idle connection (wait_timeout), rejected an oversized packet (max_allowed_packet), or the mysqld process crashed (often killed by the Linux OOM killer when the server runs out of memory).

In PHP logs these appear as mysqli_real_connect(): (HY000/1040): Too many connections and Uncaught mysqli_sql_exception: MySQL server has gone away. If you see the generic database message instead, start with our Error Establishing a Database Connection fix.

Immediate Recovery: Get the Site Back Online

1. Check If MySQL Is Running (VPS)

Over SSH, run systemctl status mysql (or service mysql status). If it is stopped or failed, restart it with systemctl restart mysql. If it crashes again within minutes, you have a memory problem โ€” not a transient failure. Check dmesg for OOM-killer entries and the MySQL error log for “Out of memory” lines.

2. Kill Sleeping Connections

Stuck idle connections hold slots open. As a database admin, run SHOW PROCESSLIST;, identify connections in Sleep state, and kill them with KILL <id>. On shared hosting without admin access, ask your host’s support to do this or restart the MySQL instance.

3. Reduce Load Immediately

Put up a maintenance page, enable full-page caching, and let the traffic spike pass. For transient spikes, caching (page cache plus object cache like Redis) cuts database load dramatically while you debug the root cause.

Fixing MySQL too many connections error in WordPress

Fixing “Too Many Connections” Properly

4. Raise max_connections (If You Control the Server)

In my.cnf, raise max_connections (default 151) to a value your RAM supports โ€” e.g. 300โ€“500 โ€” and restart MySQL. Also check max_user_connections. Warning: raising limits without fixing slow queries just postpones the failure and can exhaust RAM. Monitor CPU and memory after scaling.

5. Find and Fix the Query Hogs

Install Query Monitor on WordPress to find slow or repeated queries. The usual suspects: related-posts plugins with live queries, uncached widgets, and WooCommerce admin reports. Optimize or replace the offenders. Enable the MySQL slow query log to catch problems proactively.

6. Add Object Caching

Redis or Memcached object caching slashes the number of database queries per page load. Note: caching reduces query volume but does not directly raise the connection limit โ€” combine it with the fixes above.

Fixing “MySQL Server Has Gone Away”

7. Tune Timeouts and Packet Size

Increase wait_timeout and interactive_timeout if idle connections are being killed too aggressively, and raise max_allowed_packet to 64M if large imports or BLOB data are being rejected. In WordPress, you can also set $wpdb->query("SET SESSION wait_timeout=300"); in wp-config.php. Check that PHP’s default_socket_timeout is not aborting before MySQL’s timeout.

8. Fix Memory Tuning (VPS)

The most common VPS cause is innodb_buffer_pool_size set too high for available RAM โ€” the OOM killer terminates mysqld. Size the buffer pool to roughly 50โ€“70% of RAM on a dedicated database server, less on a shared web+DB box. After tuning, monitor for 24 hours before declaring victory.

How to Prevent Database Connection Errors

  • Keep WordPress, themes, and plugins updated โ€” old code runs inefficient queries.
  • Run object caching (Redis) on any site with real traffic.
  • Monitor SHOW PROCESSLIST during peak hours for connection buildup.
  • Set up alerts for MySQL restarts and connection errors in your logs.
  • On shared hosting, know your plan’s connection limit and upgrade before traffic forces the issue.

Conclusion

“Too many connections” means demand exceeded the MySQL connection pool โ€” kill the idle connections, fix the query hogs, add caching, and raise limits only with enough RAM. “Server has gone away” means connections are being dropped โ€” tune timeouts, raise max_allowed_packet, and fix memory tuning so mysqld stops crashing. Handle the immediate outage first, then fix the root cause, or the error will be back with the next traffic spike. For more database troubleshooting, see our WordPress critical error guide.