我在我的一个 PHP 脚本中遇到了类似的东西,我在执行慢查询之前添加了这个内联:
$timeout_seconds = 3153600; // 1 year...
// Make sure the PHP script doesn't time out
set_time_limit(0);
ignore_user_abort(1);
// Make sure the PHP socket doesn't time out
ini_set('default_socket_timeout', $timeout_seconds);
ini_set('mysqlnd.net_read_timeout', $timeout_seconds);
// Make sure the MySQL server doesn't time out
// Assuming your $link is a MySQLi object:
$link->query("SET SESSION connect_timeout=" . $timeout_seconds);
$link->query("SET SESSION delayed_insert_timeout=" . $timeout_seconds);
$link->query("SET SESSION have_statement_timeout='NO'");
$link->query("SET SESSION net_read_timeout=" . $timeout_seconds);
$link->query("SET SESSION net_write_timeout=" . $timeout_seconds);
显然,您需要适当地设置秒数,但我根本不希望我的脚本超时。
PHP 初始化指令:https ://www.php.net/manual/en/ini.list.php
MySQL 服务器变量:https ://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html
注意:查看文档以获取更多信息,并验证变量是否与您使用的 PHP 和 MySQL 版本匹配。我正在使用 PHP 7.3 和 MySQL 5.7。
** 编辑:设置 PHP 超时对于我的脚本来说还不够,我还必须添加 MySQL SESSION 变量。