MySQL

Resolving mysql Error “Cannot connect to DB: Too many connections”

There are 2 different limits for MYSQL. Max Connections and Max user connections. By default, the limit is 151 for max connections and 0 or unlimited for user connections.

MySQL server has a default limit of 151 simultaneous connections. Most connections to the SQL server run very quickly so even a large amount of queries should not cause a server to hit this limit. This problem usually occurs when a query takes too long to execute or locks a table preventing other queries from executing (and building up the number of simultaneous connections).

Once the server hits the limit of connections, it will refuse to accept new queries and return error 1203: Too many connections.

If the server is currently at the limit of connections, it will still accept one more connection from a MySQL account with super user privileges (like user root). You can use this connection to view the currently running queries or try to kill the queries that are running for too long.

Check for the settings. Log into the mysql server. Run the following:
Max Connections

MariaDB [(none)]> SHOW VARIABLES LIKE "max_connections";
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 151   |
+-----------------+-------+
1 row in set (0.00 sec)

Max Use
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘max_use%’;
+———————-+——-+
| Variable_name | Value |
+———————-+——-+
| max_user_connections | 0 |
+———————-+——-+
1 row in set (0.00 sec)
How to update max_connections Value

Before increasing this value, make sure your server has enough resources to handle more queries. Now execute below query in mysql terminal to set this value temporarily. Remember that this value will reset on next mysql reboot.


MariaDB [(none)]> SET GLOBAL max_connections = 250;
Query OK, 0 rows affected (0.00 sec)

To set this value permanently, edit mysql configuration file on your server and set following variable. The configuration file location may change as per your operating system. By default you can find this at /etc/my.cnf on CentOS and RHEL based system and /etc/mysql/my.cnf on Debian based system. Under [mysqld]:

max_connections = 250

Now restart mysql service and check value again with above given command. This time you will see that value is set to 250.

MariaDB [(none)]> SHOW VARIABLES LIKE "max_connections";
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 250   |
+-----------------+-------+
1 row in set (0.00 sec)

Another simple way to temporarily resolve this issue is to restart the MySQL server. On CentOS/RHEL:

 # systemctl restart mariadb.service

You can also enable the slow query log which will record queries that take longer than a specified amount of time (two seconds by default) for later review and optimization.

nano /etc/my.cnf
slow_query_log = 1
slow_query_log_file = /var/log/mariadb/slow-query.log

Restart

 # systemctl restart mariadb.service