PHP mysqli重新连接问题 [英] PHP mysqli reconnect problem

查看:466
本文介绍了PHP mysqli重新连接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中使用mysqli类时遇到麻烦,而且无法在任何地方找到答案.

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

在我的脚本中,一个类创建一个mysqli连接,该连接在整个函数中都使用.之后,此脚本分叉.子级也使用该连接,但是当子级死亡时,我遇到了父级关闭连接(MYSQL Server已消失)的问题.

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.

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

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

php mysql_connect函数允许您获取一个已经存在的连接,因此,如果我使用的是mysql而不是mysqli,则可以在分支过程之后立即在子级中重新使用该连接.但是mysqli似乎没有任何此类功能...

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

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

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

有什么建议吗?

推荐答案

mysqli_ping()的文档说,如果将全局选项 mysqli.reconnect 设置为1(在php.ini中)然后mysqli_ping()将在检测到连接断开时重新连接.

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.

更新:自从我在2009年回答这个问题以来,PHP大多默认使用默认的 mysqlnd 驱动程序,而不是 libmysql .如下面的注释中所述, mysqlnd 不支持mysqli_ping()函数,PHP开发人员根据

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

因此,似乎不再有PHP中自动重新连接的解决方案.

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

这篇关于PHP mysqli重新连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