Home php教程 php手册 PHP访问MySQL查询超时处理

PHP访问MySQL查询超时处理

Jun 13, 2016 am 10:38 AM
mysql php host use deal with supply Inquire of access time out connect

PHP连接MySQL主要是使用Mysql提供的 libmysqlclient 的客户端库,同时也延伸出来 mysql 和  mysqli 两套PHP的扩展,相对来说 mysqli 比 mysql 更好,更稳定。

目前两个客户端扩展库连接超时可以设置选项来操作,比如mysqli:

//创建对象
$mysqli = mysqli_init();

//设置超时选项
$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);

//连接
$mysqli->real_connect(localhost, my_user, my_password, world);

//如果超时或者其他连接失败打印错误信息
if (mysqli_connect_errno()) {
    printf("Connect failed: %s ", mysqli_connect_error());
    exit();
}
//成功输出连接信息
printf ("Connection: %s .", $mysqli->host_info);

$mysqli->close();
?>

这个是连接超时,但是有些时候我们需要查询读写超时,比如说我们一个数据库压力很大,或者连接很多,那么数据库查询就很缓慢,但是我希望某些不重要的数据,比如说文章点击数这种如果查询超时了就不显示,至少能够保证主体页面正确显示,但是查遍PHP手册没有发现这个操作选项或者函数。

手册里只有这么四个选项

 

跟踪 mysqli 的扩展源代码发现它底层调用的是 libmysqlclient 的 mysql_options:

php-5.2.8/ext/mysqli/mysqli_api.c

 

并且在mysqli的PHP扩展中就只导出了几个变量:

php-5.2.8/ext/mysqli/mysqli.c

 

大概看了一下 libmysqlclient 的代码,发现其实它自带是有读写超时设置的:

mysql-5.1.30/sql-common/client.c

 

因为它自己定义了很多操作选项,只是php扩展里没有:

mysql-5.1.30/include/mysql.h

 

看看mysql中的读写超时是如何实现的:

mysql-5.1.30/sql-common/client.c

 

 

读写超时真正操作的地方,超时处理这里重试了两次,还是写死了:


mysql-5.1.30/sql/net_serv.cc

 

 

 

 

 

现在基本得出了结论:


按照上面查看代码来看,目前PHP针对MySQL查询超时以下限制:

1. 超时设置单位为秒,最少配置1秒

2. 但mysql底层的read会重试两次,所以实际会是 3 秒

 

重试两次 + 自身一次 = 3倍超时时间。

 

那么就是说最少超时时间是3秒,不会低于这个值,对于大部分应用来说可以接受,但是对于小部分应用需要优化。

 

 


 


现在我们来看看如果我们自己要设置超时,我们自己压入 MYSQL_OPT_READ_TIMEOUT 也是可以达到读写超时效果的,写一段代码来测试一下:

 


//自己定义读写超时常量
if (!defined(MYSQL_OPT_READ_TIMEOUT)) {
        define(MYSQL_OPT_READ_TIMEOUT,  11);
}
if (!defined(MYSQL_OPT_WRITE_TIMEOUT)) {
        define(MYSQL_OPT_WRITE_TIMEOUT, 12);
}

//设置超时
$mysqli = mysqli_init();
$mysqli->options(MYSQL_OPT_READ_TIMEOUT, 3);
$mysqli->options(MYSQL_OPT_WRITE_TIMEOUT, 1);

//连接数据库
$mysqli->real_connect("localhost", "root", "root", "test");
if (mysqli_connect_errno()) {
   printf("Connect failed: %s ", mysqli_connect_error());
   exit();
}

//执行查询 sleep 1秒不超时
printf("Host information: %s ", $mysqli->host_info);
if (!($res=$mysqli->query(select sleep(1)))) {
    echo "query1 error: ". $mysqli->error ." ";
} else {
    echo "Query1: query success ";
}

//执行查询 sleep 9秒会超时
if (!($res=$mysqli->query(select sleep(9)))) {
    echo "query2 error: ". $mysqli->error ." ";
} else {
    echo "Query2: query success ";
}

$mysqli->close();
echo "close mysql connection ";
?>


查看上面代码的执行结果,验证了上面的观点,第一个查询成功了,第二个查询连接被断开了:

 

 

如果需要修改这个秒级别的超时,比如改成毫秒级别的超时,只能两个地方修改:

 

1.  修改客户端,比如 mysqli 的 query 代码,加入定时器,超时则返回

2.  修改 Mysql 中的vio代码,因为mysql的网络处理底层都是经过vio的操作

 

MySQL相关的vio代码:


poll 超时:

 


setsockopt 超时:

 

 


基本上到这里就基本能够解决PHP在针对MySQL读写查询操作超时的处理了,希望对你有帮助。

 

aspx"> 

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Top 10 PHP CMS Platforms For Developers in 2024 Top 10 PHP CMS Platforms For Developers in 2024 Dec 05, 2024 am 10:29 AM

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content

The page is blank after PHP is connected to MySQL. What is the reason for the invalid die() function? The page is blank after PHP is connected to MySQL. What is the reason for the invalid die() function? Apr 01, 2025 pm 03:03 PM

The page is blank after PHP connects to MySQL, and the reason why die() function fails. When learning the connection between PHP and MySQL database, you often encounter some confusing things...

See all articles