PHP mysqli 重新连接问题

发布于 2024-08-05 03:58:58 字数 595 浏览 11 评论 0原文

我在 PHP 中使用 mysqli 类时遇到问题,并且无法在任何地方找到答案。

在我的脚本中,一个类创建了一个 mysqli 连接,并在其整个函数中使用该连接。之后,该脚本会分叉。该连接也被孩子们使用,但是当孩子们死亡时,我遇到了父级连接被关闭(MYSQL Server Has Gone Away)的问题。

在我切换到 mysqli 之前(只是使用 mysql),我只是调用 mysql_ping 以确保在父进程中执行查询之前数据库连接已存在。 Mysqli 有类似的 ping 功能,但如果连接消失,它实际上不会重新连接。我尝试使用 mysqli.reconnect=ON 全局设置,但没有成功(使用 php.ini 和 ini_set)。

php mysql_connect 函数允许您获取已存在的连接,因此如果我使用 mysql 而不是 mysqli,我可以在进程分叉后立即重用子级中的连接。但 mysqli 似乎没有任何这样的功能...

我唯一能做的就是调用 mysqli->ping() ,如果返回 false,则重新连接到父级中的数据库。这是非常低效的,我更愿意弄清楚如何使用 mysqli 正确地完成它(并且不需要手动重新连接),而必须改回 mysql..

有什么建议吗?

I am having trouble using the mysqli class in PHP and I haven't been able to find the answer anywhere.

In my script a class creates a mysqli connection that it uses throughout it's functions. Afterward, this script forks. The connection is used by the children as well, but I'm running into the problem of the connection being closed (MYSQL Server Has Gone Away) in the parent when the children die.

Before I switched to mysqli (was just using mysql) I simply called mysql_ping to assure that the db connection was there before performing the query in the parent process. Mysqli has a similar ping function BUT it doesn't actually reconnect if the connection is gone. I tried using the mysqli.reconnect=ON global setting with no luck (using php.ini and ini_set).

The php mysql_connect function allows you to grab an already-existing connection, so if I was using mysql instead of mysqli, I could simply reuse the connection in the child right after the process forked. BUT mysqli doesn't seem to have any such functionality...

The only thing I was able to do was call mysqli->ping() and if that returned false then reconnect to the database in the parent. This is terribly inefficient, and I would much rather figure out how to do it correctly with mysqli (and no need for manual reconnects) that having to change back to mysql..

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

英雄似剑 2024-08-12 03:58:58

mysqli_ping() 的文档说,如果您将全局选项 mysqli.reconnect 设置为 1(在 php.ini 中),则 mysqli_ping() > 当检测到连接消失时将重新连接。

更新:自从我在 2009 年回答这个问题以来,PHP 大多数情况下都默认使用 mysqlnd 驱动程序,而不是 libmysql。正如下面的评论中提到的,mysqlnd 不支持 mysqli_ping() 函数,PHP 开发人员是故意这样做的,根据他们的“Won't Fix”回答https://bugs.php.net/bug.php?id=52561

所以 PHP 中似乎没有自动重新连接的解决方案了。

The doc for mysqli_ping() says that if you set the global option mysqli.reconnect to 1 (in your php.ini) then mysqli_ping() will reconnect when it detects the connection has gone away.

Update: Since I answered this question in 2009, PHP has mostly moved on to use the mysqlnd driver by default instead of libmysql. As mentioned in the comment below, mysqlnd does not support the mysqli_ping() function, and the PHP developers did this intentionally, according to their "Won't Fix" answer in https://bugs.php.net/bug.php?id=52561

So there appears to be no solution for automatic reconnect in PHP anymore.

破晓 2024-08-12 03:58:58

你可以尝试一些不同的东西..而不是ping..尝试发送一个非常简单的低强度查询,如“select now()”,看看是否能得到更好的结果。

u can try something a bit different .. instead of ping .. try to send a really simple low intensity query like "select now()" and see if you get any better results.

最终幸福 2024-08-12 03:58:58
function IsConnected() {
    if (empty($this->_connectionID) === FALSE && mysqli_ping($this->_connectionID) === TRUE) {
        return true; // still have connect
    }
    $this->_connectionID = false;
    return false; // lose connection
}
function IsConnected() {
    if (empty($this->_connectionID) === FALSE && mysqli_ping($this->_connectionID) === TRUE) {
        return true; // still have connect
    }
    $this->_connectionID = false;
    return false; // lose connection
}
好菇凉咱不稀罕他 2024-08-12 03:58:58

使用 http://www.php.net/manual/en/mysqli .real-connect.php ...重新安装 php 并检查 php.ini 设置

Use http://www.php.net/manual/en/mysqli.real-connect.php ... reinstall php and check your php.ini settings

云朵有点甜 2024-08-12 03:58:58

我认为你需要在 my.cng 中设置 mysqli.reconnect,这是 mysql 配置,而不是 php.ini,我无法通过 ini_set 更改重新连接,但我的系统管理员在 my.cnf 中做到了。

所以,有点困惑其他人已经在 php.ini 中完成了它......

I think you need to set mysqli.reconnect in my.cng, which is the mysql config, rather than php.ini, I couldn't manage to change reconnect via ini_set, but my sys admin did it in my.cnf.

So, tad confused that others have done it within php.ini....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文