PHP implements query timeout method for accessing MySQL: 1. Modify the client, such as the query code of mysqli, add a timer, and return when timeout occurs; 2. Modify the vio code in Mysql, because the underlying network processing of mysql All are operated by vio.
【Related learning recommendations: mysql tutorial】
PHP implements the query timeout method for accessing MySQL:
In libmysql, the MYSQL_OPT_READ_TIMEOUT
setting item is provided, and libmysql provides an API for setting related setting items. , mysql_options:
Currently only mysqli supports
<?php $mysqli = mysqli_init(); $mysqli->options(11 /*MYSQL_OPT_READ_TIMEOUT*/, 1); $mysql->real_connect(***);
However, because there is a retry mechanism in libmysql (try once, retry twice), so in the end the timeout threshold we set will be three Times the value we set.
In other words, if we set MYSQL_OPT_READ_TIMEOUT to 1, it will eventually time out after 3 seconds. In other words, the shortest timeout we can currently set is 3 seconds...
1. Modify the client, such as the query code of mysqli, add a timer, and return
2 when timeout occurs. 2. Modify the vio code in Mysql, because the bottom layer of mysql's network processing is operated by vio
If you want to learn more about it, please pay attention to the php training column!
The above is the detailed content of How does PHP implement query timeout when accessing MySQL?. For more information, please follow other related articles on the PHP Chinese website!