YII支持DB主从吗?

YII1.1.7支持DB主从吗?

主从或是多数据库,如何链接?

谢谢

实现数据分离:

insert,update,delete连接db1,select连接db2

谢谢

实现一主一从,貌似可以:

在CApplication类里加个getDb2(),db2为重库

public function getDb2()

{

return $this->getComponent(‘db2’);

}

然后query()方法里,加上

self::$db = Yii::app()->getDb2();

就OK了。

但是,但是,如有多个重库,那当前用户请求进来的find到那个重库呢?这又该如何分配设置呢?

数据库主从分离

可以使用这个类,目前有些网站在使用它了

//日志组件的配置

return array(

‘class’=>‘CDbConnectionExt’,

‘emulatePrepare’ => true,

‘charset’ => ‘utf8’,

//主数据库配置

‘connectionString’ => ‘mysql:host=localhost;dbname=a;port=3306’,

‘username’ => ‘root’,

‘password’ => ‘11’,

//从数据库配置

‘slaveConfig’=>array(

array(‘connectionString’=>‘mysql:host=localhost;dbname=a;port=3306’,‘username’=>‘root’,‘password’=>‘11’,),

array(‘connectionString’=>‘mysql:host=localhost;dbname=a;port=3306’,‘username’=>‘root’,‘password’=>‘11’,),

array(‘connectionString’=>‘mysql:host=localhost;dbname=a;port=3306’,‘username’=>‘root’,‘password’=>‘11’,),

array(‘connectionString’=>‘mysql:host=localhost;dbname=a;port=3306’,‘username’=>‘root’,‘password’=>‘11’,),

),

);

DbConnectionMan

我用了这个主从分离扩展

挺好用,只要在main.php配置文件里,做简单配置就可以,呵

DbConnectionMan 并不完美,从代码上来说不支持Query Builder方式的查询

做一个记号