简体   繁体   English

PHP无法关闭Mysql连接并达到最大连接数

[英]PHP not closing Mysql connections and reaching max connections

I have a site that is about 6 years old, and in the last 6 months, we have been getting the same issue over and over with maxing out the mysql connections. 我有一个大约6年的网站,在过去的6个月中,我们不断地使mysql连接最大化,从而遇到相同的问题。

Now every couple of days the site becomes unavailable because there are no sql connections left. 现在每两天该站点将变得不可用,因为没有剩余的sql连接。 As it's on a shared server, I need to get the host company to flush the connections. 由于它位于共享服务器上,因此我需要让主机公司刷新连接。

Here's what I tried: 这是我尝试过的:

  • Made sure every page now has a close on it 确保每个页面都已关闭
  • Upgraded to PHP7 and converted to mysqli 升级到PHP7并转换为mysqli
  • Pulled my hair out 拉我的头发

Everywhere I look, it says not to use Persistant connections - I am not explicitly using them - and that PHP will automatically release connections after the page completes - hmmm... 我到处看,它说不要使用持久连接-我没有明确地使用它们-并且PHP将在页面完成后自动释放连接-hmmm ...

The host company is usually really helpful, but not over this. 托管公司通常会非常有帮助,但不能超出此范围。 So I have 2 questions... 所以我有2个问题...

  1. What is causing the connections to not close either on my mysqli_close() or on PHP exit? 是什么原因导致连接无法在我的mysqli_close()或PHP出口上关闭? Perhaps pages are aborting or too slow and failing? 也许页面正在中止或太慢而失败?

  2. Why don't the connections close themselves... It can be hours later that I report it to the host company and they flush the connections... Why are they not bring tidied up? 为什么连接不关闭自身……可能要过了几个小时,我才向主机公司报告连接并刷新连接...为什么不整理一下? In the mean time the site is unavailable... 同时,该站点不可用...

Please help.. 请帮忙..

Cris. 危机


Ah... I have just read the host company responses more clearly... (it's been a hectic few days) They say "The issue that is the database server on the shared hosting platform is designed to lock the database once it hits it's maximum allowed connections, so it never closes any connections once it hits 25 simultaneous connections. " 啊...我刚刚看过主机公司的回复,却很清楚...(这已经忙了几天),他们说: “共享主机平台上的数据库服务器的问题是,一旦数据库被击中,它就会锁定数据库。最大允许连接数,因此一旦达到25个同时连接数,它就永远不会关闭任何连接。”

So the question is why am I hitting 25 connections? 所以问题是为什么我要达到25个连接? Is is that I'm holding connections open for slow loading pages and so limiting the number available? 我是否保持连接打开以加载缓慢的页面并因此限制了可用数量?


whenever you open a mysqli connection 每当您打开mysqli连接时

$mysqli = connect_db();

you have to explicitly close it or they linger around and mysql server eventually is unhappy 您必须显式关闭它,否则它们会徘徊,并且mysql服务器最终会感到不满意

$mysqli->close();

While explicitly closing open connections and freeing result sets is optional, doing so is recommended. 虽然显式关闭打开的连接和释放结果集是可选的,但建议这样做。 This will immediately return resources to PHP and MySQL, which can improve performance. 这将立即将资源返回给PHP和MySQL,从而可以提高性能。 http://php.net/manual/en/mysqli.close.php http://php.net/manual/zh/mysqli.close.php

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

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