PHP銜接MySql閃斷主動重連的辦法。本站提示廣大學習愛好者:(PHP銜接MySql閃斷主動重連的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是PHP銜接MySql閃斷主動重連的辦法正文
應用php作為後台運轉法式(例如短信群發),在cli形式下履行php,php須要銜接mysql輪回履行數據庫處置。
當mysql銜接閃斷時,以後輪回的履行將會掉敗。
我們須要設計一個辦法,當mysql閃斷時,可以主動從新銜接,使前面的法式可以正常履行下去。
1.創立測試數據表
CREATE TABLE `user` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.拔出測試數據
insert into user(name) values('fdipzone'),('xfdipzone'),('terry'); mysql> select * from user; +----+-----------+ | id | name | +----+-----------+ | 1 | fdipzone | | 2 | xfdipzone | | 3 | terry | +----+-----------+
3.後台運轉的php文件
db.php
<?php // 數據庫操作類 class DB{ // 保留數據庫銜接 private static $_instance = null; // 銜接數據庫 public static function get_conn($config){ if(isset(self::$_instance) && !empty(self::$_instance)){ return self::$_instance; } $dbhost = $config['host']; $dbname = $config['dbname']; $dbuser = $config['user']; $dbpasswd = $config['password']; $pconnect = $config['pconnect']; $charset = $config['charset']; $dsn = "mysql:host=$dbhost;dbname=$dbname;"; try { $h_param = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, ); if ($charset != '') { $h_param[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $charset; //設置默認編碼 } if ($pconnect) { $h_param[PDO::ATTR_PERSISTENT] = true; } $conn = new PDO($dsn, $dbuser, $dbpasswd, $h_param); } catch (PDOException $e) { throw new ErrorException('Unable to connect to db server. Error:' . $e->getMessage(), 31); } self::$_instance = $conn; return $conn; } // 履行查詢 public static function query($dbconn, $sqlstr, $condparam){ $sth = $dbconn->prepare($sqlstr); try{ $sth->execute($condparam); } catch (PDOException $e) { echo $e->getMessage().PHP_EOL; } $result = $sth->fetchAll(PDO::FETCH_ASSOC); return $result; } } ?>
test.php
<?php require 'db.php'; // 數據庫設定 $config = array( 'host' => 'localhost', 'dbname' => 'user', 'user' => 'root', 'password' => '', 'pconnect' => 0, 'charset' => '' ); // 輪回履行 while(true){ // 創立數據銜接 $dbconn = DB::get_conn($config); // 履行查詢 $sqlstr = 'select * from user where id=?'; $condparam = array(mt_rand(1,3)); $data = DB::query($dbconn, $sqlstr, $condparam); print_r($data); // 延時10秒 echo 'sleep 10'.PHP_EOL.PHP_EOL; sleep(10); } ?>
4.履行步調
在php cli形式下履行test.php,然後立時履行mysql.server stop 與 mysql.server start 模仿閃斷
mysql.server stop Shutting down MySQL .. SUCCESS! mysql.server start Starting MySQL SUCCESS!
可以看到,閃斷後不克不及從新銜接數據庫,前面的法式不克不及履行下去。
Array ( [0] => Array ( [id] => 3 [name] => terry ) ) sleep 10 SQLSTATE[HY000]: General error: 2006 MySQL server has gone away Array ( ) sleep 10 SQLSTATE[HY000]: General error: 2006 MySQL server has gone away Array ( ) sleep 10 ...
5.增長重連機制
if(isset(self::$_instance) && !empty(self::$_instance)){ return self::$_instance; }
閃斷後,由於 self::$_instance 的值存在,是以挪用get_conn其實不會從新銜接,而是應用保留的銜接停止處置。
如許現實上是當銜接存在時,不須要再次創立mysql銜接,削減mysql銜接數。
所以須要在閃斷後,清空self::$_instance的值,使下次從新獲得銜接,而不應用曾經創立但掉效的數據庫銜接。
改良辦法以下:
增長reset_connect辦法,當湧現毛病時挪用。假如斷定毛病是MySQL server has gone away則清空曾經存在的數據庫銜接,清空後下次則會從新銜接mysql。
修正後的php文件以下:
db.php
<?php // 數據庫操作類 class DB{ // 保留數據庫銜接 private static $_instance = null; // 銜接數據庫 public static function get_conn($config){ if(isset(self::$_instance) && !empty(self::$_instance)){ return self::$_instance; } $dbhost = $config['host']; $dbname = $config['dbname']; $dbuser = $config['user']; $dbpasswd = $config['password']; $pconnect = $config['pconnect']; $charset = $config['charset']; $dsn = "mysql:host=$dbhost;dbname=$dbname;"; try { $h_param = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, ); if ($charset != '') { $h_param[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $charset; //設置默認編碼 } if ($pconnect) { $h_param[PDO::ATTR_PERSISTENT] = true; } $conn = new PDO($dsn, $dbuser, $dbpasswd, $h_param); } catch (PDOException $e) { throw new ErrorException('Unable to connect to db server. Error:' . $e->getMessage(), 31); } self::$_instance = $conn; return $conn; } // 履行查詢 public static function query($dbconn, $sqlstr, $condparam){ $sth = $dbconn->prepare($sqlstr); try{ $sth->execute($condparam); } catch (PDOException $e) { echo $e->getMessage().PHP_EOL; self::reset_connect($e->getMessage()); // 失足時挪用重置銜接 } $result = $sth->fetchAll(PDO::FETCH_ASSOC); return $result; } // 重置銜接 public static function reset_connect($err_msg){ if(strpos($err_msg, 'MySQL server has gone away')!==false){ self::$_instance = null; } } } ?>
6.再次停止閃斷履行
可以看到改良後的後果,閃斷後,以後履行的會掉敗,但以後的可以從新創立新銜接持續履行下去。
Array ( [0] => Array ( [id] => 2 [name] => xfdipzone ) ) sleep 10 SQLSTATE[HY000]: General error: 2006 MySQL server has gone away Array ( ) sleep 10 Array ( [0] => Array ( [id] => 1 [name] => fdipzone ) ) sleep 10 ...
以上所述是小編給年夜家引見的PHP銜接MySql閃斷主動重連的辦法,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!