Home » Php » php – Too many connections error with Laravel 5.4 and Mariadb

php – Too many connections error with Laravel 5.4 and Mariadb

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have 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 weird error. Sometimes, I would just try to refresh my browser and PHPMyAdmin would display #1040 - Too many connections. Other times I would click on a new link on my app and then would get this same error. I did some research on the internet and executed the command below:

show variables like 'max_connections`

The above command gave 100. Which I then increase to 500.

But now I still get the error. This is weird to me because I have oracle mysql 5.6 installed on another server and max_connections is 151 and for over a year and a half I have not experienced this error.

When I run the command show processlist, I get the result below and this keeps increasing. is this normal?

enter image description here

What could be the issue with Mariadb and how can I fix it.

How to&Answers:

I am not sure if you guys found the solution or not but I was facing the same problem as well and found the solution. As we upgraded our laravel application from 5.2 – 5.4, we missed something important. php artisan queue:work has been upgraded as well.

In laravel 5.2, when we execute queue:work. It just process one job at a time but in laravel 5.4 queue:work creates a connection for your queue and keeps it connected. So if you have queue:work in your cron to run every minute it will create a new connection every minute and that is why we get the error of too many connections.

I did not find this information on laravel 5.4 documentation. I was going through php artisan to see the description of queue:work and found out that the description is changed.

This is description in laravel 5.2:

queue:work >>>> Process the next job on a queue

This is description in laravel 5.4

queue:work >>>> Start processing jobs on the queue as a daemon

As you can see the difference in what they are meant to do.

I hope that this helps you out.

Thanks.

Answer:

Maybe it is a silly observation, but did you use DB::disconnect('foo') to closes the conenctions of your db?

Anyway, the db closes automatically, so the problem may be somewhere else, did you try monitoring?

A very powerful tool to monitor MySQL is innotop. You can find it here:

https://github.com/innotop/innotop

Check this

Answer:

What webserver are you using. If Apache, then what is the setting of MaxRequestWorkers (formerly called MaxClients) for it? Generally it should be no more than about 20.

If that is set to more than max_connections, that would explain what you are seeing.