Database master-slave synchronization

1: Open three virtual machines or three containers

docker run -itd --name mysql_m -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.35

docker run -itd --name mysql_s1 -p 3308:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.35

docker run -itd --name mysql_s2 -p 3309:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.35

2: Set one of them as the master and the other two as slaves.

 docker cp 容器名称:/etc/mysql/conf.d/docker.cnf m.cnf
 三个复制过来,主mysql的上面加上
 设置server-id
 开启bin-log
 
 从机上只用加server-id
 三个的server-id应该不同

host


[mysqld]
skip-host-cache
skip-name-resolve
server-id=1
log-bin=master-bin

from


[mysqld]
skip-host-cache
skip-name-resolve
server-id=2

After the modification is completed, place the corresponding files in the corresponding containers.

3: Set up the main

1: Enter the database and create a user

create user 'rep'@'%' identified by '123456';

2: Authorize new users

 grant replication slave on *.* to 'rep'@'%';

3: Refresh permissions

flush privileges;

4: Try to log in as a new user

mysql -urep -p123456

5: Check the log number and position number

 show master status;

4: Set up the slave

change master to
master_host="192.168.128.138",master_port=3307,
master_user="rep",
master_password="123456",
master_log_file="master-bin.000001",
master_log_pos=737;

strat slave;

5: Verification

show slave status \GIt is valid
if the bottom two are both
YES

Guess you like

Origin blog.csdn.net/m0_51828898/article/details/131760598