从新配置main.php片段

代码如下

-----------------------------------------------------------

'db'=>array(
'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
),
// uncomment the following to use a MySQL database
//////////////////////////////////////////////////////
'db'=>array( 'class'=>'application.extensions.DbConnectionMan',//扩展路径 'connectionString' => 'mysql:host=localhost;dbname=test',//主数据库 写 'emulatePrepare' => true, 'username' => 'root', 'password' => '5201314', 'charset' => 'utf8', 'tablePrefix' => '', //表前缀 'enableSlave'=>true,//从数据库启用 //'urgencyWrite'=>true,//紧急情况 主数据库无法连接 启用从数据库 写功能 //'masterRead'=>true,//紧急情况 从数据库无法连接 启用主数据库 读功能 'slaves'=>array(//从数据库 array( //slave1 'connectionString'=>'mysql:host=localhost;dbname=test2', 'emulatePrepare' => true, 'username'=>'root', 'password'=>'5201314', 'charset' => 'utf8', 'tablePrefix' => '', //表前缀 ), array( //slave2 'connectionString'=>'mysql:host=localhost;dbname=test3', 'emulatePrepare' => true, 'username'=>'root', 'password'=>'5201314', 'charset' => 'utf8', 'tablePrefix' => '', //表前缀 ),
),
),

-----------------------------------------------------------

在protected/extensions中新建文件名DbConnectionMan.php

代码如下

----------------------------------------

<?php

/**

 * 主数据库 写 从数据库(可多个)读

 * 实现主从数据库 读写分离 主服务器无法连接 从服务器可切换写功能

 * 从务器无法连接 主服务器可切换读功

 * by lmt

 * */

class DbConnectionMan extends CDbConnection {

    public $timeout = 10; //连接超时时间

    public $markDeadSeconds = 600; //如果从数据库连接失败 600秒内不再连接

    //用 cache 作为缓存全局标记

    public $cacheID = 'cache';

    /**

     * @var array $slaves.Slave database connection(Read) config array.

     * 配置符合 CDbConnection.

     * @example

     * 'components'=>array(

     *   'db'=>array(

     *    'connectionString'=>'mysql://<master>',

     *    'slaves'=>array(

     *     array('connectionString'=>'mysql://<slave01>'),

     *     array('connectionString'=>'mysql://<slave02>'),

     *    )

     *   )

     * )

     * */

    public $slaves = array();

    /**

     *

     * 从数据库状态 false 则只用主数据库

     * @var bool $enableSlave

     * */

    public $enableSlave = true;

    /**

     * @var slavesWrite 紧急情况主数据库无法连接 切换从服务器(读写).

     */

    public $slavesWrite = false;

    /**

     * @var masterRead 紧急情况从主数据库无法连接 切换从住服务器(读写).

     */

    public $masterRead = false;

    /**

     * @var _slave

     */

    private $_slave;

    /**

     * @var _disableWrite 从服务器(只读).

     */

    private $_disableWrite = true;

    /**

     *

     * 重写 createCommand 方法,1.开启从库 2.存在从库 3.当前不处于一个事务中 4.从库读数据

     * @param string $sql

     * @return CDbCommand

     * */

