A MySQL primary-replica pair is the lowest-friction way to add a read-only copy of your data — for query offload, for backup without touching production, or as the first half of a manual failover plan. This article covers GTID-based asynchronous replication on MySQL 8, the consistent-snapshot bootstrap that avoids re-syncing terabytes, and the lag-monitoring you need running before you point any reader at the replica.
How to verify
On a working pair, four queries tell you whether replication is healthy. Run them on the replica.
-- Threads up, IO and SQL running, no errors
SHOW REPLICA STATUS\G
-- Lag in seconds (approximation — use heartbeat for precision)
SELECT TIMESTAMPDIFF(SECOND, MAX(ts), NOW()) AS seconds_behind
FROM mysql.heartbeat;
-- GTID positions — replica should be at or near the primary's UUID:GTID set
SELECT @@gtid_executed;
-- Replica's own write ability (should be 0 for read-only role)
SHOW VARIABLES LIKE 'read_only';
In SHOW REPLICA STATUS: Replica_IO_Running and Replica_SQL_Running both Yes, Last_Errno = 0, Seconds_Behind_Source low or 0. If Seconds_Behind_Source is NULL, the IO thread is disconnected — fix that first.
What’s happening
Asynchronous primary-replica replication has the primary write its changes to a binary log (binlog), the replica’s IO thread pull binlog events over the network, and the replica’s SQL thread apply them. With GTIDs (Global Transaction Identifiers, introduced in MySQL 5.6, made the default in 8 with gtid_mode = ON), every transaction has a globally unique ID — the replica tracks which IDs it has executed, and on reconnection or failover the system uses the ID set instead of file-and-position coordinates.
Asynchronous means: the primary commits without waiting for the replica to apply. The replica can lag — milliseconds normally, hours during a long-running migration. Use semi-synchronous replication when you cannot tolerate lag at commit time, or use synchronous replication (Group Replication, Galera) when you need write quorums.
A bootstrap requires a consistent snapshot of the primary — either a mysqldump --single-transaction --master-data=2 or a physical backup with Percona XtraBackup. The snapshot ships to the replica, the GTID position is set, and START REPLICA resumes from that point.
The procedure
-
Configure the primary. Edit
/etc/mysql/mysql.conf.d/mysqld.cnfand restart.[mysqld] server-id = 1 log_bin = /var/log/mysql/mysql-bin binlog_format = ROW binlog_row_image = MINIMAL gtid_mode = ON enforce_gtid_consistency = ON binlog_expire_logs_seconds = 604800 # 7 days sync_binlog = 1 innodb_flush_log_at_trx_commit = 1Restart:
sudo systemctl restart mysql. -
Create a replication user on the primary.
CREATE USER 'repl'@'10.0.0.%' IDENTIFIED WITH caching_sha2_password BY 'strong-password' REQUIRE SSL; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.0.0.%'; -
Configure the replica. Note the different
server-id. Read-only by default.[mysqld] server-id = 2 log_bin = /var/log/mysql/mysql-bin relay_log = /var/log/mysql/mysql-relay-bin binlog_format = ROW gtid_mode = ON enforce_gtid_consistency = ON read_only = ON super_read_only = ON skip_replica_start = ON -
Take a consistent snapshot on the primary. Either logical (
mysqldump) or physical (xtrabackup). For logical:mysqldump --all-databases --single-transaction --triggers --routines --events \ --master-data=2 --set-gtid-purged=ON --hex-blob \ | gzip > /tmp/primary-bootstrap.sql.gz scp /tmp/primary-bootstrap.sql.gz replica:/tmp/ -
Load the snapshot on the replica.
gunzip < /tmp/primary-bootstrap.sql.gz | mysql -u root -pThe dump includes a
SET @@GLOBAL.GTID_PURGED = '<uuid:gtidset>'line which seeds the replica’s executed-GTID state to match the primary at the moment of dump. -
Configure the primary connection on the replica.
CHANGE REPLICATION SOURCE TO SOURCE_HOST = '10.0.0.5', SOURCE_PORT = 3306, SOURCE_USER = 'repl', SOURCE_PASSWORD = 'strong-password', SOURCE_AUTO_POSITION = 1, SOURCE_SSL = 1; START REPLICA; -
Confirm replication is running.
SHOW REPLICA STATUS\GReplica_IO_Running: Yes,Replica_SQL_Running: Yes,Last_Errno: 0. IfSeconds_Behind_Sourceis nonzero on first start, that is normal — it will drain. -
Install a heartbeat table for accurate lag measurement (
Seconds_Behind_Sourceis misleading on idle systems).-- On the primary CREATE DATABASE IF NOT EXISTS mysql; CREATE TABLE IF NOT EXISTS mysql.heartbeat (id INT PRIMARY KEY, ts TIMESTAMP(6)); INSERT INTO mysql.heartbeat VALUES (1, NOW(6)) ON DUPLICATE KEY UPDATE ts = NOW(6);Cron a tick every second on the primary:
* * * * * for i in $(seq 1 60); do mysql -uroot -e "UPDATE mysql.heartbeat SET ts=NOW(6) WHERE id=1"; sleep 1; doneOn the replica,
SELECT TIMESTAMPDIFF(MICROSECOND, ts, NOW(6)) FROM mysql.heartbeat;gives true lag.
Common pitfalls
- A replica with
read_only = ONstill accepts writes from accounts with SUPER. Usesuper_read_only = ONto fully lock it. - Replicating from a 5.7 primary to an 8.0 replica works; the reverse does not. Always upgrade replicas first.
binlog_format = STATEMENTwill cause silent divergence on any non-deterministic query. ROW is the only safe choice.- A replica that falls behind by hours has likely hit a single big transaction — long DDL on a giant table, a
DELETEwithoutLIMIT. The SQL thread is single-threaded by default; enablereplica_parallel_workers = 4(or 8) andreplica_parallel_type = LOGICAL_CLOCKto parallelize replay. - Reverse the roles for a failover and you must remember to unset
read_onlyon the new primary and set it on the new replica. Automation (Patroni-equivalent for MySQL: Orchestrator, MHA) handles this; manual flips often miss it.
In the engagements we run, MySQL replication topology is documented in code (server-id assignments, GTID UUIDs, network paths), monitored end-to-end with Prometheus + the mysqld_exporter, and tested with quarterly failover drills. We operate that under Managed Operations — and for clients who need automated promotion with sub-minute RTO, we wire Orchestrator into the topology.