Databases 6 min read

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.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Step‑by‑Step Guide to MySQL 8.0 Master‑Slave Replication with GTID

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   # slave

Restart MySQL after editing:

systemctl restart mysql

Step 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 settings

GTID 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=TRUE

On 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 info

Additional utilities:

SHOW VARIABLES LIKE 'server_id';   # check server_id
SHOW GLOBAL VARIABLES LIKE 'gtid_mode'; # check GTID status
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

databasemysqlMaster‑SlaveReplicationGTID
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.