    public function createCommand($sql = null) {

        if ($this->enableSlave && !empty($this->slaves) && is_string($sql) && !$this->getCurrentTransaction() && self::isReadOperation($sql) && ($slave = $this->getSlave())

        ) {

            return $slave->createCommand($sql);

        } else {
// if (!$this->masterRead) {
//
// if ($this->_disableWrite && !self::isReadOperation($sql)) {
//
// throw new CDbException("Master db server is not available now!Disallow write operation on slave server!");
//
// }
//
// } return parent::createCommand($sql); } } /** * 获得从服务器连接资源 * @return CDbConnection * */ public function getSlave() { if (!isset($this->_slave)) { shuffle($this->slaves); foreach ($this->slaves as $slaveConfig) { if ($this->_isDeadServer($slaveConfig['connectionString'])) { continue; } if (!isset($slaveConfig['class'])) $slaveConfig['class'] = 'CDbConnection'; $slaveConfig['autoConnect'] = false; try { if ($slave = Yii::createComponent($slaveConfig)) { Yii::app()->setComponent('dbslave', $slave); $slave->setAttribute(PDO::ATTR_TIMEOUT, $this->timeout); $slave->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); $slave->setActive(true); $this->_slave = $slave; break; } } catch (Exception $e) { $this->_markDeadServer($slaveConfig['connectionString']); Yii::log("Slave database connection failed!ntConnection string:{$slaveConfig['connectionString']}", 'warning'); continue; } } if (!isset($this->_slave)) { $this->_slave = null; $this->enableSlave = false; } } return $this->_slave; } public function setActive($value) { if ($value != $this->getActive()) { if ($value) { try { if ($this->_isDeadServer($this->connectionString)) { throw new CDbException('Master db server is already dead!'); } //PDO::ATTR_TIMEOUT must set before pdo instance create $this->setAttribute(PDO::ATTR_TIMEOUT, $this->timeout); $this->open(); } catch (Exception $e) { $this->_markDeadServer($this->connectionString); $slave = $this->getSlave(); Yii::log($e->getMessage(), CLogger::LEVEL_ERROR, 'exception.CDbException'); if ($slave) { $this->connectionString = $slave->connectionString; $this->username = $slave->username; $this->password = $slave->password; if ($this->slavesWrite) { $this->_disableWrite = false; } $this->open(); } else { //Slave also unavailable if ($this->masterRead) { $this->connectionString = $this->connectionString; $this->username = $this->username; $this->password = $this->password; $this->open(); } else { throw new CDbException(Yii::t('yii', 'CDbConnection failed to open the DB connection.'), (int) $e->getCode(), $e->errorInfo); } } } } else { $this->close(); } } } /** * 检测读操作 sql 语句 * * 关键字: SELECT,DECRIBE,SHOW ... * 写操作:UPDATE,INSERT,DELETE ... * */ public static function isReadOperation($sql) { $sql = substr(ltrim($sql), 0, 10); $sql = str_ireplace(array('SELECT', 'SHOW', 'DESCRIBE', 'PRAGMA'), '^O^', $sql); //^O^,magic smile
return strpos($sql, '^O^') === 0; } /** * 检测从服务器是否被标记 失败. */ private function _isDeadServer($c) { $cache = Yii::app()->{$this->cacheID}; if ($cache && $cache->get('DeadServer::' . $c) == 1) { return true; } return false; } /** * 标记失败的slaves. */ private function _markDeadServer($c) { $cache = Yii::app()->{$this->cacheID}; if ($cache) { $cache->set('DeadServer::' . $c, 1, $this->markDeadSeconds); } } }

-----------------------------------------

配置完毕,接下来在控制器中测试代码如下

----------------------------

$goods = new goods();
$d = $goods->findAll();
foreach($d AS $k =>$v){
echo $v->name;
}
$goods->name='test insert aaaa';
$goods->insert();

-----------------------------

yii1.1.3主从(多从)、读写分离配置的更多相关文章

  1. MySQL主从同步、读写分离配置步骤、问题解决笔记

    MySQL主从同步.读写分离配置步骤.问题解决笔记 根据要求配置MySQL主从备份.读写分离,结合网上的文档,对搭建的步骤和出现的问题以及解决的过程做了如下笔记:       现在使用的两台服务器已经 ...

  2. MySQL主从同步、读写分离配置步骤

    现在使用的两台服务器已经安装了MySQL,全是rpm包装的,能正常使用. 为了避免不必要的麻烦,主从服务器MySQL版本尽量保持一致; 环境:192.168.0.1 (Master) 192.168. ...

  3. Redis系列之(二):Redis主从同步,读写分离

    1. Redis主从同步 Redis支持主从同步.数据可以从主服务器向任意数量的从服务器上同步,同步使用的是发布/订阅机制. 2. 配置主从同步 Mater Slave的模式,从Slave向Maste ...

  4. Redis系列之(二):Redis主从同步,读写分离(转)

