注:首先确保已实现了主从复制。
在主库my.ini中增加
auto_increment_offset = 1
auto_increment_increment = 2
log-slave-updates = true
最终变为如下:
[mysqld]
log-bin=mysql-bin
server-id=2
binlog-ignore-db=information_schema
binlog-do-db=test
auto_increment_offset = 1
auto_increment_increment = 2
log-slave-updates = true
在从库my.ini中增加:
auto_increment_offset = 2
auto_increment_increment = 2
最终变为如下:
[mysqld]
log-bin=mysql-bin
server-id=3
binlog-ignore-db=information_schema
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60
auto_increment_offset = 2
auto_increment_increment = 2
重启从库MySQL,给主库赋权限
赋予从库权限账号,允许用户在主库上读取日志,赋予从库有File权限及REPLICATION SLAVE的权限。
grant FILE on *.* to 'root'@'127.0.0.1' identified by 'root';
grant replication slave on *.* to 'root'@'127.0.0.1' identified by 'root';
flush privileges;
重启从库,登录mysql,查看从库信息
show master status;
重启主库,进入mysql,配置同步
stop slave;
change master to master_host='127.0.0.1',master_port=3309,master_user='root',master_password='123456',master_log_file='mysql-bin.000002', master_log_pos=154;
start slave;
查看同步状态
分别在两个库中查看同步状态
show slave statusG;
如果Slave_IO_Running和 Slave_SQL_Running都显示为YES,则说明配置成功。