# How to fix the “too many connections” error in MySQL/MariaDB

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](https://www.ionos.com/digitalguide/hosting/technical-matters/databases/) can only handle a limited number of requests simultaneously. If this **maximum value is exceeded**, [MySQL](https://www.ionos.com/digitalguide/server/know-how/what-is-mysql/) or [MariaDB](https://www.ionos.com/digitalguide/server/know-how/what-is-mariadb/) will display the MySQL “too many connections” error message. This occurs, for example, when a [PHP](https://www.ionos.com/digitalguide/websites/website-creation/learn-php-our-all-encompassing-php-tutorial-for-beginners/) script tries to establish too many simultaneous connections to the [relational database](https://www.ionos.com/digitalguide/hosting/technical-matters/relational-databases/). Similarly, if you have a web application that uses a MySQL database, the MySQ “too many connections” error may appear during very **high demand**.

## 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](https://www.ionos.com/digitalguide/server/know-how/what-is-ram/) (RAM)
- How much RAM each connection consumes (simple queries require less RAM than complex ones)
- The acceptable response time

According to the [MySQL documentation](https://dev.mysql.com/doc/refman/8.0/en/too-many-connections.html "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:

```none
sudo nano /etc/my.cnf
```

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

```none
[mysqld]
```

Add a new entry:

```plaintext
max_connections=[desired new maximum number]
```

#### 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:

```none
SET GLOBAL max_connections = [desired new maximum number];
```

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

### 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:

```none
[mysqld]
```

Insert the following lines:

```none
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.

```none
SET GLOBAL wait_timeout=120;
SET GLOBAL interactive_timeout=120;
```


This is a markdown version of: [https://www.ionos.com/digitalguide/websites/web-development/solve-mysqlmariadb-too-many-connections-error/](https://www.ionos.com/digitalguide/websites/web-development/solve-mysqlmariadb-too-many-connections-error/) for AI/LLM consumption.