Lab 01: MySQL Replication

Time: 45 minutes | Level: Advanced | DB: MySQL 8.0

Overview

Set up MySQL 8 binary log replication with GTID (Global Transaction Identifiers). You'll configure a primary node that streams changes to a replica via ROW-based binary logs — the foundation of all MySQL HA architectures.


Step 1: Launch Primary MySQL Container

# Create a Docker network so containers can communicate
docker network create mysql-replication

# Start the PRIMARY node
docker run -d \
  --name mysql-primary \
  --network mysql-replication \
  -e MYSQL_ROOT_PASSWORD=rootpass \
  -e MYSQL_DATABASE=testdb \
  mysql:8.0 \
  --server-id=1 \
  --log-bin=mysql-bin \
  --binlog-format=ROW \
  --gtid-mode=ON \
  --enforce-gtid-consistency=ON \
  --log-slave-updates=ON

# Wait for MySQL to be ready
for i in $(seq 1 30); do
  docker exec mysql-primary mysql -uroot -prootpass -e "SELECT 1" 2>/dev/null && break || sleep 2
done
echo "Primary ready!"

💡 server-id must be unique across all nodes in your replication topology. 1 = primary, 2 = first replica, etc.

📸 Verified Output:


Step 2: Create the Replication User on Primary

📸 Verified Output:

💡 MySQL 8 defaults to caching_sha2_password. Using mysql_native_password here avoids SSL certificate issues in lab environments. Production should use caching_sha2_password with SSL.


Step 3: Check Primary Binary Log Status

📸 Verified Output:


Step 4: Launch Replica MySQL Container

💡 --read-only=ON and --super-read-only=ON prevent accidental writes to the replica. super-read-only blocks even SUPER privileged users.


Step 5: Connect Replica to Primary

📸 Verified Output:

💡 Both Slave_IO_Running and Slave_SQL_Running must show Yes. Seconds_Behind_Master: 0 means fully caught up.


Step 6: Test Replication — Write on Primary, Read on Replica

📸 Verified Output:


Step 7: Monitor Replication with GTID

📸 Verified Output:

💡 GTID sets on primary and replica should match when fully synchronized. A GTID like uuid:1-8 means transactions 1 through 8 have been applied.


Step 8: Capstone — Simulate Failover & Promote Replica

📸 Verified Output:


Summary

Concept
Key Setting
Purpose

server-id

Unique integer per node

Identifies each MySQL in topology

binlog_format=ROW

--binlog-format=ROW

Replicates actual row changes (safest)

GTID

--gtid-mode=ON

Automatic position tracking

Replication User

GRANT REPLICATION SLAVE

Dedicated user for replica connections

CHANGE MASTER TO

MASTER_AUTO_POSITION=1

Connect replica using GTID

read_only

--read-only=ON

Prevent writes on replica

SHOW SLAVE STATUS

Slave_IO/SQL_Running: Yes

Health check for replication

Key Takeaways

  • ROW format is safest — replicates actual data changes, not SQL statements

  • GTID eliminates manual log position tracking and simplifies failover

  • Always monitor Seconds_Behind_Master — spikes indicate replica lag

  • Failover = STOP SLAVE → RESET SLAVE ALL → disable read_only

  • In production, use MHA, Orchestrator, or MySQL InnoDB Cluster for automated failover

Last updated