The MySQL ‘too many con­nec­tions’ error occurs when the server’s maximum number of sim­ul­tan­eous 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 sim­ul­tan­eously. 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 sim­ul­tan­eous con­nec­tions to the re­la­tion­al database. Similarly, if you have a web ap­plic­a­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
  • ISO-certified European data centres
  • Highly secure and GDPR compliant

How to fix the ‘too many con­nec­tions’ MySQL/MariaDB error

There are various ap­proaches 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­term­ines 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­ment­a­tion, most Linux systems can typically handle 500 to 1,000 con­nec­tions without problems.

Per­man­ently adjusting max_connections

To per­man­ently 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­por­ar­ily adjusting max_connections

To tem­por­ar­ily 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.

Solution 2: Close unused con­nec­tions

A frequent cause of the ‘too many con­nec­tions’ error is when ap­plic­a­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 re­com­men­ded 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­form­ance, 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 auto­mat­ic­ally closes them. wait_timeout applies to standard con­nec­tions, while interactive_timeout is used for in­ter­act­ive 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­man­ently in the con­fig­ur­a­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­ne­ces­sar­ily block resources for too long.

Temporary ad­just­ment

This ad­just­ment can also be made tem­por­ar­ily through the MySQL console. The change will then only apply to the current con­nec­tion and the values will be reset after re­start­ing the SQL server.

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