要在Docker中搭建MySQL主從復(fù)制,可以按照以下步驟操作:
docker network create mysql-replication
docker run -d --name mysql-master --network mysql-replication -e MYSQL_ROOT_PASSWORD=password mysql:latest
docker run -d --name mysql-slave --network mysql-replication -e MYSQL_ROOT_PASSWORD=password mysql:latest
docker exec -it mysql-master bash
mysql -u root -p
CREATE USER 'repl'@'%' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
FLUSH PRIVILEGES;
SET GLOBAL binlog_format = 'ROW';
SET GLOBAL log_slave_updates = 1;
SET GLOBAL gtid_mode = ON;
SET GLOBAL enforce-gtid-consistency = ON;
docker exec -it mysql-slave bash
mysql -u root -p
CHANGE MASTER TO MASTER_HOST='mysql-master', MASTER_USER='repl', MASTER_PASSWORD='password', MASTER_AUTO_POSITION=1;
START SLAVE;
SHOW MASTER STATUS;
在從服務(wù)器上執(zhí)行以下命令檢查主從復(fù)制狀態(tài):
SHOW SLAVE STATUS \G;
如果輸出結(jié)果中的Slave_IO_Running
和Slave_SQL_Running
均顯示Yes
,則表示主從復(fù)制已成功搭建。
通過以上步驟,您可以在Docker中成功搭建MySQL主從復(fù)制的環(huán)境。