什么会导致 PHP 忽略 mysqli.reconnect 设置? [英] What would cause PHP to ignore mysqli.reconnect setting?

查看:82
本文介绍了什么会导致 PHP 忽略 mysqli.reconnect 设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有几个不同的服务器运行相同的 PHP 脚本,它们都使用 PHP mysqli 连接/函数,我们注意到,在一台新服务器上,我们开始收到许多 MYSQL Gone Away 错误.

We have several different servers running the same PHP scripts, all of which use PHP mysqli connections/functions, and we noticed that, on one new server, we started getting many MYSQL Gone Away errors.

wait_timeout 在 MYSQL 中的所有服务器上都设置为 300 秒,这是在此特定服务器上断开连接之前所需的时间长度(即在查询之间等待 301 秒)下面的代码会产生错误,但 299 秒不会).

wait_timeout in MYSQL is set to 300 seconds on all servers, and this is the length of time it takes before the connection drops on this particular server (i.e. a wait of 301 seconds between queries in the code below produces the error, but 299 seconds does not).

然而,所有服务器也将 mysqli.reconnect 设置为 1(开启).根据文档,mysqli.reconnect 应该意味着断开的连接会自动重新连接,但绝对不是.

However, all servers also have mysqli.reconnect set to 1 (On). According to documentation, mysqli.reconnect should mean that the dropped connection is reconnected automatically, however it definitely isn't.

这是我运行的代码片段,用于演示该问题.它适用于除此特定服务器之外的所有服务器:

Here is a code snippet that I've run that demonstrates the issue. It works on all servers except this particular server:

$mysqli = new mysqli('ip', 'username', 'pass', 'db');
if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}

$query = "SELECT * FROM table LIMIT 1";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
sleep(301);
$query = "SELECT * FROM table LIMIT 1";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
mysqli_close($mysqli);

print "Finished\n";
exit;

我还重新编写了测试脚本以使用 mysqli_ping()(因为文档指出如果 mysqli.reconnect 设置为 1,此函数应自动重新连接),但是这仍然没有重新连接,第二次查询总是报错,MySQL 已经消失了.

I have also re-written the test script to use mysqli_ping() (since the documentation states that this function should reconnect automatically if mysqli.reconnect is set to 1), however this still does not reconnect, and the second query always produces the error that MySQL has gone away.

所有服务器运行的 PHP 版本略有不同.出现故障的服务器运行的是5.3.21,其他服务器运行的是5.3.0和5.3.10.

All servers are running slightly different versions of PHP. The server that fails is running 5.3.21, other servers are running 5.3.0 and 5.3.10.

推荐答案

原来问题出在 MYSQLND 驱动程序上.我们的服务提供商基本上重新编译了 PHP,这解决了问题.

Turned out the issue was with the MYSQLND driver. Our service provider basically recompiled PHP, and this resolved the issue.

这篇关于什么会导致 PHP 忽略 mysqli.reconnect 设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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