PHP MySQL sets connection timeout

There are specific messages about the MySQL-PHP connection time using mysql.connect_timeout. I want to know if this timeout is set from PHP only when initially connecting to MySQL or is it valid for a specific database query?

My thing is that I have a page with connecting to MySQL at the top, and then I do 3-4 queries to MySQL one by one. The 1st and 2nd requests are completed in just 1-2 seconds to execute when the third request takes 20 seconds. Now that the third request takes more than 20 seconds, I want to call a timeout. So, the question is that setting this time from PHP is applicable to the initial connection to the database or applicable to each subsequent request (independently). If so, how can I set it to timeout after 20 seconds for the third request?

+7
source share
6 answers

The connect_timeout parameter is valid only during connection. It is useful to check if your database server is accessible in 20 seconds or so. Once connected, the specified timeout is no longer valid.

I cannot find the request timeout parameter on the official mysql manual page: http://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html , so I don’t think it is possible.

+4
source

see it:

set_time_limit(0); ini_set('mysql.connect_timeout','0'); ini_set('max_execution_time', '0'); 
+4
source

Sigalarm is your friend. You can set an alarm and use a signal handler to detect an alarm.

http://php.net/manual/en/function.pcntl-alarm.php

+3
source

Use MySQL version 5.7.4 or higher and set the corresponding variable "max_statement_time", the request will fail if the timeout is reached

see http://mysqlserverteam.com/server-side-select-statement-timeouts/

+2
source

This is the php configuration file. The location of this file is different from one system to another. On Ubuntu and Debian, it is in / etc / php 5 / apache2 / php.ini

 mysql.connect_timeout = 60 /* the default value is 60 seconds */ 
+1
source

You will have to split the child process, the parent process will kill the child process if some condition is not met within 20 seconds.

Or use a language like node.js that does not block the current thread for an I / O operation, like a request.

0
source

All Articles