PHP mysqli 重新连接问题
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
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) thenmysqli_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=52561So there appears to be no solution for automatic reconnect in PHP anymore.
你可以尝试一些不同的东西..而不是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.
使用 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
我认为你需要在 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....