I'm getting the following errors in my script:
mysqli_connect(): (08004/1040): Too many connections
mysqli_connect(): (HY000/1040): Too many connections
What is the difference and how can I solve this problem?
I'm getting the following errors in my script:
mysqli_connect(): (08004/1040): Too many connections
mysqli_connect(): (HY000/1040): Too many connections
What is the difference and how can I solve this problem?
"Too many connections"
indicates, that your script is opening at least more than one connection to the database. Basically, to one server, only one connection is needed. Getting this error is either a misconfiguration of the server (which I assume isn't the case because max connections = zero isn't an option) or some programming errors in your script.
Check for re-openings of your database connections (mysqli_connect
). There should only be one per script (!) and usually you should take care of reusing open connections OR close them properly after script execution (mysqli_close
)
Steps to resolve that issue:
my.cnf
Run below command to check:
mysql -e "show variables like '%connection%';"
You will see like this:
max_connections = 500
Increase it as per you want:
max_connections = 50000
Restart the MySQL service:
$ service MySQL restart
Now check your website, I hope the error will not occur!
Thank You!
While I could not tell you the difference between the 2 error numbers above, I can tell you what causes this.
Your MySQL database only allows so many connections at the same time. If you connect to MySQL via PHP, then you generally open a new connection every time a page on your site loads. So if you've got too much traffic to your site this can cause this issue.
I think it is pretty common for people to have one connection to their database per page load, and multiple queries for sure. So really what it comes down to are 3 points:
(Let me just tell you now, persistent connections will not solve your issue.)
If you have access to your server's CLI/SSH, try to increase the limit by modifying your MySQL configuration (don't forget to restart the service for changes to take affect). This will of course consume more system resources on your database server.
If you have a lot of AJAX requests or other internal database connections you should try to get these down to a single script with a single call. Your site may make multiple AJAX calls to various PHP files that pulls MySQL data, which uses a whole database connection for each one. Instead, create a single PHP file to collect all the data you need on a given page, this script can get all the data you need while only using 1 database connection.
As far as the difference between the two, I believe that HY000 is a PDO exception where 08004 is actually coming from MySQL. Error 1040 is the code for "Too Many Connections".
You should also check if your disk is full, this can cause the same error:
df -h
will show you the remaining space on each partition, you probably have to check the root partition /
(or /var/ in case you have an extra partition for this):
df -h /