创建主主同步账号repl_user和主从同步账号slave_sync_user
主1、主2 数据库:
mysql> CREATE USER 'repl_user'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.03 sec)
mysql> CREATE USER 'slave_sync_user'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl_user'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave_sync_user'@'%';
Query OK, 0 rows affected (0.00 sec)
主1 mysql 下:
show master status;
记住 binlog文件 和 偏移量,后面会用到
从1 mysql 下:
CHANGE MASTER TO MASTER_HOST='192.168.168.224', MASTER_USER='slave_sync_user', MASTER_PASSWORD='123456', MASTER_LOG_FILE='binlog.000003', MASTER_LOG_POS=952;
start slave;
show slave status \G;
遇到不是两个 Yes (下面截图这种情况)怎么办,别慌,执行下面命令:
stop slave;
reset master;
然后再 CHANGE MASTER .....
主2 mysql 下:
show master status;
CHANGE MASTER TO MASTER_HOST='192.168.168.226', MASTER_USER='slave_sync_user', MASTER_PASSWORD='123456', MASTER_LOG_FILE='binlog.000004', MASTER_LOG_POS=356;
start slave;
show slave status \G;
主1 mysql 下:
show master status;
主2 mysql 下
CHANGE MASTER TO MASTER_HOST='192.168.168.224', MASTER_USER='repl_user', MASTER_PASSWORD='123456', MASTER_LOG_FILE='binlog.000003', MASTER_LOG_POS=952;
start slave;
show slave status \G;
主2 mysql 下
show master status;
主1 mysql 下:
CHANGE MASTER TO MASTER_HOST='192.168.168.226', MASTER_USER='repl_user', MASTER_PASSWORD='123456', MASTER_LOG_FILE='binlog.000004', MASTER_LOG_POS=156;
start slave;
show slave status \G;
主1 mysql 下创建数据库 mydb2,创建表 books,插入一条数据:
create database mydb2;
create table books(id int primary key auto_increment, name varchar(50));
insert into books(name) values("MySQL 8");
查看 主1 主2 从1 从2 是否同步生成数据库、表、数据。