How to Build MySQL Master‑Slave and Redis Clusters with Docker
This guide explains the principles of MySQL master‑slave replication, shows how to configure and verify replication, provides Dockerfiles and docker‑compose files for MySQL and Redis clusters, and demonstrates testing each cluster step by step.
Building MySQL Master‑Slave Cluster with Docker
MySQL replication (master‑slave) creates data redundancy and high availability by synchronizing changes from the master to one or more slaves.
Why Use Master‑Slave?
Read‑write separation improves performance: the master handles writes, slaves handle reads.
Real‑time backup stores master data on slaves.
High availability allows automatic failover when a node fails.
Core Workflow
Binary Log – the master records all data‑changing operations (INSERT, UPDATE, DELETE) in a binary log.
I/O Thread – the slave connects to the master and streams the binary log.
Relay Log – the slave writes the received binary log to a local relay log and replays it with its SQL thread.
Data Consistency – ordered execution of logs guarantees consistency; asynchronous mode may have slight lag, semi‑synchronous waits for one slave acknowledgment, and full synchronous waits for all slaves.
Replication Modes
Asynchronous replication – high performance, low latency, but possible lag.
Semi‑synchronous replication – master waits for at least one slave to confirm receipt.
Full synchronous replication – master waits for all slaves, ensuring stronger consistency at the cost of performance.
Configuration Steps
Enable binary logging and set a unique server‑id on the master:
[mysqld]
log-bin=mysql-bin
server-id=1 # unique identifierCreate a replication user for the slaves:
CREATE USER 'repl'@'%' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';On each slave, specify the master connection details:
CHANGE MASTER TO
MASTER_HOST='master_host',
MASTER_USER='repl',
MASTER_PASSWORD='password',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=120;
START SLAVE;Verify the setup with SHOW SLAVE STATUS\G and SHOW MASTER STATUS\G.
Docker Setup
Dockerfiles for master and slave use mysql:5.7.36, set the timezone, and copy initialization scripts.
# Example master Dockerfile snippet
FROM mysql:5.7.36
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtimeThe docker‑compose.yml defines three services (master, slave, slave2) with volumes, ports, environment variables, privileged mode, and command‑line options for server‑id, binary log, and other settings.
services:
mysql-master:
build:
context: ./
dockerfile: ./master/Dockerfile
image: mysqlmaster:1.0
container_name: mysql-master
volumes:
- ./mastervarlib:/var/lib/mysql
ports:
- "8080:3306"
environment:
MYSQL_ROOT_PASSWORD: root
privileged: true
command: [
'--server-id=1',
'--log-bin=master-bin',
'--binlog-ignore-db=mysql',
'--binlog_cache_size=256M',
'--binlog_format=mixed',
'--lower_case_table_names=1',
'--character-set-server=utf8',
'--collation-server=utf8_general_ci'
]
# slave and slave2 definitions omitted for brevityBuild and start the stack with docker compose build and docker compose up -d, then test replication by creating a database and table on the master and confirming their presence on the slaves.
Deploying a Redis Cluster with Docker
Redis Cluster provides sharding, high availability, horizontal scaling, and a leader‑less architecture.
Core Features
Data sharding using 16 384 hash slots; each key maps to a slot and a specific node.
Each master can have multiple slaves for failover and read‑only queries.
Nodes can be added or removed dynamically without downtime.
No single central node, avoiding a single point of failure.
Configuration
Create redis.conf with cluster settings:
daemonize no
port 6379
dir /data/redis
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
requirepass 123456
masterauth 123456
bind * -::*Dockerfile
Multi‑stage build compiles Redis from source in an Ubuntu builder and copies the binaries into a lightweight runtime image.
FROM ubuntu:22.04 AS buildstage
RUN apt-get update && apt-get install -y build-essential wget gcc g++ make && rm -rf /var/lib/apt/lists/*
ADD redis-7.0.15.tar.gz /
WORKDIR /redis-7.0.15
RUN make
RUN mv src/redis-server /redis/ && mv src/redis-cli /redis/
ENTRYPOINT ["/redis/redis-server","/redis/redis.conf"]
FROM ubuntu:22.04
RUN mkdir -p /data/redis /redis
COPY --from=buildstage /redis /redis
EXPOSE 6379
ENTRYPOINT ["/redis/redis-server","/redis/redis.conf"]Docker‑Compose
Define seven services (six Redis nodes plus a helper that creates the cluster). Each node has a health check that runs /redis/redis-cli ping. The helper service runs redis-cli --cluster create with the six nodes and one replica per master.
services:
redis01:
image: myredis:1.0
ports: ["6379:6379"]
healthcheck:
test: /redis/redis-cli ping
interval: 10s
timeout: 5s
retries: 10
redis02:
image: myredis:1.0
healthcheck: {test: /redis/redis-cli ping, interval: 10s, timeout: 5s, retries: 10}
# redis03‑redis06 similar
redis07:
entrypoint: ["/redis/redis-cli","--cluster","create","redis01:6379","redis02:6379","redis03:6379","redis04:6379","redis05:6379","redis06:6379","--cluster-replicas","1","-a","123456","--cluster-yes"]
depends_on:
redis01:
condition: service_healthy
redis02:
condition: service_healthy
redis03:
condition: service_healthy
redis04:
condition: service_healthy
redis05:
condition: service_healthy
redis06:
condition: service_healthyBuild the image ( docker build -t myredis:v1.0 .), start the stack ( docker compose up -d), and verify the cluster with redis-cli -c -a 123456 cluster info and cluster nodes. Set and get a key to confirm operation.
The remainder of the original document contains promotional material unrelated to the technical tutorial and has been omitted.
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
