Home/Engineer/Databases/MySQL Replication: Setting U…
DatabasesAdvancedlinuxmysqlmariadbreplication

MySQL Replication: Setting Up Source-Replica (Master-Slave) HA, Diagnosing Lag, and Failing Over

A practitioner’s walkthrough of building Master-Slave (Source-Replica) high availability with MySQL/MariaDB binlog replication, covering GTID, semi-sync, lag diagnosis, and failover.

Contents
  1. What Is MySQL Replication?
  2. Replication Topology and Data Flow
  3. 1. Master Configuration
  4. 2. Replica Configuration
  5. 3. Diagnosing Replication Status
  6. 4. Semi-Synchronous Replication (Semi-Sync)
  7. 5. Replication Lag: Causes and Remedies
  8. 6. Recovering Broken Replication
  9. 7. Failover (Manual Promotion)
  10. Summary

What Is MySQL Replication?

MySQL replication is a built-in feature that asynchronously ships changes from one server (Master/Source) to another (Slave/Replica) so both stay in sync. The core mechanism is the binary log (binlog). The Master records every write event in the binlog, and the Replica receives those events and re-executes them until it reaches the same state.

  • Read load distribution: Send read queries to Replicas
  • High availability (HA): Promote a Replica if the Master fails
  • Backup offloading: Take dumps on a Replica to spare the Master

Starting with MySQL 8.0, the official terms changed from Master/Slave to Source/Replica. This guide uses both names for compatibility with older versions.


Replication Topology and Data Flow

CODE
[Master] --(binlog write)--> binlog
 | Dump Thread
   v
[Replica] -- IO Thread --> relay log -- SQL Thread -->
ThreadLocationRole
Binlog Dump ThreadMasterSends binlog events to the Replica
IO ThreadReplicaReceives events and writes them to the relay log
SQL ThreadReplicaReads the relay log and applies the events

There are three binlog formats: ROW (recommended), STATEMENT, and MIXED. Use ROW for the strongest data consistency.


1. Master Configuration

Edit /etc/mysql/mysql.conf.d/mysqld.cnf (or /etc/my.cnf).

INI
[mysqld]
#
server-id = 1

#
log_bin = /var/log/mysql/mysql-bin.log
binlog_format = ROW

# GTID
gtid_mode = ON
enforce_gtid_consistency = ON

# binlog — 7
binlog_expire_logs_seconds = 604800

# DB when
# binlog_do_db = appdb

After a restart, create a dedicated replication account.

SQL
-- Master run
CREATE USER 'repl'@'192.168.1.%' IDENTIFIED WITH caching_sha2_password BY 'StrongReplPass!2026';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.%';
FLUSH PRIVILEGES;

-- binlog check(-GTID method )
SHOW MASTER STATUS\G

Example output:

CODE
File: mysql-bin.000004
Position: 1547

If the Master is already in production, take a consistent snapshot with mysqldump --single-transaction --master-data=2 --source-data=2 and load it onto the Replica.


2. Replica Configuration

INI
[mysqld]
server-id = 2
relay_log = /var/log/mysql/mysql-relay-bin.log
gtid_mode = ON
enforce_gtid_consistency = ON

# Replica dedicated ( super_read_only)
read_only = ON
super_read_only = ON

# after save
relay_log_recovery = ON

Starting GTID-based replication (MySQL 8.0)

SQL
CHANGE REPLICATION SOURCE TO
  SOURCE_HOST = '192.168.1.10',
  SOURCE_USER = 'repl',
  SOURCE_PASSWORD = 'StrongReplPass!2026',
  SOURCE_PORT = 3306,
  SOURCE_AUTO_POSITION = 1,
  GET_SOURCE_PUBLIC_KEY = 1;

START REPLICA;

Coordinate (binlog position) method — older versions / MariaDB compatibility

SQL
CHANGE MASTER TO
  MASTER_HOST = '192.168.1.10',
  MASTER_USER = 'repl',
  MASTER_PASSWORD = 'StrongReplPass!2026',
  MASTER_LOG_FILE = 'mysql-bin.000004',
  MASTER_LOG_POS = 1547;

