How to Build a MySQL Master‑Slave Replication Environment with Docker‑Compose
Learn step‑by‑step how to set up a binlog‑based MySQL master‑slave replication using Docker‑Compose and Dockerfiles, covering directory structure, configuration files, container creation, verification, and troubleshooting, so you can quickly deploy a reliable MySQL replication environment.
Preface
This article records how to use docker-compose and Dockerfiles to build a binlog‑based MySQL master‑slave environment. Follow the steps precisely and you should be able to set up a basic MySQL replication setup quickly.
Introduction
MySQL master‑slave synchronization consists of five steps:
The master writes data changes to the binary log.
The slave starts an I/O thread to request logs from the master after a specific position.
The master’s binlog dump thread pushes the requested logs to the slave.
The slave’s I/O thread writes the received logs to a relay log file.
The slave’s SQL thread reads the relay log, converts entries to SQL statements, and replays them on the slave database to keep data consistent.
Configuration
Create Directory Structure
First set up the directory layout as shown below. If you use a different layout, adjust the paths in docker-compose.yaml and the Dockerfiles accordingly.
Docker‑Compose Template
version: "3"
services:
mysql-master:
build:
context: ./
dockerfile: mysql/master/Dockerfile
container_name: mysql-master
volumes:
- ./mysql/master/data:/var/lib/mysql
restart: always
ports:
- 3305:3306
links:
- mysql-slave
mysql-slave:
build:
context: ./
dockerfile: mysql/slave/Dockerfile
container_name: mysql-slave
volumes:
- ./mysql/slave/data:/var/lib/mysql
restart: always
ports:
- 3306:3306Master Node Configuration
cluster.cnf
[mysqld]
server_id=100
binlog-ignore-db=mysql
log-bin=replicas-mysql-bin
binlog_cache_size=1M
binlog_format=mixed
slave_skip_errors=1062
default_authentication_plugin=mysql_native_password
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ciDockerfile
FROM mysql:latest
ADD ./mysql/master/cluster.cnf /etc/mysql/conf.d/cluster.cnf
ENV MYSQL_ROOT_PASSWORD=passwordSlave Node Configuration
cluster.cnf
[mysqld]
server_id=101
binlog-ignore-db=mysql
binlog_cache_size=1M
binlog_format=mixed
slave_skip_errors=1062
relay_log=replicas-mysql-relay-bin
log_slave_updates=1
read_only=1
default_authentication_plugin=mysql_native_password
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ciDockerfile
FROM mysql:latest
ADD ./mysql/slave/cluster.cnf /etc/mysql/conf.d/cluster.cnf
ENV MYSQL_ROOT_PASSWORD=passwordCreate Containers
docker-compose up -d mysql-master mysql-slaveIf the build takes too long, you can switch to a faster mirror source, such as:
NetEase: http://hub-mirror.c.163.com
Alibaba Cloud: http://your-id.mirror.aliyuncs...
USTC: http://docker.mirrors.ustc.edu.cn
After the containers start, run docker ps to verify they are running as expected.
Configure Slave Node
Enter the mysql-master container and run: show master status; Note the File and Position values; they will be used on the slave.
Then enter the mysql-slave container and execute:
CHANGE MASTER TO
MASTER_HOST='mysql-master',
MASTER_USER='root',
MASTER_PASSWORD=your_password,
MASTER_LOG_FILE='[File from previous step]',
MASTER_LOG_POS=[Position from previous step];
START SLAVE;
SHOW SLAVE STATUS \G;Test Replication
Log into the master, create a new database, then log into the slave and run show databases;. If the new database appears on the slave, replication is successful.
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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
