基于上一篇博客本地多实例基础上搭建双主双从。再次特别感谢此篇博客。
1、理清楚主从关系
2、配置第一台主数据库,进入my.ini文件,新增如下配置
server-id=3306 #全局唯一
log-bin=D:\mysqlBinLog\mysql3306\mysqlbin #logbin日志
binlog-ignore-db=mysql #不同步的数据库,也可以不配置
binlog_format=mixed #同步模式
auto_increment_increment=2 #有几台主机就写几
auto_increment_offset=1 #步长另外一台主机写2,两个主机不能相同
log-slave-updates #主机同步的数据往从机渗透
sync_binlog=1 #MySQL把binlog缓存刷进日志文件中,默认是0,最安全的是设置为1
3、配置第二台主数据库,进入my.ini文件,新增如下配置
server-id=3308 #全局唯一(注意与上面的区别)
log-bin=D:\mysqlBinLog\mysql3308\mysqlbin #logbin日志(注意与上面的区别)
binlog-ignore-db=mysql #不同步的数据库,也可以不配置
binlog_format=mixed #同步模式
auto_increment_increment=2 #有几台主机就写几
auto_increment_offset=2 #步长另外一台主机写2,两个主机不能相同(注意与上面的区别)
log-slave-updates #主机同步的数据往从机渗透
sync_binlog=1 #MySQL把binlog缓存刷进日志文件中,默认是0,最安全的是设置为1
4、配置从数据库,进入my.ini文件,新增如下配置
server-id=3307
relay-log=mysql-relay #开启超级日志
server-id=3309
relay-log=mysql-relay #开启超级日志
5、重启4个服务,我是在服务管理里面直接右键重新启动。
6、配置主从关系
在3306主服务上创建用户slave1,用于从库访问主库。
GRANT REPLICATION SLAVE ON *.* TO 'slave1'@'%' IDENTIFIED BY '123456'
在3308主服务上创建用户slave2,用于从库访问主库。
GRANT REPLICATION SLAVE ON *.* TO 'slave2'@'%' IDENTIFIED BY '123456'
在两台主服务器上停止复制并刷新binlog日志:(3306、3308),mysql下:
stop slave;
reset slave;
reset master;
在从服务器上停止复制:(3307、3309)
stop slave;
reset slave;
主服务上查看master状态:
SHOW MASTER STATUS;
在Slave上设置Master(相当于是4台都需要设置),设置从服务器3307、3308,他们的主均为3306,即在3307和3308上执行如下操作:
CHANGE MASTER TO MASTER_HOST='192.168.31.134', #主机ip
MASTER_USER='slave1', #主机创建的用户
MASTER_PASSWORD='123456', #创建用户的密码
master_port=3306,
MASTER_LOG_FILE='mysqlbin.000002',MASTER_LOG_POS=368; #开始备份的戳,对应主机里面的状态
在Slave上设置Master(相当于是4台都需要设置),设置从服务器3306、3309,他们的主均为3308,即在3306和3309上执行如下操作:
CHANGE MASTER TO MASTER_HOST='192.168.31.134', #主机ip
MASTER_USER='slave2', #上面创建的用户
MASTER_PASSWORD='123456', #创建用户的密码
master_port=3308,
MASTER_LOG_FILE='mysqlbin.000002',MASTER_LOG_POS=368; #开始备份的戳
在四台MySQL服务器上执行:start slave; (MySQL命令行执行)执行后即开始进入主从复制状态。
7、验证主从关系
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.31.134
Master_User: slave2
Master_Port: 3308
Connect_Retry: 60
Master_Log_File: mysqlbin.000002
Read_Master_Log_Pos: 360
Relay_Log_File: mysqld3306-relay-bin.000005
Relay_Log_Pos: 252
Relay_Master_Log_File: mysqlbin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 360
Relay_Log_Space: 558
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 3308
1 row in set (0.00 sec)
都是YES表示ok。