Yii中实现主从数据库配置方法

发布时间:2016-12-8 0:17:39 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"Yii中实现主从数据库配置方法",主要涉及到Yii中实现主从数据库配置方法方面的内容,对于Yii中实现主从数据库配置方法感兴趣的同学可以参考一下。

现在Yii框架没有配置主从数据库,本人对yii增加的扩展(不修改原现的框架为基础),得写了数据库连接的方式,实现了一主多从,同时也可以连接多个不同的数据库(支持一主多从).详细代码如下: 一、介绍一主多从的配置: 1.extensions目录下增加DbConnectionMan.php文件,代码如下: <?php /** * 主数据库 写 从数据库(可多个)读 * 实现主从数据库 读写分离 主服务器无法连接 从服务器可切换写功能 * 从务器无法连接 主服务器可切换读功 * @author: pzliang QQ:327168521 * Date:2013-10-21 * */ 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!\n\tConnection 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); } } } 2.config/main.php加增加以下代码 //------------------------主从数据库配置开始------------------------------------ 'db2'=>array( 'class'=>'application.extensions.DbConnectionMan',//扩展路径 'connectionString' => 'mysql:host=127.0.0.1;dbname=esk_eboss_db',//主数据库 写 'emulatePrepare' => true, 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', 'tablePrefix' => 'eboss_', //表前缀 'enableSlave'=>true,//从数据库启用 'slavesWrite'=>false,//紧急情况 主数据库无法连接 启用从数据库 写功能 'masterRead'=>true,//紧急情况 从数据库无法连接 启用主数据库 读功能 'enableProfiling'=>true, 'slaves'=>array(//从数据库 读 array(//slave1 'connectionString'=>'mysql:host=localhost;dbname=test', 'emulatePrepare' => true, 'username'=>'root', 'password'=>'root', 'charset' => 'utf8', 'enableProfiling'=>true, //'tablePrefix' => 'xcpt_', //表前缀 ), array(//slave2 'connectionString'=>'mysql:host=localhost;dbname=yg_eboss_db', 'emulatePrepare' => true, 'username'=>'root', 'password'=>'root', 'charset' => 'utf8', 'enableProfiling'=>true, //'tablePrefix' => 'xcpt_', //表前缀 ), ), ), 以上是配置一主多从的配置,欢迎指正! 二、配置同时连接多个不同的数据库(支持一主多从的方式): 1.components目录下创建ActiveRecord.php文件 代码如下: <?php class ActiveRecord extends CActiveRecord { public function getDbConnection() { if(self::$db!==null) { return self::$db; } else { self::$db=Yii::app()->getComponent('db2');//这里是我们要修改的 db2就是另一个数据库 //self::$db=Yii::app()->db1; if(self::$db instanceof CDbConnection) return self::$db; else throw new CDbException(Yii::t('yii','Active Record requires a "db2" CDbConnection application component.')); } } } 2.配置连接db2配置 把上面config.php中的 db的配置复制一份 同时把db 修改成db2就可以了! 3.在model创建 Category.php(只是示例,模型名称大家可以随意定的哦)  代码如下: <?php /* * * @类别表模型 eboss_cate * @author pzliang QQ:327168521 * @CreateDate 2014-02-18 */ class Category extends ActiveRecord //一下要继承components目录下的ActiveRecord.php这个文件中的类哦 { public static function model($className = __CLASS__) { return parent::model($className); } public function tableName() { return "{{cate}}"; } public function rules() { return array( array('cat_name,root_id,ts','required') ); } } 4.控制器中的调用方式是: $info = Category::model()->find();

上一篇:JVM 参数详解(从JVM的常见异常来看Tomcat中内存的设置)
下一篇:使用HtmlParser 解析链接节点

相关文章

相关评论