Heim  >  Artikel  >  Backend-Entwicklung  >  用MySQL内建复制来最佳化可用性(六)_PHP教程

用MySQL内建复制来最佳化可用性(六)_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:22:02709Durchsuche

第五步:一个改进的数据库连接程序
$#@60;?php
/********************************************************
function db_connect_plus()
returns a link identifier on success, or false on error
********************************************************/
function db_connect_plus(){
$username = "username";
$password = "password";
$primary = "10.1.1.1";
$backup = "10.1.1.2";
$timeout = 15; // timeout in seconds
if($fp = fsockopen($primary, 3306, &$errno, &$errstr, $timeout)){
fclose($fp);
return $link = mysql_connect($primary, $username, $password);
}
if($fp = fsockopen($secondary, 3306, &$errno, &$errstr, $timeout)){
fclose($fp);
return $link = mysql_connect($secondary, $username, $password);
}
return 0;
}
?$#@62;
  这个新改进的函数向我们提供了一个可调的超时特性,这正是mysql_connect函数所缺少的。如果连接立即失败,这种情况如机器"活"着,但mysqld"当"掉了,函数立即移到第二个服务器。上面的函数相当健壮,在试图进行连接之前先测试一下,查看服务程序是否在指定端口进行监听,让你的脚本在一段可接受的时间段后超时,允许你适当地对出错情况进行处理。如果你修改了缺省端口3306,请保证对端口号进行修改。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/532384.htmlTechArticle第五步:一个改进的数据库连接程序 $#@60;?php /******************************************************** function db_connect_plus() returns a link identifier on succes...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn