Too many connection errors with Laravel 5.4 and Mariadb
I recently upgraded my laravel app from 5.1 to 5.4. My server is now on PHP 7 and Maria db 10.1.22. Now I am getting a really strange error. Sometimes I just try to refresh my browser and PHPMyAdmin will display #1040 - Too many connections
. In other cases, I would click on a new link in my application and then get the same error. I did some research online and ran the below command:
show variables like 'max_connections`
The command above gave 100. Which I then increase to 500.
But now I am still getting the error. This is strange to me because I have mysql 5.6 oracle installed on another server and max_connections is 151 and have not experienced this error for over a year and a half.
When I run the command show processlist
, I get the result below and it keeps growing. is it normal?
What could be the problem with Mariadb and how can I fix it.
source to share
I'm not sure if you found a solution or not, but I faced the same problem and found a solution. When we updated our laravel app from 5.2 to 5.4, we were missing something important. queue php artisan: work has also been updated.
In laravel 5.2, when we execute the queue: work. It just processes one job at a time, but in laravel 5.4 queue: job creates a connection for your queue and maintains it. So if you have a queue: work in your cron to run every minute, it will create a new connection every minute, which is why we are getting too many connections error.
I didn't find this information in the laravel 5.4 documentation. I went through php artisan to see the description of the queue: work and found out that the description was changed.
This is the description in laravel 5.2:
queue: work → → Process the next job in the queue
This is the description in laravel 5.4
queue: work → → Start processing jobs in the queue as a daemon
As you can see, the difference is in what they should do.
I hope this helps you.
Thank.
source to share
Maybe this is a silly observation, but have you used DB::disconnect('foo')
to close the cones of your db?
Anyway, the db is automatically closed, so the problem might be somewhere else, have you tried to control?
A very powerful MySQL monitoring tool - innotop
. You can find it here:
https://github.com/innotop/innotop
Check it out
source to share
What web server are you using. If Apache, then what is the value of MaxRequestWorkers (formerly called MaxClients
) for it? Usually this should be no more than 20.
If this value is greater than max_connections
, it explains what you are seeing.
source to share