START SLAVE;

3. Diagnosing Replication Status

SQL
-- MySQL 8.0
SHOW REPLICA STATUS\G
-- /MariaDB
SHOW SLAVE STATUS\G

Key fields to check:

FieldHealthy valueMeaning
Replica_IO_RunningYesConnected to the Master and receiving events
Replica_SQL_RunningYesApplying the relay log normally
Seconds_Behind_Source0Replication lag (seconds)
Last_IO_Error(empty)Network/authentication errors
Last_SQL_Error(empty)SQL errors during apply

Replication is healthy when both the IO and SQL threads show Yes and Seconds_Behind_Source is 0 or a low value.


4. Semi-Synchronous Replication (Semi-Sync)

With asynchronous replication, the Master does not wait for Replicas to receive events, so failover can lose data. Semi-sync finishes a commit only after at least one Replica has acknowledged that it wrote the event to its relay log.

SQL
-- Master
INSTALL PLUGIN rpl_semi_sync_source SONAME 'semisync_source.so';
SET GLOBAL rpl_semi_sync_source_enabled = 1;
SET GLOBAL rpl_semi_sync_source_timeout = 1000; -- ms, when

-- Replica
INSTALL PLUGIN rpl_semi_sync_replica SONAME 'semisync_replica.so';
SET GLOBAL rpl_semi_sync_replica_enabled = 1;
STOP REPLICA IO_THREAD; START REPLICA IO_THREAD;

After the timeout it automatically falls back to asynchronous mode, so semi-sync is not “zero data loss.” If you truly need no data loss, look at Group Replication / InnoDB Cluster.


5. Replication Lag: Causes and Remedies

SymptomCauseRemedy
SQL Thread apply lagSingle-threaded applyParallel apply with replica_parallel_workers
Sudden spike at a point in timeLarge batch jobs / ALTERNightly batches, pt-online-schema-change
IO Thread lagNetwork bandwidthCompression (SOURCE_COMPRESSION_ALGORITHMS)
SQL
--
STOP REPLICA SQL_THREAD;
SET GLOBAL replica_parallel_type = 'LOGICAL_CLOCK';
SET GLOBAL replica_parallel_workers = 4;
SET GLOBAL replica_preserve_commit_order = ON;
START REPLICA SQL_THREAD;

6. Recovering Broken Replication

If the SQL Thread has stopped because of a data conflict, identify the cause and skip the event at GTID granularity.

SQL
-- GTID check after,
SET GTID_NEXT = 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee:42';
BEGIN; COMMIT;
SET GTID_NEXT = 'AUTOMATIC';
START REPLICA;

Skipping an event leaves data inconsistency behind. Confirm the root cause, and if consistency is in doubt, compare Master and Replica with pt-table-checksum.


7. Failover (Manual Promotion)

This is the basic procedure for promoting a Replica to the new Master when the Master fails.

SQL
-- 1) Replica
STOP REPLICA;
RESET REPLICA ALL;

-- 2) dedicated
SET GLOBAL super_read_only = OFF;
SET GLOBAL read_only = OFF;

Then point the application’s DB endpoint at the new Master. If you need automation, use tools such as Orchestrator, MHA, ProxySQL, or MySQL Router.


Summary

ItemRecommended setting
binlog formatROW
Replication coordinatesGTID (SOURCE_AUTO_POSITION=1)
Replica protectionsuper_read_only = ON
Preventing data lossSemi-sync + a short timeout
Apply performanceParallel replication (LOGICAL_CLOCK)
Consistency checksRun pt-table-checksum regularly
Automatic failoverOrchestrator / MHA / Router

Once you clearly understand the binlog → Dump → IO → SQL flow, diagnosis becomes straightforward. Combining GTID, semi-sync, and parallel replication lets you run read scaling and HA reliably.

#mysql#mariadb#replication#ha#binlog
Editorial note

AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us

Topic-related official docs (auto-linked · not per-claim sources)MySQL manual ↗

Questions & answers

Ask a question about this guide. We'll review and reply.