    1. Redis主从同步 Redis支持主从同步.数据可以从主服务器向任意数量的从服务器上同步,同步使用的是发布/订阅机制. 2. 配置主从同步 Mater Slave的模式,从Slave向Maste ...

  5. MySQL主从复制技术与读写分离技术amoeba应用

    MySQL主从复制技术与读写分离技术amoeba应用 前言:眼下在搭建一个人才站点,估计流量会非常大,须要用到分布式数据库技术,MySQL的主从复制+读写分离技术.读写分离技术有官方的MySQL-pr ...

  6. laravel学习:主从读写分离配置的实现

    本篇文章给大家带来的内容是关于laravel学习:主从读写分离配置的实现,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 在DB的连接工厂中找到以下代码.../vendor/larav ...

  7. MySQL主从及读写分离配置

    <<MySQL主从又叫做Replication.AB复制.简单讲就是A和B两台机器做主从后,在A上写数据,B也会跟着写数据,两者数据实时同步>> MySQL主从是基于binlo ...

  8. Mysql一主多从和读写分离配置简记

    近期开发的系统中使用MySQL作为数据库,由于数据涉及到Money,所以不得不慎重.同时,用户对最大访问量也提出了要求.为了避免Mysql成为性能瓶颈并具备很好的容错能力,特此实现主从热备和读写分离. ...

  9. yii2的数据库读写分离配置

    简介 数据库读写分离是在网站遇到性能瓶颈的时候最先考虑优化的步骤,那么yii2是如何做数据库读写分离的呢?本节教程来给大家普及一下yii2的数据库读写分离配置. 两个服务器的数据同步是读写分离的前提条 ...

  10. Mycat入门配置_读写分离配置

    1.Mycat的分片 两台数据库服务器: 192.168.80.11 192.168.80.4 操作系统版本环境:centos6.5 数据库版本:5.6 mycat版本:1.4 release 数据库 ...

随机推荐

  1. Linux常用命令_(系统管理)

    用户管理:useradd.passwd.userdel用户组管理:groupadd.groupdel.gpasswd系统信息:uname.who.whoami.id登录注销:login.logout. ...

  2. js:数据结构笔记8--集合

    集合:唯一性,无序性: 基本结构: function Set () { this.dataStore = []; this.add = add; this.remove = remove; this. ...

  3. VMware12版虚拟机怎么安装win7系统

    工具/原料   VMware workstation 12 windows7镜像ios系统文件 链接:http://pan.baidu.com/s/1c0YrDgG 密码:vna1 建立一个新的虚拟机 ...

  4. 转:ie6与firefox操作iframe中DOM节点的一点不同

    依次在两个浏览器中运行以下代码 <html> <body> <iframe id="myiframe"></iframe> < ...

  5. ZOJ 3805 (树形DP)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5337 题目大意:方块连接,呈树形.每个方块有两种接法,一种接在父块 ...

  6. (转)Linq学习笔记

    写在前面 最近在看Linq,在博客园看到这篇文章,写的通俗易懂,转来和大家一起做个分享.原文地址http://www.cnblogs.com/goscan/archive/2011/05/05/Lin ...

  7. POJ 3373 Changing Digits(DP)

    题目链接 记录路径的DP,看的别人的思路.自己写的也不好,时间居然2000+,中间的取余可以打个表,优化一下. 写的各种错,导致wa很多次,写了一下午,自己构造数据,终于发现了最后一个bug. dp[ ...

  8. JSON.stringify语法解析(自己留存)

    语法: JSON.stringify(value [, replacer] [, space]) value:是必选字段.就是你输入的对象,比如数组,类等. replacer:这个是可选的.它又分为2 ...

  9. LINQ Count/Sum/Min/Max/Avg

    参考:http://www.cnblogs.com/peida/archive/2008/08/11/1263384.html Count/Sum/Min/Max/Avg用于统计数据,比如统计一些数据 ...

  10. mac使用指南:brew的安装

    官网 http://brew.sh/ 安装并且下载brew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/ ...