How to Set Up MySQL Master‑Slave Replication in Docker (Step‑by‑Step Guide)
This article explains the principles of MySQL master‑slave replication and provides a detailed, step‑by‑step tutorial for configuring a master and a slave instance in a Docker environment, including configuration files, Docker commands, replication setup, testing, and troubleshooting.
What Is Master‑Slave Replication?
Master‑slave replication copies DDL and DML operations from the primary database’s binary log to the replica, where the logs are re‑executed to keep both databases synchronized.
Replication Principle
The primary MySQL server records data changes as events in the binary log (Binlog) when a transaction commits.
The primary pushes these events to the replica’s relay log, and the replica replays them to achieve data consistency.
Three threads handle replication: the Binlog Dump thread on the master, and the I/O and SQL threads on the replica.
When replication starts, the I/O thread connects to the master, receives events via the Binlog Dump thread, writes them to the relay log, and the SQL thread applies the events from the relay log.
Master Instance Setup
Run the MySQL master container:
docker run -p 3307:3306 --name mysql-master \
-v /mydata/mysql-master/log:/var/log/mysql \
-v /mydata/mysql-master/data:/var/lib/mysql \
-v /mydata/mysql-master/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7Create a configuration file my.cnf in /mydata/mysql-master/conf: touch my.cnf Edit my.cnf with the following settings:
[mysqld]
## Set a unique server_id in the LAN
server_id=101
## Databases to ignore for replication
binlog-ignore-db=mysql
## Enable binary logging
log-bin=mall-mysql-bin
## Binary log cache size
binlog_cache_size=1M
## Binary log format (mixed, statement, row)
binlog_format=mixed
## Expire logs after 7 days
expire_logs_days=7
## Skip replication errors (e.g., duplicate key 1062)
slave_skip_errors=1062Restart the master container: docker restart mysql-master Enter the container and connect to MySQL:
docker exec -it mysql-master /bin/bash
mysql -uroot -prootCreate a replication user:
CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';Slave Instance Setup
Run the MySQL slave container:
docker run -p 3308:3306 --name mysql-slave \
-v /mydata/mysql-slave/log:/var/log/mysql \
-v /mydata/mysql-slave/data:/var/lib/mysql \
-v /mydata/mysql-slave/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7Create my.cnf in /mydata/mysql-slave/conf and edit it:
[mysqld]
## Unique server_id
server_id=102
## Databases to ignore
binlog-ignore-db=mysql
## Enable binary logging for potential downstream masters
log-bin=mall-mysql-slave1-bin
## Binary log cache size
binlog_cache_size=1M
## Binary log format
binlog_format=mixed
## Expire logs after 7 days
expire_logs_days=7
## Skip replication errors
slave_skip_errors=1062
## Relay log configuration
relay_log=mall-mysql-relay-bin
## Write replicated events to own binlog
log_slave_updates=1
## Set read‑only mode (except for super users)
read_only=1Restart the slave container:
docker restart mysql-slaveConnecting Master and Slave
On the master, check status: show master status; Enter the slave container and connect to MySQL:
docker exec -it mysql-slave /bin/bash
mysql -uroot -prootConfigure replication on the slave:
change master to master_host='192.168.6.132', master_user='slave', master_password='123456', master_port=3307, master_log_file='mall-mysql-bin.000001', master_log_pos=617, master_connect_retry=30;Parameter explanations:
master_host – IP of the master server.
master_port – Port of the master server.
master_user – Replication user created on the master.
master_password – Password for the replication user.
master_log_file – Binary log file to start replicating from.
master_log_pos – Position within the binary log.
master_connect_retry – Retry interval in seconds.
Check slave status: show slave status \G; Start replication:
start slave;Replication Test
To verify the setup, create a database on the master and confirm it appears on the slave.
Create database mall on the master.
Observe that the same mall database exists on the slave, confirming successful replication.
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.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.
