Laravel 5.4 和 Mariadb 的连接错误太多
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44248036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Too many connections error with Laravel 5.4 and Mariadb
提问by Fokwa Best
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:
我最近将我的 Laravel 应用程序从 5.1 升级到 5.4。我的服务器现在使用 PHP 7 和 Maria db 10.1.22。现在我遇到了一个非常奇怪的错误。有时,我会尝试刷新浏览器,PHPMyAdmin 会显示#1040 - Too many connections
. 其他时候我会点击我的应用程序上的一个新链接,然后会得到同样的错误。我在互联网上做了一些研究并执行了以下命令:
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. is this normal?
当我运行命令时show processlist
,我得到下面的结果,并且这个结果不断增加。这是正常的吗?
What could be the issue with Mariadb and how can I fix it.
Mariadb 可能存在什么问题,我该如何解决。
回答by Maqsood Muhammad Mujtaba Abro
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.
我不确定你们是否找到了解决方案,但我也面临同样的问题并找到了解决方案。当我们将 laravel 应用程序从 5.2 升级到 5.4 时,我们错过了一些重要的东西。php artisan queue:work 也升级了。
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.
在 laravel 5.2 中,当我们执行 queue:work 时。它一次只处理一项工作,但在 laravel 5.4 queue:work 为您的队列创建一个连接并保持连接。因此,如果您有 queue:work 在您的 cron 中每分钟运行一次,它将每分钟创建一个新连接,这就是我们收到连接过多错误的原因。
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.
我没有在 laravel 5.4 文档中找到这些信息。我正在通过 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.
谢谢。
回答by Damián Rafael Lattenero
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
. You can find it here:
监视 MySQL 的一个非常强大的工具是innotop
. 你可以在这里找到它:
https://github.com/innotop/innotop
https://github.com/innotop/innotop
Check this
检查这个
回答by Rick James
What webserverare 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.
你用的是什么网络服务器。如果是 Apache,那么MaxRequestWorkers(以前称为MaxClients
)的设置是什么?一般不超过20个。
If that is set to more than max_connections
, that would explain what you are seeing.
如果设置为 more than max_connections
,这将解释您所看到的内容。