Step‑by‑Step Guide to MySQL 8.0 Master‑Slave Replication with GTID
This guide walks you through preparing multiple virtual machines, configuring MySQL 8.0 server IDs, setting up both classic and GTID‑based master‑slave replication, and managing replication commands, providing clear code snippets and commands for a reliable MySQL clustering environment.
Prerequisites
Prepare 3–5 virtual machines.
Install MySQL 8.0 on each machine.
Master‑Slave Architecture
Two common patterns are used: “one master many slaves” and “cascading replication”. Both rely on the binary log (binlog) to record DDL and certain DML statements; slaves replay these statements to stay synchronized.
Step 1: Configure server_id
Set a unique server_id for each MySQL instance. You can query the current value with: SHOW VARIABLES LIKE 'server_id'; Edit /etc/mysql/my.cnf (or /etc/mysql/my.conf) and add the following under [mysqld]:
[mysqld]
server-id=1 # master [mysqld]
server-id=2 # slaveRestart MySQL after editing:
systemctl restart mysqlStep 2 (Optional): Check master status
On the master run: SHOW MASTER STATUS; This displays the current binlog file name (File) and position (Position) as well as other replication‑related variables such as Binlog_Do_DB, Binlog_Ignore_DB, and Executed_Gtid_Set.
Step 3: Configure the slave
On the slave execute the CHANGE REPLICATION SOURCE command, filling in the master’s address and credentials:
CHANGE REPLICATION SOURCE TO
SOURCE_HOST='192.168.3.107',
SOURCE_USER='root',
SOURCE_PASSWORD='1234',
SOURCE_LOG_FILE='binlog.000009',
SOURCE_LOG_POS=0;Key fields: SOURCE_HOST: master IP or hostname SOURCE_USER: replication user SOURCE_PASSWORD: password SOURCE_LOG_FILE: current binlog file on master SOURCE_LOG_POS: position within the binlog
After running the command, verify the replica status: SHOW REPLICA STATUS; Common replica management commands:
SHOW REPLICA STATUS; # view status
START REPLICA; # begin replication
STOP REPLICA; # pause replication
RESET REPLICA ALL; # remove all replication settingsGTID Mode (Recommended)
Check GTID mode on the master: SHOW GLOBAL VARIABLES LIKE 'gtid_mode'; Enable GTID in the configuration:
[mysqld]
server-id=1
gtid_mode=ON
enforce_gtid_consistency=TRUEOn the slave, use GTID‑based replication which eliminates the need to specify SOURCE_LOG_FILE and SOURCE_LOG_POS:
CHANGE REPLICATION SOURCE TO
SOURCE_HOST='192.168.3.107',
SOURCE_USER='root',
SOURCE_PASSWORD='123',
SOURCE_AUTO_POSITION=1;Common Commands
Master:
SHOW MASTER STATUS; # view master status
RESET MASTER; # reset binlog (starts from 000001)Slave:
SHOW REPLICA STATUS; # view slave status
START REPLICA; # start replication
STOP REPLICA; # stop replication
RESET REPLICA ALL; # delete replication configuration
SELECT * FROM performance_schema.replication_applier_status_by_worker; # view thread infoAdditional utilities:
SHOW VARIABLES LIKE 'server_id'; # check server_id
SHOW GLOBAL VARIABLES LIKE 'gtid_mode'; # check GTID statusSigned-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.
