DEV Community

Cover image for How to Fix MySQL Too Many Connections Error (Step-by-Step Guide)
Meghna Meghwani for ServerAvatar

Posted on Originally published at serveravatar.com

How to Fix MySQL Too Many Connections Error (Step-by-Step Guide)

Your site is up one minute, then visitors get blank page or database error. You check logs and find the MySQL Too Many Connections Error. It looks like this:

ERROR 1040 (08004): Too many connections
Enter fullscreen mode Exit fullscreen mode

That’s MySQL too many connections error, and it is most common database problem on a growing VPS. In this guide, I will walk you through how to diagnose, raise limit safely, and fix real cause so it does not come back.

TL;DR

  • Error 1040 means every available connection slot is in use, so MySQL refuses new clients
  • Diagnose first with SHOW FULL PROCESSLIST before touching any settings
  • Raise max_connections only as temporary relief, and check your RAM first
  • Lower wait_timeout to clear idle connections, and fix slow queries and leaky app code
  • Use connection pooling and monitoring to prevent repeat

What MySQL Too Many Connections Error Actually Means

Every client that talks to MySQL, whether it’s your PHP app, cron job, or backup script, uses one connection slot. Number of slot is controlled by “max_connections” system variable. According to official MySQL reference manual, when this error appear, all available connections are in use by other client.

MySQL Too Many Connections Error

Default is usually 151 on stock install. That sounds like plenty, until you remember that one busy WordPress page can open several connections, and slow query can hold slot for minutes.

MySQL also keeps one extra slot in reserve. It’s available only to account with “CONNECTION_ADMIN” privilege (or older “SUPER”), so admin can still log in and investigate during outage.

If WordPress only show generic database error, start with guide to fixing the WordPress database connection error to check whether MySQL is running and your database setting are correct.

Start by checking connection limit and seeing which apps or jobs are using available slots. If your site is already down, you can make small, temporary change to get it back online while you look for the cause. The steps below walk you through what to check, what to change, and how to check problem early if it happen again.

Step 1: Log In and Check Your Current Limit

Connect as admin user:

sudo mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

If your normal users are locked out, reserved admin connection is what lets you in. Check current limit first using below command.

SHOW VARIABLES LIKE 'max_connections';
SHOW STATUS LIKE 'Max_used_connections';
SHOW STATUS LIKE 'Threads_connected';
Enter fullscreen mode Exit fullscreen mode

You can see the output as mentioned:

check the status for MySQL Too Many Connections Error

Here’s how read output:

Value Table

If “Max_used_connections” equal “max_connections”, you have hit limit. If it’s far below limit, something else is happening, such as restart that reset counter, or different user-level limit.

Read Full Article: https://serveravatar.com/fix-mysql-too-many-connections-error

Top comments (0)