Introduction to the MySQL error: Too Many Connections
When you have a web application (site, blog, forum, etc.) that is in high demand, that is, a large number of users visit, this translates into increased consumption on the server. If said web app uses a MySQL database and the queries are really many (due to bad programming of the web or by many users using the web), there is a possibility that MySQL will show this error:
mysqli_connect(): (HY000/1040): Too many connections
What does the MySQL: Too Many Connections error mean?
It means that too many requests are coming to MySQL, more than it can accept, more than it can queue or wait.
How to solve it?
Simple, we must increase the maximum limit of requests (connections) that MySQL supports.
I will give you two options to fix this problem:
1. We edit the file /etc/mysql/my.cfg:
nano /etc/mysql/my.cfg
In it we put the following under where it says [mysql]:
max_connections = 500 max_user_connections = 500
This will increase the maximum number of connections from 100 (which is the default) to 500.
We save and exit, then we restart the MySQL service and that's it. This change is permanent.
2. Another way to solve this problem is to change the maximum limit equal, but through a MySQL query.
Let's first show the current limit:
mysql --user="root" --password="PASSWORD" --execute='SHOW VARIABLES LIKE "max_connections";'
This will show us something like this:
+ ----------------- + ------- + | Variable_name | Value | + ----------------- + ------- + | max_connections | 151 | + ----------------- + ------- +
In other words, the current limit is 151 connections, well, let's raise it to 500 by means of a query:
mysql --user="root" --password="PASSWORD" --execute='SET GLOBAL max_connections = 500;'
Ready!
The problem in this way is that when the service is restarted, this configuration is lost.
To supply this detail, you can make a bash script that every X time verifies, or even add the line to the start or restart block of the daemon 😉
But then why do I want to know this 2nd option? ... well, that's what I used to say. But a month ago an Ubuntu Server ignored method No.1, so ... in extreme cases of silly OS, we have this 2nd option that works just as well 😉
Good post, MySql… they use a lot in FreeBSD,… .KZKG ^ Gaara?,… It would be good if you make a post, on how to install and configure MySql in FreeBSD like: SSH (secure - shell), SSH (via Web), SFTP (SSH-File Transfer Protocol), Apache - PHP- MySql, PHP5 and PhpSysInfo Extensions.
To do this I would have to install FreeBSD, I don't think I have the time right now, I just changed jobs and I have many new responsibilities 🙁
I recently had to do something similar for a project with nodejs. In my case, I increased to 250 and that was enough for me, for now I'm doing fine. Thanks for the information
Hello, could you help me how to enter /etc/mysql/my.cfg?
I have a VPS, but I can't get in with PUTTY.
Greetings.
To solve the problem of Option 1.- Commented option, and keep the changes when restarting the server, the file must be edited according to version:
/////////////////////////////////////////////////////// ////////////////////
// I change the directory in Ubuntu 16.04 ////////////////////////
/////////////////////////////////////////////////////// ////////////////////
I saw /etc/mysql/mysql.conf.d/mysqld.cnf
/////////////////////////////////////////////////////// ////////////////////
// I change the directory in Ubuntu 15.04 ////////////////////////
/////////////////////////////////////////////////////// ////////////////////
vi /etc/mysql/mariadb.conf.d/mysqld.cnf
/////////////////////////////////////////////////////// ////////////////////
// I change the directory in Ubuntu old ////////////////////////
/////////////////////////////////////////////////////// ////////////////////
I saw /etc/mysql/my.cnf
/////////////////////////////////////////////////////// ////////////////////
// add this line below the [mysqld] or [mysql] tag //
// Then restart the server //
/////////////////////////////////////////////////////// ////////////////////
Max_conneccions = 500
Tutorial from scratch at no time tells you how to change it