The MySQL “too many con­nec­tions” error occurs when the server’s maximum number of si­mul­ta­ne­ous con­nec­tions is exceeded. Each active client session uses one con­nec­tion, and once the limit is reached, no ad­di­tion­al con­nec­tions can be es­tab­lished. Common causes include excessive parallel queries, con­nec­tions left open, or a max_connections value that is set too low on the server.

How does the “too many con­nec­tions” error occur in MySQL/MariaDB?

A database can only handle a limited number of requests si­mul­ta­ne­ous­ly. If this maximum value is exceeded, MySQL or MariaDB will display the MySQL “too many con­nec­tions” error message. This occurs, for example, when a PHP script tries to establish too many si­mul­ta­ne­ous con­nec­tions to the re­la­tion­al database. Similarly, if you have a web ap­pli­ca­tion that uses a MySQL database, the MySQ “too many con­nec­tions” error may appear during very high demand.

HiDrive Cloud Storage
Store and share your data on the go
  • Store, share, and edit data easily
  • Backed up and highly secure
  • Sync with all devices

How to fix the “too many con­nec­tions” MySQL/MariaDB error

There are various ap­proach­es to fix the MySQL “too many con­nec­tions” error, depending on the exact issue. The most common solutions are explained in detail below.

Solution 1: Choose a new maximum number of con­nec­tions

The system variable max_connections de­ter­mines the number of con­nec­tions that MySQL/MariaDB will accept. The default value is 151 con­nec­tions, allowing 150 regular con­nec­tions plus one from the SUPER account. SUPER is a MySQL privilege that grants admin rights to the user.

Start by deciding on the new maximum value you want to set for max_connections. When raising the number of MySQL/MariaDB con­nec­tions, keep in mind several important factors:

  • The amount of available memory (RAM)
  • How much RAM each con­nec­tion consumes (simple queries require less RAM than complex ones)
  • The ac­cept­able response time

According to the MySQL doc­u­men­ta­tion, most Linux systems can typically handle 500 to 1,000 con­nec­tions without problems.

Per­ma­nent­ly adjusting max_connections

To per­ma­nent­ly adjust the max_connections variable, you need to update the my.cnf or my.ini file.

Open the file /etc/my.cnf for editing with the command:

sudo nano /etc/my.cnf

Directly below the first line, you will see the following entry:

[mysqld]

Add a new entry:

max_connections=[desired new maximum number]
plaintext

Tem­porar­i­ly adjusting max_connections

To tem­porar­i­ly change the number of maximum con­nec­tions for the current session, you can execute the following command in the MySQL console:

SET GLOBAL max_connections = [desired new maximum number];

The ad­just­ment remains in effect until the SQL server is restarted.

Create a website with your domain
Build your own website or online store, fast
  • Pro­fes­sion­al templates
  • Intuitive cus­tomiz­able design
  • Free domain, SSL, and email address

Solution 2: Close unused con­nec­tions

A frequent cause of the “Too many con­nec­tions” error is when ap­pli­ca­tions fail to properly close database con­nec­tions. Every open con­nec­tion uses up a slot in MySQL or MariaDB, and if con­nec­tions aren’t released, the server can quickly hit the maximum limit.

In PHP, always call mysqli_close($connection) once your queries are finished. In Python, use connection.close(), and in Node.js, call connection.end().

A rec­om­mend­ed best practice is to use con­nec­tion pooling. With this approach, a fixed number of database con­nec­tions are created, managed centrally, and reused instead of opening a new con­nec­tion for each request. This helps lower server load and improves per­for­mance, since es­tab­lish­ing and closing con­nec­tions takes extra time.

Solution 3: Adjust con­nec­tion timeout

Another effective way to prevent the “Too many con­nec­tions” error is by limiting how long inactive con­nec­tions can remain open. By default, MySQL/MariaDB keeps idle con­nec­tions alive for quite a while, even if they’re no longer running queries. Each of these idle con­nec­tions still consumes a slot.

The system variables wait_timeout and interactive_timeout control how long inactive con­nec­tions stay open before the server au­to­mat­i­cal­ly closes them. wait_timeout applies to standard con­nec­tions, while interactive_timeout is used for in­ter­ac­tive clients, such as the MySQL console. Lowering these values helps free up “hanging” con­nec­tions more quickly.

Permanent ad­just­ment

The ad­just­ment can be made per­ma­nent­ly in the con­fig­u­ra­tion file my.cnf or my.ini. To do this, open it and look for the following entry:

[mysqld]

Insert the following lines:

wait_timeout=120
interactive_timeout=120

This ensures that inactive con­nec­tions do not un­nec­es­sar­i­ly block resources for too long.

Temporary ad­just­ment

This ad­just­ment can also be made tem­porar­i­ly through the MySQL console. The change will then only apply to the current con­nec­tion and the values will be reset after restart­ing the SQL server.

SET GLOBAL wait_timeout=120;
SET GLOBAL interactive_timeout=120;
Go to Main Menu