Replication is how MySQL scales reads, survives failures and feeds change data capture.
MySQL replication is built on the binary log (binlog), which records every committed change on the source. Replicas connect, copy the events into a relay log (the I/O, or receiver, thread), and apply them (applier threads, parallel by default in 8.x).
Key choices:
- binlog_format = ROW (the default): logs the actual row changes, which is deterministic and safe. Statement-based logging can diverge on non-deterministic statements.
- GTIDs (global transaction identifiers,
gtid_mode = ON,enforce_gtid_consistency = ON): each transaction gets an id likeuuid:42. Replicas track which GTIDs they've applied, so you point a replica at any source withSOURCE_AUTO_POSITION = 1instead of hunting for file names and positions. Essential for failover. - Asynchronous by default; semi-synchronous replication (a plugin) makes commit wait until at least one replica has received the transaction.
MySQL 8.4 uses source/replica terminology only: CHANGE REPLICATION SOURCE TO, START REPLICA, SHOW REPLICA STATUS. Monitor Replica_IO_Running, Replica_SQL_Running and Seconds_Behind_Source, or the performance_schema.replication_* tables.
Make replicas super_read_only = ON so nothing writes to them by accident. Provision new replicas with the clone plugin or a backup, then start replication from that point.
Example
-- my.cnf on both servers (unique server_id each)
-- server_id = 1 # 2 on the replica
-- gtid_mode = ON
-- enforce_gtid_consistency = ON
-- on the source
CREATE USER 'repl'@'10.0.%' IDENTIFIED BY 'repl-password';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.0.%';
-- on the replica (after loading a copy of the data)
CHANGE REPLICATION SOURCE TO
SOURCE_HOST = '10.0.0.10',
SOURCE_USER = 'repl',
SOURCE_PASSWORD = 'repl-password',
SOURCE_AUTO_POSITION = 1,
SOURCE_SSL = 1;
START REPLICA;
SET GLOBAL super_read_only = ON;
SHOW REPLICA STATUS\G
-- Replica_IO_Running: Yes Replica_SQL_Running: Yes Seconds_Behind_Source: 0Remember
- Replication ships binlog events from source to replicas
- Use ROW format and GTIDs with SOURCE_AUTO_POSITION
- 8.4 uses CHANGE REPLICATION SOURCE TO / START REPLICA syntax
Watch out for
- Writing to a replica by accident because super_read_only wasn't set.