How to Set Up MySQL Master‑Slave Replication with Docker in Minutes
This guide walks you through building custom MySQL Docker images, configuring master and slave my.cnf files, launching the containers, creating a replication user, and synchronizing the slave using Docker commands and a ready‑made shell script, all with detailed code examples.
Build the Dockerfile
Start from the official MySQL 5.7 image, expose port 3306, copy a custom my.cnf configuration file, and set the default command to start MySQL:
FROM mysql:5.7
EXPOSE 3306
COPY my.cnf /etc/mysql/
CMD ["mysqld"]Prepare my.cnf by first launching a temporary MySQL container, copying the default configuration, and then editing the file to enable binary logging, set a unique server-id, and comment out bind-address so remote connections are allowed. Key options include:
#bind-address = 127.0.0.1 # comment to allow remote access
log-bin = /var/log/mysql/mysql-bin.index
server-id = 1 # master, change to 2 for slaveDirectory layout
Create two parallel directories, one for the master and one for the slave, each containing its own Dockerfile and my.cnf (the slave’s server-id should be 2):
master/
├── Dockerfile
└── my.cnf
slave/
├── Dockerfile
└── my.cnfBuild the images
From each directory run:
docker build -t master/mysql:5.7.20 . # in master/
docker build -t slave/mysql:5.7.20 . # in slave/Run the containers
Start the master and slave containers, linking the slave to the master so it can resolve the hostname master:
# Master
docker run -p 3306 --name mysql-master -e MYSQL_ROOT_PASSWORD=root -d master/mysql:5.7.20
# Slave
docker run -p 3306 --name mysql-slave --link mysql-master:master -e MYSQL_ROOT_PASSWORD=root -d slave/mysql:5.7.20Use docker ps to verify the containers are running and note the host ports assigned.
Create replication user on the master
Enter the master container and execute:
mysql -uroot -proot
GRANT REPLICATION SLAVE ON *.* TO 'user'@'%' IDENTIFIED BY 'mysql';
GRANT SELECT, REPLICATION SLAVE ON *.* TO 'user'@'%';Then obtain the binary‑log file and position: SHOW MASTER STATUS\G Typical output shows File: mysql-bin.000003 and Position: 154.
Configure the slave
Inside the slave container run:
CHANGE MASTER TO
MASTER_HOST='master',
MASTER_PORT=3306,
MASTER_USER='user',
MASTER_PASSWORD='mysql',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=154;Start replication and verify:
START SLAVE;
SHOW SLAVE STATUS\GKey fields Slave_IO_Running and Slave_SQL_Running should both be Yes.
Shell script to automate the whole process
The following Bash script performs the steps above in about one minute. Adjust the paths to your custom my.cnf files as needed.
#!/bin/bash
MASTER_DIR=/var/lib/mysql/mysql-master
SLAVE_DIR=/var/lib/mysql/mysql-slave
# Remove old containers and data directories
docker rm -f mysql-master mysql-slave
rm -rf $MASTER_DIR $SLAVE_DIR
# Start fresh containers
docker run -p 3306 --name mysql-master -v /etc/master.cnf:/etc/mysql/my.cnf -v $MASTER_DIR:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d master/mysql:5.7.20
docker run -p 3306 --name mysql-slave -v /etc/slave.cnf:/etc/mysql/my.cnf -v $SLAVE_DIR:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root --link mysql-master:master -d slave/mysql:5.7.20
# Give containers time to initialize
sleep 3
# Create replication user on master
docker exec -it mysql-master mysql -uroot -proot -e "CREATE USER 'users'@'%' IDENTIFIED BY 'mysql'; GRANT REPLICATION SLAVE ON *.* TO 'users'@'%';"
# Capture master status
master_status=$(docker exec -it mysql-master mysql -uroot -proot -e "SHOW MASTER STATUS\G")
master_log_file=$(echo "$master_status" | awk 'NR==2{print $2}')
master_log_pos=$(echo "$master_status" | awk 'NR==3{print $2}')
# Configure slave
docker exec -it mysql-slave mysql -uroot -proot -e "CHANGE MASTER TO MASTER_HOST='master',MASTER_PORT=3306,MASTER_USER='users',MASTER_PASSWORD='mysql',MASTER_LOG_FILE='$master_log_file',MASTER_LOG_POS=$master_log_pos;"
docker exec -it mysql-slave mysql -uroot -proot -e "START SLAVE;"
docker exec -it mysql-slave mysql -uroot -proot -e "SHOW SLAVE STATUS\G"Running this script sets up a fully functional MySQL master‑slave replication environment using Docker.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
