1、主机容器
docker run -d --name mysql-master --network my_network -p 3307:3306 -e MYSQL_ROOT_PASSWORD=密码 mysql
2、从机容器
docker run -d --name mysql-slave --network my_network -p 3308:3306 -e MYSQL_ROOT_PASSWORD=密码 mysql
3、在主机修改mysql配置
[mysqld]
log-bin = mysql-bin
server-id = 1 #主机是1
重启容器
4、在主机数据库中运行
CREATE USER'replication_user'@'%' IDENTIFIED BY '密码';
GRANT REPLICATION SLAVE ON *.* TO'replication_user'@'%';
ALTER USER'replication_user'@'%' IDENTIFIED WITH mysql_native_password BY'AXgz$3919';
5、在从机修改mysql配置
[mysqld]
log-bin = mysql-bin
server-id = 2 #主机是2
重启容器
6、在从机数据库中运行
CHANGE MASTER TO MASTER_HOST='从机获得的IP地址',MASTER_PORT=3306, MASTER_USER='replication_user', MASTER_PASSWORD='密码', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=0;
START SLAVE;