[Solved] MySQL: Too many connections error

This guide shows you how to solve the error message "Connect to MySQL server 127.0.0.1: Too many connections". The error can appear in any log file of a service that connects to the MySQL server. This may e.g. be the /var/log/mail.log file if your email system uses a MySQL database. The error means that the maximum number of connections limit in MySQL has been reached. The same applies to MariaDB servers as well.

Increase MySQL Connection limit

To increase the number of simultaneous connections in MySQL, edit the /etc/mysql/my.cnf file:

nano /etc/mysql/my.cnf

and add or change the lines for MySQL max_connections and max_user_connections in the [mysqld] section of my.cnf file:

[mysqld]
......
max_connections = 500
max_user_connections = 500
.......

Restart MySQL to apply the changes:

systemctl restart mysql

What did these settings do?

  • max_connections: The max_connections setting limits the max overall number of open connections to MySQL.
  • max_user_connections: This limit restricts how many connections a single MySQL user can open.

Choose a value for both settings that suits your server. In most cases, a value of 500 should be enough. the default value which is used when these values are not set is 100.

Raise MariaDB Connection limit

The same error can occur on MariaDB servers too. The solution is basically the same as what you use for MySQL, just the name of the configuration file differs. Older MariaDB systems will use the /etc/mysql/my.cnf file too. Newer systems on Debian and Ubuntu will use the file /etc/mysql/mariadb.conf.d/50-server.cnf.

Edit the MariaDB server configuration file, e-g- with the nano editor:

nano /etc/mysql/mariadb.conf.d/50-server.cnf

And add the lines:

max_connections = 500
max_user_connections = 500

In the [mysqld] section of the file. See screenshot below:

MySQL too many connections

Restart MariaDB to apply the changes.

systemctl mariadb restart

In case you lost root access to MySQL, have a look here.