Yii多数据库主从读写分离实例介绍
Yii框架数据库多数据库、主从、读写分离 实现,功能描述:
1.实现主从数据库读写分离 主库:写 从库(可多个):读
2.主数据库无法连接时 可设置从数据库是否 可写
3.所有从数据库无法连接时 可设置主数据库是否 可读
4.如果从数据库连接失败 可设置N秒内不再连接
利用yii扩展实现,代码如下:
- <?php
-
-
-
-
-
-
- class DbConnectionMan extends CDbConnection {
-
- public $timeout = 10;
- public $markDeadSeconds = 600;
-
- public $cacheID = 'cache';
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public $slaves = array();
-
-
-
-
-
-
- public $enableSlave = true;
-
-
-
-
- public $slavesWrite = false;
-
-
-
-
- public $masterRead = false;
-
-
-
-
- private $_slave;
-
-
-
-
- private $_disableWrite = true;
-
-
-
-
-
-
-
- public function createCommand($sql = null) {
- if ($this->enableSlave && !emptyempty($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);
- }
- }
-
-
-
-
-
- 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!');
- }
-
- $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 {
- 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();
- }
- }
- }
-
-
-
-
-
-
-
- public static function isReadOperation($sql) {
- $sql = substr(ltrim($sql), 0, 10);
- $sql = str_ireplace(array('SELECT', 'SHOW', 'DESCRIBE', 'PRAGMA'), '^O^', $sql);
- 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;
- }
-
-
-
-
- private function _markDeadServer($c) {
-
- $cache = Yii::app()->{$this->cacheID};
- if ($cache) {
- $cache->set('DeadServer::' . $c, 1, $this->markDeadSeconds);
- }
- }
-
- }
main.php配置:components 数组中,代码如下:
- 'db'=>array(
- 'class'=>'application.extensions.DbConnectionMan',
- 'connectionString' => 'mysql:host=192.168.1.128;dbname=db_xcpt',
- 'emulatePrepare' => true,
- 'username' => 'root',
- 'password' => 'root',
- 'charset' => 'utf8',
- 'tablePrefix' => 'xcpt_',
- 'enableSlave'=>true,
- 'urgencyWrite'=>true,
- 'masterRead'=>true,
- 'slaves'=>array(
- array(
- 'connectionString'=>'mysql:host=localhost;dbname=db_xcpt',
- 'emulatePrepare' => true,
- 'username'=>'root',
- 'password'=>'root',
- 'charset' => 'utf8',
- 'tablePrefix' => 'xcpt_',
- ),
- array(
- 'connectionString'=>'mysql:host=localhost;dbname=db_xcpt',
- 'emulatePrepare' => true,
- 'username'=>'root',
- 'password'=>'root',
- 'charset' => 'utf8',
- 'tablePrefix' => 'xcpt_',
- ),
-
- ),
- ),