$this->config['rw_separate'] 为true的时候采用读写分离,为false的时候读写不分离。读写分离为什么必须是主从式的呢?因为从服务器不能写只能读,如果向从服务器写入数据的话,数据是没法同步的。这样就会造成数据不一致的情况。所以说,如果我们的系统是主从式的话,我们必须采用读写分离。也就是说DB_RW_SEPARATE选项必须配置为true。
'DB_MASTER_NUM'=>1
该选项后面的数字表示读写分离后,主服务器的数量。因此该选项也是用于主从式数据库系统。
下面的代码是选择主服务器。
$m = floor(mt_rand(0,$this->config['master_num']-1));
主从式数据库读取的时候选择从服务器读的核心代码
其中$this->config['master_num']表示主服务器的数量。
'DB_SLAVE_NO'=> ''
指定从服务器的序号,用于读取数据。如果不设置,则根据主服务器的数量计算书从服务器的数量,然后从中随机选取一台进行读取。
if(is_numeric($this->config['slave_no'])) {// 指定服务器读
$r = $this->config['slave_no'];
}else{
// 读操作连接从服务器
$r = floor(mt_rand($this->config['master_num'],count($_config['hostname'])-1)); // 每次随机连接的数据库
}
以上是对每个选项的作用的实现代码进行了一个简单的说明。
下面我们来看其连接的部分
if($m != $r ){
$db_master = array(
'username' => isset($_config['username'][$m])?$_config['username'][$m]:$_config['username'][0],
'password' => isset($_config['password'][$m])?$_config['password'][$m]:$_config['password'][0],
'hostname' => isset($_config['hostname'][$m])?$_config['hostname'][$m]:$_config['hostname'][0],
'hostport' => isset($_config['hostport'][$m])?$_config['hostport'][$m]:$_config['hostport'][0],
'database' => isset($_config['database'][$m])?$_config['database'][$m]:$_config['database'][0],
'dsn' => isset($_config['dsn'][$m])?$_config['dsn'][$m]:$_config['dsn'][0],
'charset' => isset($_config['charset'][$m])?$_config['charset'][$m]:$_config['charset'][0],
);
}
$db_config = array(
'username' => isset($_config['username'][$r])?$_config['username'][$r]:$_config['username'][0],
'password' => isset($_config['password'][$r])?$_config['password'][$r]:$_config['password'][0],
'hostname' => isset($_config['hostname'][$r])?$_config['hostname'][$r]:$_config['hostname'][0],
'hostport' => isset($_config['hostport'][$r])?$_config['hostport'][$r]:$_config['hostport'][0],
'database' => isset($_config['database'][$r])?$_config['database'][$r]:$_config['database'][0],
'dsn' => isset($_config['dsn'][$r])?$_config['dsn'][$r]:$_config['dsn'][0],
'charset' => isset($_config['charset'][$r])?$_config['charset'][$r]:$_config['charset'][0],
);
return $this->connect($db_config,$r,$r == $m ? false : $db_master);
看到这,我觉得大家应该对上面在介绍各个配置选项的代码的时候其中的$r和$m是什么作用了。
现在我们来看 $r == $m ? false : $db_master ,如果数据库读写不分离的情况下,读写是一台服务器的话 传给connect函数的值为false。或者是如果是主从分离的写的情况下传给connect的值也为false。通过上面代码我们看到,如果$r和$m不相等的情况下,会设置$db_master。其实也就是相当于一台备用的,如果选择的$r服务器出现故障不能连接,将会去连接$db_master。
connect()函数的第三个参数其实是表示当$db_config这台服务器连接故障时是否选择备用的连接。false表示不重连,其它值即表示重新连接。
其核心代码如下
try{
if(empty($config['dsn'])) {
$config['dsn'] = $this->parseDsn($config);
}
if(version_compare(PHP_VERSION,'5.3.6','<=')){
// 禁用模拟预处理语句
$this->options[PDO::ATTR_EMULATE_PREPARES] = false;
}
$this->linkID[$linkNum] = new PDO( $config['dsn'], $config['username'], $config['password'],$this->options);
}catch (\PDOException $e) {
if($autoConnection){ //$autoConnection不为false,而是默认的主服务器
trace($e->getMessage(),'','ERR');
return $this->connect($autoConnection,$linkNum); //出现异常,使用递归函数重新连接
}elseif($config['debug']){
E($e->getMessage());
}
}
ThinkPHP整合datatables实现服务端分页的示例代码下面小编就为大家分享一篇ThinkPHP整合datatables实现服务端分页的示例代码,具有很好的参考价值,希望对大家有所帮
PHP长连接实现与使用方法详解这篇文章主要介绍了PHP长连接实现与使用方法,结合实例形式较为详细的分析了php长连接的概念、功能、实现与使用方
PHP使用Redis长连接的方法详解这篇文章主要介绍了PHP使用Redis长连接的方法,结合实例形式详细分析php操作redis长连接的使用方法及相关注意事项,需
thinkphp3.2.0 setInc方法 源码全面解析下面小编就为大家分享一篇thinkphp3.2.0 setInc方法 源码全面解析,具有很好的参考价值,希望对大家有所帮助。一起跟
php实现mysql连接池效果实现代码这篇文章主要介绍了php代码实现mysql连接池效果,需要的朋友可以参考下
tp5(thinkPHP5)操作mongoDB数据库的方法这篇文章主要介绍了tp5(thinkPHP5)操作mongoDB数据库的方法,结合实例形式简单分析了mongoDB数据库及thinkPHP5连接、查询Mo