Databases 4 min read

How to Set Up MySQL Master‑Slave Replication with Docker

This guide walks through configuring MySQL master‑slave replication inside Docker containers, covering environment setup, container creation, MySQL configuration changes, replication user creation, synchronization commands, troubleshooting tips, and verification of data replication between the master and slave instances.

Ops Development Stories
Ops Development Stories
Ops Development Stories
How to Set Up MySQL Master‑Slave Replication with Docker

Environment

Docker, CentOS and the official MySQL image are used. The image is pulled directly from Docker Hub.

Run the master container

Start a MySQL container named master and map its internal port 3306 to host port 3307:

docker run -d --name master -p 3307:3306 -eMYSQL_ROOT_PASSWORD=root mysql

List containers with docker ps -a. The -e flag sets the root password, -d runs the container in the background.

Configure the master

Enter the master container: docker exec -it $(docker ps -qf "name=master") /bin/bash Create a replication user:

grant replication slave on *.* to 'wanger'@'%' identified by 'wanger';
flush privileges;

Modify my.cnf to enable binary logging and set a unique server-id (e.g., 1). Restart the container and check the binary log file and position:

show master status;

Run the slave container

Start a second MySQL container named slave and map its port 3306 to host port 3308:

docker run -d --name slave -p 3308:3306 -eMYSQL_ROOT_PASSWORD=root mysql

Enter the slave container and edit its configuration to enable binary logging and assign a different server-id (e.g., 2). Restart the service.

Configure replication on the slave

In the slave MySQL session, point it to the master using the binary log file name and position recorded earlier:

change master to master_host='master', master_user='wanger', master_password='wanger', master_log_file='mysql-bin.000001', master_log_pos=154;
start slave;

Verify the replication status:

show slave status\G;

Troubleshooting

If Slave_IO_Running stays in Connecting, check that the slave is using the master’s IP address, not its own. Correct the IP in the change master command and restart the slave.

Test the replication

Create a database on the master: create database wanger; The new database appears on the slave, confirming that replication works.

With these steps, MySQL master‑slave replication is fully configured and operational inside Docker containers.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerdatabasemysqlMaster‑SlaveReplication
Ops Development Stories
Written by

Ops Development Stories

Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.