Mysql 读写分离
YIi 配置
<?php
return [
‘class‘ =>
‘yii\db\Connection‘,
‘masterConfig‘ => [
// ‘dsn‘ => ‘mysql:host=localhost;dbname=studyyii‘,
‘username‘ =>
‘root‘,
‘password‘ =>
‘caesar‘,
‘charset‘ =>
‘utf8‘,
‘attributes‘ => [
// use a smaller connection timeout
PDO::ATTR_TIMEOUT
=> 10,
],
],
// 配置主服务器组
‘masters‘ =>
[
[‘dsn‘ =>
‘mysql:host=172.31.80.31;dbname=studyyii‘],
// [‘dsn‘ =>
‘dsn for master server 2‘],
],
// 配置从服务器
‘slaveConfig‘ => [
‘username‘ =>
‘root‘,
‘password‘ =>
‘root‘,
‘charset‘ =>
‘utf8‘,
‘attributes‘ => [
// use a smaller connection timeout
PDO::ATTR_TIMEOUT => 10,
],
],
// 配置从服务器组
‘slaves‘ =>
[
[‘dsn‘ =>
‘mysql:host=localhost;dbname=studyyii‘],
],
];
Linux mysql master
My.cnf:
server_id = 1 #从服务器标识,范围1---2E32-1
log_bin = mysql_bin
binlog_do_db = studyyii
binlog_do_db = mysql
mysql service restart
windows mysql slave
mysql.ini:
[mysqld]
port=3306
server-id = 2
replicate-do-db = study
重启mysql
登陆mysql
mysql> stop
slave;
mysql> change
master to
master_host=‘192.168.1.200‘,master_user=‘root‘,master_password=‘123456‘;
mysql> start
slave;
mysql> show
slave status\G;
查看这两项是否为YES,yes为正常。
Slave_IO_Running:
Yes
Slave_SQL_Running:
Yes
Ok 了 读写分离,主从同步