PHP connection MySQL is mainly using MySQL provided by the Libmysqlclient client library, but also extends out of MySQL and mysqli two sets of PHP extensions, relatively speaking mysqli better than MySQL, more stable.
Currently two client extension Library connection timeouts can be set with the option to operate, such as mysqli:
Creating objects
$mysqli = Mysqli_init ();
Setting the timeout option
$mysqli->options (Mysqli_opt_connect_timeout, 5);
Connection
$mysqli->real_connect (localhost, my_user, My_password, World);
Print error message if timeout or other connection failure
if (Mysqli_connect_errno ()) {
printf ("Connect failed:%s", Mysqli_connect_error ());
Exit ();
}
Successful output connection information
printf ("Connection:%s.", $mysqli->host_info);
$mysqli->close ();
?>
This is the connection timeout, but sometimes we need to query read-write timeout, such as our database is very stressful, or a lot of connections, then the database query is very slow, but I hope that some unimportant data, such as the article hits the number of this if the query timed out, do not display, at least to ensure that the main page display correctly, However, the PHP manual does not find this operation option or function.
There are only four options in this manual.
The extension source code that tracks Mysqli discovers that it calls the Libmysqlclient mysql_options:
Php-5.2.8/ext/mysqli/mysqli_api.c
And only a few variables were exported in the PHP extension of mysqli:
Php-5.2.8/ext/mysqli/mysqli.c
Probably looked at the Libmysqlclient code, found that it actually comes with a read-write timeout setting:
Mysql-5.1.30/sql-common/client.c
Because it defines a number of operating options for itself, it is not in the PHP extension:
Mysql-5.1.30/include/mysql.h
See how the read-write timeout in MySQL is implemented:
Mysql-5.1.30/sql-common/client.c
Read-write timeout where the real operation, timeout processing here retry two times, or write dead:
mysql-5.1.30/sql/net_serv.cc
Now the basic conclusion is:
As seen in the code above, currently PHP time-out for MySQL query timeout the following restrictions:
1. Time-out setting is seconds, minimum configuration is 1 seconds
2. But MySQL's underlying read will retry two times, so it will actually be 3 seconds.
Retry two times + self once = 3 times times the time-out.
So the minimum time-out is 3 seconds, not below this value, which is acceptable for most applications, but optimized for a small subset of applications.
Now let's see if we're going to set the timeout ourselves, and we can get the read-write timeout effect on our own mysql_opt_read_timeout, write a code to test it:
Self-defined read/write super-constant amount
if (!defined (mysql_opt_read_timeout)) {
Define (Mysql_opt_read_timeout, 11);
}
if (!defined (mysql_opt_write_timeout)) {
Define (Mysql_opt_write_timeout, 12);
}
Set timeout
$mysqli = Mysqli_init ();
$mysqli->options (Mysql_opt_read_timeout, 3);
$mysqli->options (mysql_opt_write_timeout, 1);
Connecting to a database
$mysqli->real_connect ("localhost", "root", "root", "test");
if (Mysqli_connect_errno ()) {
printf ("Connect failed:%s", Mysqli_connect_error ());
Exit ();
}
Execute query Sleep 1 seconds does not time out
printf ("Host Information:%s", $mysqli->host_info);
if (! ( $res = $mysqli->query (select Sleep (1))) {
echo "Query1 error:". $mysqli->error. "";
} else {
echo "Query1:query success";
}
Execute query Sleep 9 seconds Time Out
if (! ( $res = $mysqli->query (select Sleep (9))) {
echo "Query2 error:". $mysqli->error. "";
} else {
echo "Query2:query success";
}
$mysqli->close ();
echo "Close MySQL connection";
?>
Check the execution result of the above code, verify the above view, the first query succeeds, the second query connection is broken:
If you need to modify this second-level timeout, such as changing to a millisecond-level timeout, only two places can be modified:
1. Modify the client, such as the query code of the MYSQLI, add the timer, and time out to return
2. Modify the vio code in MySQL, because MySQL's network processing base is vio operation
MySQL-related vio code:
Poll Timeout:
SetSockOpt timeout:
Basically here to solve the PHP read-write query operation timeout processing, I hope it will help you.
ASPX ">
http://www.bkjia.com/PHPjc/486260.html www.bkjia.com true http://www.bkjia.com/PHPjc/486260.html techarticle PHP connection MySQL is mainly using MySQL provided by the Libmysqlclient client library, but also extended out of MySQL and mysqli two sets of PHP extensions, relatively speaking mysqli better than MySQL, more stable ...