如何防止 php 脚本因长时间的 mysql 查询而超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/365496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to keep a php script from timing out because of a long mysql query
提问by Eric Goodwin
I have an update query being run by a cron task that's timing out. The query takes, on average, five minutes to execute when executed in navicat.
我有一个由超时的 cron 任务运行的更新查询。在 navicat 中执行时,查询平均需要 5 分钟才能执行。
The code looks roughly like this. It's quite simple:
代码大致如下。这很简单:
// $db is a mysqli link
set_time_limit (0); // should keep the script from timing out
$query = "SLOW QUERY";
$result = $db->query($query);
if (!$result)
echo "error";
Even though the script shouldn't timeout, the time spent waiting on the sql call still seems to be subject to a timeout.
即使脚本不应该超时,等待 sql 调用所花费的时间似乎仍然受到超时的影响。
Is there an asynchronous call that can be used? Or adjust the timeout?
有没有可以使用的异步调用?或者调整超时时间?
Is the timeout different because it's being called from the command line rather than through Apache?
超时是否不同,因为它是从命令行而不是通过 Apache 调用的?
Thanks
谢谢
回答by Karsten
I had the same problem somwhere, and "solved" it with the following code (first two lines of my file):
我在某处遇到了同样的问题,并使用以下代码(我的文件的前两行)“解决”了它:
set_time_limit(0);
ignore_user_abort(1);
回答by JW.
According to the manual:
根据手册:
Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running.
注意: set_time_limit() 函数和配置指令 max_execution_time 只影响脚本本身的执行时间。在确定脚本运行的最长时间时,不包括在脚本执行之外发生的活动(例如使用 system() 的系统调用、流操作、数据库查询等)上花费的任何时间。
So it's unlikely to have anything to do with PHP's time limit. What message are you getting when it times out? Perhaps there's a MySQL setting involved.
所以它不太可能与 PHP 的时间限制有任何关系。超时时您会收到什么消息?也许涉及到 MySQL 设置。
回答by Tuminoid
Is your php running in safe-mode? Quote from PHP manual of set_time_limit:
您的 php 是否在安全模式下运行?引用自set_time_limit 的 PHP 手册:
This function has no effect when PHP is running in safe mode. There is no workaround other than turning off safe mode or changing the time limit in the php.ini.
当 PHP 在安全模式下运行时,此功能无效。除了关闭安全模式或更改 php.ini 中的时间限制外,没有其他解决方法。
回答by epochwolf
Assuming you are on linux, Debian based systems have separate configurations for mod_php/php cgi and php-cli. This shouldn't be too difficult to set up on a different linux system that doesn't separate cgi/cli configuration.
假设您使用的是 linux,基于 Debian 的系统对 mod_php/php cgi 和 php-cli 有单独的配置。在不分离 cgi/cli 配置的不同 linux 系统上进行设置应该不会太难。
Once you have separate configs, I would adjust your php cli configuration. Disable safe mode and any time limits and ram limits.
一旦你有单独的配置,我会调整你的 php cli 配置。禁用安全模式和任何时间限制和 ram 限制。
回答by barfoon
Check out some of the resource limit variables in php,ini: max_execution_time, max_input_time, memory_limit
查看php,ini中的一些资源限制变量:max_execution_time、max_input_time、memory_limit
You could also set a time limit for the script in PHP itself: http://ca3.php.net/set_time_limit
您还可以在 PHP 本身中为脚本设置时间限制:http: //ca3.php.net/set_time_limit

