How to Quickly Build a MySQL Master‑Slave Cluster with Docker
This step‑by‑step guide shows how to use Docker on CentOS 7 to pull MySQL 5.7 images, configure master and slave my.cnf files, run containers, set up replication, and verify that a test database created on the master is automatically synchronized to the slave.
Container technology is becoming ubiquitous, and mastering Docker and Kubernetes is essential for backend developers. This article explains how to quickly set up a MySQL master‑slave architecture using Docker.
Environment Preparation
CentOS 7
Docker 18.09.0
MySQL 5.7
Environment Setup
1. Search for and pull the MySQL 5.7 image.
docker search mysql
docker pull mysql:5.72. Create a master folder and add my.cnf to enable binary logging and set a unique server‑id.
mkdir master && vi ./master/my.cnf
[mysqld]
log-bin = mysql-bin
server-id = 33103. Run the master container.
docker run -d -e MYSQL_ROOT_PASSWORD=123456 --name master -v $PWD/master:/etc/mysql/conf.d -p 3310:3306 mysql:5.7Check the container status:
docker ps mysql4. Connect to the master container.
docker exec -it master bash
mysql -uroot -p5. Verify the master status and that binary logging is enabled.
6. Repeat steps 2‑4 for the slave container, using a different port and server‑id.
mkdir slave && vi ./slave/my.cnf
docker run -d -e MYSQL_ROOT_PASSWORD=123456 --name slave -v $PWD/slave:/etc/mysql/conf.d -p 3311:3306 mysql:5.77. Configure the slave to replicate from the master.
change master to master_host='localhost', master_user='root', master_password='123456', master_port=3310, master_log_file='mysql-bin.000003', master_log_pos=154, master_connect_retry=60;8. Start replication and check its status.
start slave
show slave statusResult Verification
9. Connect to the master container and create a test database.
docker exec -it master bash
mysql -uroot -p123456
create database test;10. Connect to the slave container and list databases to confirm the new database appears.
docker exec -it slave bash
mysql -uroot -p123456
show databases;When both SlaveIORunning and SlaveSQLRunning show Yes, replication is successful, confirming that the Docker‑based MySQL master‑slave setup is complete.
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
