The MySQL “too many connections” error occurs when the server’s maximum number of simultaneous connections is exceeded. Each active client session uses one connection, and once the limit is reached, no additional connections can be established. Common causes include excessive parallel queries, connections left open, or a max_connections value that is set too low on the server.

How does the “too many connections” error occur in MySQL/MariaDB?

A database can only handle a limited number of requests simultaneously. If this maximum value is exceeded, MySQL or MariaDB will display the MySQL “too many connections” error message. This occurs, for example, when a PHP script tries to establish too many simultaneous connections to the relational database. Similarly, if you have a web application that uses a MySQL database, the MySQ “too many connections” 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 connections” MySQL/MariaDB error

There are various approaches to fix the MySQL “too many connections” error, depending on the exact issue. The most common solutions are explained in detail below.

Solution 1: Choose a new maximum number of connections

The system variable max_connections determines the number of connections that MySQL/MariaDB will accept. The default value is 151 connections, allowing 150 regular connections 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 connections, keep in mind several important factors:

  • The amount of available memory (RAM)
  • How much RAM each connection consumes (simple queries require less RAM than complex ones)
  • The acceptable response time

According to the MySQL documentation, most Linux systems can typically handle 500 to 1,000 connections without problems.

Permanently adjusting max_connections

To permanently 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

Temporarily adjusting max_connections

To temporarily change the number of maximum connections for the current session, you can execute the following command in the MySQL console:

SET GLOBAL max_connections = [desired new maximum number];

The adjustment remains in effect until the SQL server is restarted.

Create a website with your domain
Build your own website or online store, fast
  • Professional templates
  • Intuitive customizable design
  • Free domain, SSL, and email address

Solution 2: Close unused connections

A frequent cause of the “Too many connections” error is when applications fail to properly close database connections. Every open connection uses up a slot in MySQL or MariaDB, and if connections 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 recommended best practice is to use connection pooling. With this approach, a fixed number of database connections are created, managed centrally, and reused instead of opening a new connection for each request. This helps lower server load and improves performance, since establishing and closing connections takes extra time.

Solution 3: Adjust connection timeout

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

The system variables wait_timeout and interactive_timeout control how long inactive connections stay open before the server automatically closes them. wait_timeout applies to standard connections, while interactive_timeout is used for interactive clients, such as the MySQL console. Lowering these values helps free up “hanging” connections more quickly.

Permanent adjustment

The adjustment can be made permanently in the configuration 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 connections do not unnecessarily block resources for too long.

Temporary adjustment

This adjustment can also be made temporarily through the MySQL console. The change will then only apply to the current connection and the values will be reset after restarting the SQL server.

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