AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

Too many connections: fix error in MySQL and MariaDB

PARTAGEZ

The MySQL error “ Too many connections ” (or “Too many connections”) occurs when the maximum number of simultaneous connections allowed to the server is exceeded. Each active client occupies a connection, and once the limit is reached, no new connections can be opened. The most common causes are too many queries running in parallel, connections not closed properly, or a value max_connections too weak on the server.

A database can only handle a limited number of simultaneous queries. If this maximum limit is exceededMySQL or MariaDB displays the error message “Too many connections”. This happens, for example, when a PHP script attempts to establish too many simultaneous connections to the relational database. Likewise, in the case of a Web application based on MySQL, a high attendance can also cause the error.

HiDrive Next online storage

Your data accessible anywhere and at any time

  • Edit, share and store your files
  • ISO certified European data centers
  • High data security, GDPR compliant

There are several ways to resolve the « Too many connections » error in MySQL, depending on the exact cause of the problem. The most common solutions are detailed below.

Solution 1: Increase the MySQL/MariaDB connection limit

There system variable max_connections determines the number of connections that MySQL or MariaDB can accept simultaneously. By default, this value is set to 151 connections, i.e. 150 classic connections plus one connection reserved for SUPER account. This MySQL privilege grants administrative rights to the user.

Start by setting the new maximum value you want to assign to max_connections. Several factors should be considered before increasing this limit:

  • The amount of RAM available
  • The amount of RAM used by each connection (simple queries consume less memory than complex operations)
  • The acceptable response time for the system

According to the MySQL documentationmost Linux systems can manage between 500 and 1,000 connections without difficulty.

Permanent adjustment

To permanently adjust the variable max_connectionsyou need to update the file my.cnf Or my.ini.

Open the file /etc/my.cnf to modify it using the following command:

Just below the first line you will see the following entry:

Then add the following line:

max_connections=[nouvelle_valeur_maximale]

Temporary adjustment

To temporarily change the maximum connection count only for the current session, you can run the following command in the MySQL console:

SET GLOBAL max_connections = [nouvelle_valeur_maximale];

The adjustment is valid until the SQL Server is restarted.

Solution 2: Close unused connections

Another common cause of the “Too many connections” error is failure to properly close database connections. Each open connection occupies a slot in MySQL or MariaDB, and if these connections are not released, the server quickly reaches the allowed limit.

In PHP, it is therefore essential to call mysqli_close($connection) once the queries are completed. In Python, use the equivalent statement connection.close()and in Node.js, the method connection.end().

Another proven approach is to use connection pools. This system allows you to manage a defined number of database connections and reuse them rather than opening new ones with each request. This reduces server load and improves performance because opening and closing connections takes time.

Solution 3: Adjust the connection timeout

An effective method to avoid the “Too many connections” error is to limit the maximum lifespan inactive connections. By default, MySQL/MariaDB keeps idle connections open for a relatively long period of time, even when they are no longer executing queries. Each of these connections, however, continues to occupy a location.

System variables wait_timeout And interactive_timeout determine how long idle connections remain open before being automatically closed by the server. wait_timeout applies to normal connections, while interactive_timeout concerns interactive connections, such as those in the MySQL console. By reducing these values, you can release « hanging » connections more quickly.

Permanent adjustment

The adjustment can be applied permanently in the configuration file my.cnf Or my.ini. To do this, open this file and look for the following entry:

Add the following lines:

wait_timeout=120
interactive_timeout=120

This ensures that idle connections do not unnecessarily hog resources for an extended period of time.

Temporary adjustment

This adjustment can also be applied temporarily from the MySQL console. In this case, the change only affects the current session and the values ​​are reset after restarting the SQL server.

SET GLOBAL wait_timeout=120;
SET GLOBAL interactive_timeout=120;

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Web Marketing

Localhost: how to connect to 127.0.0.1?

When you call an IP address, you are usually trying to contact another computer on the Internet. However, if you call the IP address 127.0.0.1,

Web Marketing

What is Proxmox? – IONOS

Proxmox is an open source platform dedicated to virtualization and containerization. It allows you to manage and operate virtual machines, containers and high availability clusters.

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact