ERROR 1040 (HY000): Too many connections

MySQL Error 1040: Too Many Connections Fix

Database Errors Intermediate 👁 5 views 📅 Jun 23, 2026

Quick fix: increase max_connections or kill idle queries. Here's how to do both without restarting MySQL.

Quick Answer (for advanced users)

If you already know what you're doing: log in to MySQL as root and run:

SET GLOBAL max_connections = 500;

Then check who's hogging connections with SHOW PROCESSLIST; and kill threads with KILL thread_id;. More details below.

What's happening here?

You get this error when your MySQL server has hit its connection limit. By default, MySQL allows 151 connections (in older versions it was 100). That sounds like a lot, but it's not when you have a busy web app, cron jobs, or tools like WordPress that open a new connection per page load.

The real trigger? Usually it's one of these three things:

  • A script has a bug and doesn't close database connections properly (persistent connections gone wild)
  • Your site got a traffic spike and the default limit couldn't keep up
  • A slow query is holding connections open for too long

Don't panic. You can fix this without restarting MySQL. Here's how.

Step-by-Step Fix

Step 1: Get a connection (any connection)

If you're completely locked out, you need to force a connection. MySQL reserves one connection for the root user (the 'super' privilege). So as root, try:

mysql -u root -p

If that also says "Too many connections", you can still log in by adding --max-connections=1000 to the command line. Like this:

mysql -u root -p --max-connections=1000

That tells MySQL to temporarily raise the limit just for you. Once you're in, do step 2.

Step 2: Check who's taking all connections

Run this command inside MySQL:

SHOW PROCESSLIST;

You'll see a table with columns: Id, User, Host, db, Command, Time, State, Info. Pay attention to the 'Time' column — it shows how many seconds each connection has been idle. Anything over 30 seconds that's sleeping? That's a problem.

Also look for multiple connections from the same user (like 'wordpress' or 'appuser'). If you see 50 connections from one user, that's your culprit.

Step 3: Kill bad connections

To kill a specific connection, use:

KILL 12345;

(replace 12345 with the actual Id from the processlist)

If you have dozens to kill, you can build a command to kill them all at once. First, generate the kill commands:

SELECT CONCAT('KILL ', Id, ';') FROM information_schema.PROCESSLIST WHERE User = 'bad_user' AND COMMAND = 'Sleep' AND TIME > 30;

Copy the output and paste it into MySQL. That kills all sleeping connections from 'bad_user' that have been idle for over 30 seconds.

Step 4: Increase max_connections (temporary fix)

While still in MySQL, run:

SHOW VARIABLES LIKE 'max_connections';

Check the current value. If it's 151, you can raise it to 500 for now:

SET GLOBAL max_connections = 500;

This change is immediate but won't survive a MySQL restart. For a permanent fix, see the prevention section below.

Step 5: Confirm the fix

Run this to see current connections vs max:

SHOW VARIABLES LIKE 'max_connections';
SHOW STATUS LIKE 'Threads_connected';

If Threads_connected is below max_connections, you're good. The error should stop.

If That Didn't Work: Alternative Fixes

Fix A: Restart MySQL (last resort)

If you can't even log in as root, restarting MySQL will kill all connections and let you start fresh. On Linux:

sudo systemctl restart mysql

Or on older systems:

sudo service mysql restart

Warning: this drops all active connections. Users will see errors for a few seconds. Use only if nothing else works.

Fix B: Check for a runaway script

If the problem keeps coming back, you have a script that's not closing connections. Common culprits:

  • WordPress with a plugin that uses persistent connections (wp-config.php has define('WP_USE_PERSISTENT_CONNECTIONS', true);)
  • PHP scripts using mysql_pconnect() (old, deprecated — switch to PDO or mysqli)
  • A cron job that runs every minute but takes 2 minutes to complete (overlapping runs)

Fix the script, and the problem goes away.

Fix C: Check your connection pool

If you use a connection pool (like in Java or Python apps), make sure the pool size isn't bigger than max_connections. For example, if max_connections is 200 and your pool is set to 100, you're fine. But if you have 3 pools each set to 100, that's 300 connections — you'll hit the limit fast.

Prevention: Stop It From Happening Again

Permanent max_connections change

Edit your MySQL config file (usually /etc/mysql/my.cnf or /etc/my.cnf on Linux, or my.ini on Windows). Find the line:

max_connections = 151

Change it to something reasonable for your server's RAM. A good rule: start with 500 for a server with 2GB RAM, 1000 for 4GB RAM. Don't go crazy — each connection takes about 2MB of memory. Setting max_connections to 5000 on a 1GB server will cause swap issues.

After editing, save and restart MySQL:

sudo systemctl restart mysql

Set a connection timeout

Add this to your config to kill idle connections after 30 seconds:

wait_timeout = 30
interactive_timeout = 30

This won't fix a bad script, but it'll clean up connections that should have been closed.

Monitor connections

Set up a simple cron job that checks connections every 5 minutes and alerts you if they're over 80% of max. For example, a bash script that emails you if SHOW STATUS LIKE 'Threads_connected' returns a value above your threshold.

Use a connection proxy (advanced)

If you're running a high-traffic app, consider using a tool like ProxySQL or HAProxy in front of MySQL. These manage a connection pool so your app doesn't directly open new connections. It's overkill for a small site, but for busy sites it's a lifesaver.

One last thing: if you're on shared hosting and you see this error, you can't change the MySQL config. Contact your host and ask them to increase max_connections for your database user. If they say no, move to a VPS where you have control.

Was this solution helpful?