Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Too many connections error with Laravel 5.4 and Mariadb

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.

like image 545
Fokwa Best Avatar asked May 29 '17 17:05

Fokwa Best


People also ask

How many connections can MariaDB handle?

By default, MariaDB is configured for 150 connections plus one for root access if not already used (so 151 connections). In most cases, 150 is really enough, but it might not be in your case if you've got a lot of connection errors on your application.

Does laravel work with MariaDB?

Currently, Laravel provides first-party support for five databases: MariaDB 10.3+ (Version Policy) MySQL 5.7+ (Version Policy) PostgreSQL 10.0+ (Version Policy)


2 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.

like image 78
Maqsood Muhammad Mujtaba Abro Avatar answered Oct 09 '22 10:10

Maqsood Muhammad Mujtaba Abro


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

like image 2
A Monad is a Monoid Avatar answered Oct 09 '22 10:10

A Monad is a Monoid