PHP MySQL 设置连接超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9562124/
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
PHP MySQL set Connection Timeout
提问by kb0000
There are certain posts on MySQL connection set time out from PHP using mysql.connect_timeout. I want to know if this set timeout from PHP just time out the initial connection to MySQL or valid for a particular query to database?
有一些关于使用 mysql.connect_timeout 从 PHP 设置 MySQL 连接超时的帖子。我想知道 PHP 的这个设置超时是否只是超时到 MySQL 的初始连接或对数据库的特定查询有效?
My case here is that, I have a page with connection to MySQL on top and then I am executing say 3-4 queries to MySQL one after the another. 1st and 2nd query taken only 1-2 seconds to execute where as 3rd query takes 20 seconds. Now, in cases when 3rd query is taking more than 20 seconds, I want to call time out. So, the question here is that, setting this time out from PHP is applicable to initial connection to database or it is applicable to every subsequent query as well (independently). If later is the case, then how I can set it to timeout after 20 seconds for 3rd query?
我的情况是,我在顶部有一个连接到 MySQL 的页面,然后我一个接一个地执行对 MySQL 的 3-4 个查询。第一个和第二个查询只需要 1-2 秒来执行,而第三个查询需要 20 秒。现在,如果第三个查询花费的时间超过 20 秒,我想调用超时。所以,这里的问题是,从 PHP 设置这个超时适用于到数据库的初始连接,或者它也适用于每个后续查询(独立)。如果以后是这种情况,那么我如何将其设置为 20 秒后第三次查询超时?
采纳答案by dAm2K
The connect_timeout parameter is only valid at connection time. It's useful to check if your DB server is reachable in 20 seconds or so. Once connected the specified timeout is no longer valid.
connect_timeout 参数仅在连接时有效。检查您的数据库服务器是否可在 20 秒左右访问很有用。连接后,指定的超时不再有效。
I don't find any query timeout parameter on official mysql manual page: http://dev.mysql.com/doc/refman/5.5/en/server-system-variables.htmlso I don't think this is possibile.
我在官方 mysql 手册页上找不到任何查询超时参数:http: //dev.mysql.com/doc/refman/5.5/en/server-system-variables.html所以我认为这不可能。
回答by gsdfghs4f5a74a
see it:
看见:
set_time_limit(0);
ini_set('mysql.connect_timeout','0');
ini_set('max_execution_time', '0');
回答by Sheer
sigalarm is your friend. You can set a alarm, and use a signal handler to detect the alarm.
sigalarm 是你的朋友。您可以设置警报,并使用信号处理程序来检测警报。
回答by Laurent PELE
Use MySQL version 5.7.4 or above and set appropriate "max_statement_time" variable, query will fail if timeout is reached
使用 MySQL 5.7.4 或以上版本并设置适当的“max_statement_time”变量,如果超时,查询将失败
see http://mysqlserverteam.com/server-side-select-statement-timeouts/
见http://mysqlserverteam.com/server-side-select-statement-timeouts/
回答by Kareem
It is in your php configuration file. The location of this file is different from one system to the other. On Ubuntu and Debian it is located in /etc/php5/apache2/php.ini
它在您的 php 配置文件中。此文件的位置因一个系统而异。在 Ubuntu 和 Debian 上,它位于 /etc/php5/apache2/php.ini
mysql.connect_timeout = 60 /* the default value is 60 seconds */
回答by Josh Ribakoff
You would have to fork a child process, the parent process will kill the child process if some condition is not met within 20 seconds.
您将不得不 fork 一个子进程,如果在 20 秒内不满足某些条件,父进程将杀死子进程。
Or use a language like node.js that doesn't block the current thread for an IO operation like a query.
或者使用像 node.js 这样的语言,它不会阻止当前线程进行 IO 操作,例如查询。