简体   繁体   English

与Laravel 5.4和Mariadb的连接错误太多

[英]Too many connections error with Laravel 5.4 and Mariadb

I have recently upgraded my laravel app from 5.1 to 5.4. 我最近将我的laravel应用程序从5.1升级到5.4。 My server is now on PHP 7 and Maria db 10.1.22. 我的服务器现在是PHP 7和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 . 有时候,我会尝试刷新浏览器,PHPMyAdmin会显示#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. 上面的命令给出了100.然后我增加到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. 这对我来说很奇怪,因为我在另一台服务器上安装了oracle mysql 5.6并且max_connections是151,并且超过一年半我没有遇到过这个错误。

When I run the command show processlist , I get the result below and this keeps increasing. 当我运行命令show processlist ,我得到下面的结果,并且这种情况不断增加。 is this normal? 这是正常的吗?

在此输入图像描述

What could be the issue with Mariadb and how can I fix it. 可能是Mariadb的问题,我该如何解决它。

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. 当我们将laravel应用程序从5.2 - 5.4升级时,我们错过了一些重要的东西。 php artisan queue:work has been upgraded as well. php artisan队列:工作也已升级。

In laravel 5.2, when we execute queue:work. 在laravel 5.2中,当我们执行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. 它只是一次处理一个作业,但是在laravel 5.4队列中:work为队列创建连接并保持连接。 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. 因此,如果您有队列:在您的cron中工作以便每分钟运行一次,它将每分钟创建一个新连接,这就是我们得到太多连接错误的原因。

I did not find this information on laravel 5.4 documentation. 我没有在laravel 5.4文档中找到这些信息。 I was going through php artisan to see the description of queue:work and found out that the description is changed. 我正在通过php artisan来查看queue:work的描述并发现描述已更改。

This is description in laravel 5.2: 这是laravel 5.2中的描述:

queue:work >>>> Process the next job on a queue queue:work >>>>处理队列中的下一个作业

This is description in laravel 5.4 这是laravel 5.4中的描述

queue:work >>>> Start processing jobs on the queue as a daemon queue:work >>>>开始将队列上的作业作为守护进程处理

As you can see the difference in what they are meant to do. 正如您所看到的那样,它们的意义不同。

I hope that this helps you out. 我希望这可以帮助你。

Thanks. 谢谢。

Maybe it is a silly observation, but did you use DB::disconnect('foo') to closes the conenctions of your db? 也许这是一个愚蠢的观察,但你是否使用DB::disconnect('foo')来关闭数据库的连接?

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 . 监控MySQL的一个非常强大的工具是innotop You can find it here: 你可以在这里找到它:

https://github.com/innotop/innotop https://github.com/innotop/innotop

Check this 检查一下

What webserver are you using. 你在用什么网络服务器 If Apache, then what is the setting of MaxRequestWorkers (formerly called MaxClients ) for it? 如果是Apache,那么MaxRequestWorkers (以前称为MaxClients )的设置是什么呢? Generally it should be no more than about 20. 一般来说,它应该不超过20。

If that is set to more than max_connections , that would explain what you are seeing. 如果设置为超过max_connections ,那将解释您所看到的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM