Introduction to the MySQL error: Too Many Connections
When you have a web application (site, blog, forum, etc.) that experiences high demand, meaning a large number of users visit it, this translates into increased server resource consumption. If this web application uses a MySQL database and the queries are very numerous ( due to poor website programming or a high volume of users ), MySQL may display 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 by changing the maximum limit, but using 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 fix this detail, you can create a bash script that checks every X time, 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 was saying. But a month ago an Ubuntu Server ignored method No.1, so… in extreme cases of stupid OS, we have this 2nd option that works just as well 