Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of automatic reconnection of PHP connection MySql flash break

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Use php as the background running program (such as mass SMS). To execute php,php in cli mode, you need to connect to the mysql loop to perform database processing.

When the mysql connection is broken, the subsequent execution of the loop will fail.

We need to design a method that can automatically reconnect when the mysql is broken, so that the later programs can be executed normally.

1. Create a test data table

CREATE TABLE `user` (`id` int (11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar (20) NOT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8

two。 Insert test data

Insert into user (name) values ('fdipzone'), (' xfdipzone'), ('terry'); mysql > select * from user;+----+-+ | id | name | +-+-+ | 1 | fdipzone | | 2 | xfdipzone | | 3 | terry | +-+-+

3. Php files running in the background

Db.php

Test.php

4. Execution steps

Execute test.php in php cli mode, and then immediately perform mysql.server stop and mysql.server start simulation flash

Mysql.server stopShutting down MySQL.. SUCCESS!mysql.server startStarting MySQLSUCCESS!

As you can see, the database cannot be reconnected after the flash break, and the following programs cannot be executed.

Array ([0] = > Array ([id] = > 3 [name] = > terry)) sleep 10SQLSTATE [HY000]: General error: 2006 MySQL server has gone awayArray () sleep 10SQLSTATE [HY000]: General error: 2006 MySQL server has gone awayArray () sleep 10.

5. Increase reconnection mechanism

If (isset (self::$_instance) & &! empty (self::$_instance)) {return self::$_instance;}

After the flash break, because the value of self::$_instance exists, calling get_conn does not reconnect, but uses the saved connection for processing.

This actually means that when a connection exists, there is no need to create a mysql connection again, reducing the number of mysql connections.

So you need to clear the value of self::$_instance after the flash, so that you can get the connection again next time, instead of using the database connection that has been created but failed.

The improvement methods are as follows:

Add the reset_connect method, which is called when an error occurs. If the error is MySQL server has gone away, the existing database connection is cleared, and the mysql will be reconnected next time after emptying.

The modified php file is as follows:

Db.php

6. Flash execution again

You can see the effect of the improvement. After the flash, the current execution will fail, but the later can recreate the new connection and continue to execute.

Array ([0] = > Array ([id] = > 2 [name] = > xfdipzone) sleep 10SQLSTATE [HY000]: General error: 2006 MySQL server has gone awayArray () sleep 10Array ([0] = > Array ([id] = > 1 [name] = > fdipzone)) sleep 10.

Simultaneous release of articles: https://www.geek-share.com/detail/2683442734.html

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report