The MySQL “Too many connections” error occurs when more queries are sent to a MySQL database than can be processed. The error can be fixed by setting a new number of maximum connections in the configuration file or globally.
Contents
- How does the MySQL “Too many connections” error happen?
- Choose a new maximum number of connections
- Change max_connections
- Display the number of connections in MySQL
- Update my.cnf file
- Set number of connections globally
How does the MySQL “Too many connections” error happen?
A database can only handle a limited number of queries at a time. If the maximum is exceeded, MySQL displays the error message above. This is the case, for example, when a PHP script tries to establish too many simultaneous connections to the relational database. If you’re using a web application that uses a MySQL database, it is possible that the MySQL “Too many connections” error occurs when demand is very high.
HiDrive Cloud Storage with IONOS!
Based in Europe, HiDrive secures your data in the cloud so you can easily access it from any device!
Choose a new maximum number of connections
The system variable max_connections determines the number of connections which MySQL/MariaDB will accept. The default value is 151 connections, which allows 150 normal connections plus one connection from the SUPER account. SUPER is a MySQL privilege that grants admin rights to the user.
The first thing to decide is what new maximum value you want to set for max_connections. There are several considerations to take into account when increasing the number of MySQL/MariaDB connections. The maximum number which can be supported by the system will depend on:
- The amount of available RAM
- How much RAM each connection takes (simple queries will require less RAM than more labor-intensive connections).
- The acceptable response time.
According to the MySQL documentation, most Linux systems should be able to support 500 to 1,000 connections without difficulty.
HiDrive Cloud Storage with IONOS!
Based in Europe, HiDrive secures your data in the cloud so you can easily access it from any device!
Change max_connections
The max_connections variable can be changed in two places:
Update the my.cnf file, so that the new value is used if the MySQL/MariaDB server is restarted.
Use the SET GLOBAL command to update the value on the running MySQL/MariaDB server. In this case, there is no need to restart MySQL/MariaDB, so you do not have to allow for any downtime of your database.
Display the number of connections in MySQL
To check the current number of max_connections log in to the MySQL/MariaDB command line client with the following command:
This will output a list of all of the variables which are set for MySQL/MariaDB. Scroll up through the list to find the value for max_connections.
Update my.cnf file
Open the file /etc/my.cnf for editing with the command:
Directly beneath the first line, you’ll see the following entry:
max_connections=[desired new maximum number]
For example, to set max_connections to 200, the first two lines of your configuration file should look like this:
[mysqld] max_connections=200
Set number of connections globally
You can also set the maximum number of connections for your database globally. First log in to the MySQL/MariaDB command line client using the command:
Adjust the new maximum number of your choice of the new max_connections value with the command:
SET GLOBAL max_connections=[desired new maximum number];
For example, to set max_connections to 200, the command is:
SET GLOBAL max_connections=200;
Finally, exit MySQL/MariaDB with the command:
WordPress Managed Hosting with IONOS!
Start your website quickly and benefit from the most secure and up-to-date version of WordPress!
MySQL tutorial: the comprehensive guide for beginners
Along with Oracle and Microsoft SQL server, MySQL is one of the most popular management systems for relational databases. Nowadays the software developed by MySQL AB belongs to the product portfolio of the Oracle Corporation and is proprietarily marketed under a dual license. Since 2009, an open source MySQL fork, known as MariaDB, has also been available from the original developers. Here we give...
MySQL tutorial: the comprehensive guide for beginnersMariaDB vs. MySQL
MySQL is the most used database management system in the world. This open source software has been available for productive use since 1995 and is generally considered a standard solution for users of relational databases. This could change in the near future. MariaDB, a community-developed fork of MySQL is catching up on its predecessor and is consequently becoming an increasingly attractive...
MariaDB vs. MySQLMySQLi: a new development in PHP
Converting the MySQL extension to MySQLi led to some uncertainty in the PHP community since some of the industry’s heavyweights like WordPress continued to rely on the old extension despite it being deprecated. However, as of PHP 7, this extension is no longer available. In this article, we will go over its successor in detail and present examples which highlight the differences between the two...
MySQLi: a new development in PHP