Advertisement
database The “Too many connections” error in MySQL happens when your database server hits its maximum allowed number of open connections. This error is common on busy websites, poorly optimized applications, or servers with low default limits.

How to Fix “Too Many Connections” in MySQL (Beginner-Friendly Guide)

5 Min Read Verified Content

Seeing this error in your terminal or website?

ERROR 1040 (08004): Too many connections

Relax. You didn’t break the database. MySQL is simply saying:

“I’m full. I have no room for more users right now.”

Think of MySQL like a small café. It has a maximum number of seats. Once every seat is taken, new customers must wait outside — that’s your “Too many connections” error.

Let’s fix this problem step-by-step.




## Step 1 — Check Your Current Connection Limit

Before you change anything, find out your current MySQL limit.

Login to MySQL:

mysql -u root -p

Then run:

SHOW VARIABLES LIKE 'max_connections';

You might see something small like:

max_connections = 151

This is the number of “seats” in your MySQL café.




## Step 2 — Check How Many Connections Are Being Used

To see what’s happening right now:

SHOW STATUS LIKE 'Threads_connected';

If you see a number very close to your max_connections, that’s the cause.

Example:

Threads_connected = 149 max_connections = 151

Yep… the café is full.




## Step 3 — Check Which Clients Are Using All Connections

Sometimes a single PHP script, bot, or app gets stuck and opens hundreds of connections.

Run this:

SHOW PROCESSLIST;

Look for:

  • Many identical connections

  • Sleep state for too long

  • Many connections from the same IP

  • Queries stuck on “Locked”

If you see suspicious long Sleep connections, that’s a common beginner problem.

Example:

| Sleep | 800 seconds | | Sleep | 650 seconds | | Sleep | 700 seconds |

These idle connections eat up your limit for no reason.




## Step 4 — Fix Quickly: Kill Long Sleep Connections

Use this command to remove “sleeping” troublemakers:

KILL CONNECTION <id>;

Or kill all sleeping connections at once (safe for beginners):

SELECT CONCAT('KILL ', id, ';') FROM information_schema.processlist WHERE COMMAND='Sleep' AND TIME > 300;

Copy the output and run it.

Now your connection count should drop.




## Step 5 — Prevent the Error by Increasing max_connections


If your server is busy, increase the limit.

In your MySQL config file:

/etc/mysql/my.cnf or /etc/mysql/mysql.conf.d/mysqld.cnf

Under [mysqld], add:

max_connections = 500

Restart MySQL:

sudo systemctl restart mysql

Now check again:

SHOW VARIABLES LIKE 'max_connections';

Be careful:
Don’t set this value too high on very small servers.
More connections = more RAM usage.


Beginners can use:

Server RAMRecommended max_connections
1 GB100–200
2 GB200–400
4 GB400–800
8 GB+1000 or more


## Step 6 — Fix PHP or App Issues That Leak Connections


Most “Too many connections” errors happen because applications:

  • Open too many connections

  • Forget to close them

  • Use old database drivers

  • Have unoptimized code


✔ If you’re using PHP + MySQLi

Always close the connection:

$mysqli->close();

✔ If you're using PDO

$db = null;

✔ Use connection pooling (Node.js, Python, Java, Go)


Example for Node.js (mysql2):

const pool = mysql.createPool({ connectionLimit: 10 });

A pool ensures connections are reused instead of opening 1000 new ones.




## Step 7 — Tune MySQL Wait Timeout


If connections stay “Sleep” for too long, reduce timeout.

Add:

wait_timeout = 60 interactive_timeout = 60

This kicks out inactive clients after 60 seconds.

Restart MySQL.




## Step 8 — Check if Bots, Crawlers, or Attackers Are Flooding MySQL


Sometimes the issue is not your app — it’s an attack.

Check number of connections per IP:

SELECT host, COUNT(*) FROM information_schema.processlist GROUP BY host;

If one IP has 300+ connections → block it:

sudo ufw deny from <IP>

Or use fail2ban.




## Step 9 — Monitor MySQL So This Doesn’t Happen Again


Install a server monitoring tool:

  • Netdata

  • Prometheus + Grafana

  • MySQLTuner

  • Percona Monitoring

For a quick feel, run:

mysqladmin processlist status extended-status


## Step 10 — Final Recommendation (Beginner-Proof Fix)


Most beginners struggle because:

  • MySQL timeout too high

  • Application leaks connections

  • max_connections too low

  • They don’t monitor MySQL

The best simple fix:

  1. Increase max_connections

  2. Lower wait_timeout

  3. Use connection pooling

  4. Kill sleeping connections

  5. Restart MySQL

This eliminates 95% of “Too many connections” problems.




## Final Thoughts


The “Too many connections” error might look scary at first, but it’s actually one of the simplest MySQL problems to fix. Once you understand that MySQL has a limit on how many clients it can handle at once, everything becomes clearer.


Advertisement
Back to Database