Home > Backend Development > PHP Problem > How does PHP implement query timeout when accessing MySQL?

How does PHP implement query timeout when accessing MySQL?

coldplay.xixi
Release: 2023-03-05 09:32:01
Original
2886 people have browsed it

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.

How does PHP implement query timeout when accessing MySQL?

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(***);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template