Home > php教程 > php手册 > 用MySQL内建复制来最佳化可用性(六)

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

WBOY
Release: 2016-06-13 10:22:10
Original
905 people have browsed it

第五步:一个改进的数据库连接程序
$#@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,请保证对端口号进行修改。

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template