1、两台服务器互联master、slave
2、master配置:
server-id = 1 master端id号
log-bin=/data/logbin/mysql-bin 日志路径及文件名
#binlog-do-db = cacti 同步cacti,此处关闭的话,就是除不允许的,其它的库均同步。
binlog-ignore-db = mysql 不同步mysql库,以下同上
mysql>show master status;
3、slave配置: server-id = 2 slave的id号,此处一定要大于master端。
保存退出。
/usr/local/mysql/bin/mysqladmin -uroot -p shutdown
tar xvzf /data/mysql/cacti.tgz /data/mysql/cacti
chown -r mysql.mysql /data/mysql/cacti
/usr/local/mysql/bin/mysql -uroot -p
mysql>stop slave;
mysql>change master to
>master_host='192.168.2.67',
>master_user='rsync', master端创建的用于主从同步的账户和密码
>master_password='123456',
>master_port='3306', master端设置的client端使用的端口号。
>master_log_file='mysql-bin.000047', master端记录的file值
>master_log_pos=391592414; master端记录的position值
mysql>start slave;
mysql>show slave status \g
==========================================================
array(
* 'db'=>array(
* 'connectionstring'=>'mysql://',
* 'slaves'=>array(
* array('connectionstring'=>'mysql://'),
* array('connectionstring'=>'mysql://'),
* )
* )
* )
* */
public $slaves=array();
/**
* whether enable the slave database connection.
* defaut is true.set this property to false for the purpose of only use the master database.
* @var bool $enableslave
* */
public $enableslave=true;
/**
* @override
* @var bool $autoconnect whether connect while init
* */
public $autoconnect=false;
/**
* @var cdbconnection
*/
private $_slave;
/**
* creates a cdbcommand object for excuting sql statement.
* it will detect the sql statement's behavior.
* while the sql is a simple read operation.
* it will use a slave database connection to contruct a cdbcommand object.
* default it use current connection(master database).
*
* @override
* @param string $sql
* @return cdbcommand
* */
public function createcommand($sql) {
if ($this->enableslave && !$this->getcurrenttransaction() && self::isreadoperation($sql)) {
return $this->getslave()->createcommand($sql);
} else {
return parent::createcommand($sql);
}
}
/**
* construct a slave connection cdbconnection for read operation.
* @return cdbconnection
* */
public function getslave() {
if (!isset($this->_slave)) {
foreach ($this->slaves as $slaveconfig) {
if (!isset($slaveconfig['class']))
$slaveconfig['class']='cdbconnection';
try {
if ($slave=yii::createcomponent($slaveconfig)) {
yii::app()->setcomponent('dbslave',$slave);
$this->_slave=$slave;
break;
}
} catch (exception $e) {
echo ''; var_dump($e);echo '
';}
}
if (!$this->_slave) {
$this->_slave=clone $this;
$this->_slave->enableslave=false;
}
}
return $this->_slave;
}
/**
* detect whether the sql statement is just a simple read operation.
* read operation means this sql will not change any thing ang aspect of the database.
* such as select,decribe,show etc.
* on the other hand:update,insert,delete is write operation.
* */
public function isreadoperation($sql) {
return preg_match('/^\s*(select|show|desc|pragma)\s+/i',$sql);
}
}
==================================================
'db'=>array(
//'connectionstring' => 'mysql:host=localhost;dbname=yiitest',
'class' => 'dbconnectionman',
'connectionstring' => 'mysql:host=localhost;dbname=ms_test',
'emulateprepare' => true,
//'tableprefix' => 'ms_',
'username' => 'root',
'password' => '123456',
'charset' => 'utf8',
// 'enableprofiling' => true,
// 'enableparamlogging' => true,
'slaves' => array(
array('connectionstring' => 'mysql:host=10.237.94.13;dbname=ms_test',
// 'class' => 'cdbconnection',
'username' => 'yanghuolong',
'password' => '123456',
'enableprofiling' => true,
'enableparamlogging' => true,
'charset' => 'utf8'),
),
),
