Use it to set up replication or PITR, or when a replica stops.
MySQL Binary Log and Replication
The binary log records data changes in MySQL/MariaDB; replicas read it to replicate, and it enables point-in-time recovery.
1. SHOW VARIABLES LIKE 'log_bin'; and binlog_format.
2. SHOW BINARY LOGS; retention.
3. Replica: SHOW REPLICA STATUS\G — IO/SQL running, Seconds_Behind_Source, Last_Error.
4. GTID mode consistent.
5. Fix error and restart replication.
primary writes binlog → replica IO thread copies → SQL thread applies
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
SHOW BINARY LOG STATUS; -- MySQL 8.4 (SHOW MASTER STATUS earlier)Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Check both threads are running and lag is low. After failover, confirm GTID sets match. Keep binlogs long enough to cover the time since the last backup.
Step 2 Common failures Narrow the fault
A replica stopped on an error keeps falling behind silently unless monitored. Writes on a replica cause conflicts. Purging binlogs too early breaks point-in-time recovery and lagging replicas.
Use row-based format and GTIDs. Keep binlogs long enough for replicas and backups (expire settings). Replica errors show in SHOW REPLICA STATUS.
SHOW VARIABLES LIKE 'log_bin';— first check for this topicpsql -c "SELECT now(), version();"— PostgreSQL reachable and versionmysql -e "SELECT NOW(), VERSION();"— MariaDB/MySQL reachable and versionsqlcmd -S <server> -Q "SELECT @@VERSION"— SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand
How it works
Each committed transaction is written to the binlog. The replica's IO thread downloads events into relay logs; the SQL (applier) thread replays them. Parallel apply can reduce lag.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
GTID-based replication
With gtid_mode = ON every transaction gets an ID source_uuid:sequence. A replica records which GTIDs it has applied (gtid_executed) and asks the source only for missing ones, so failover does not require finding file names and positions.
-- on the replica
CHANGE REPLICATION SOURCE TO SOURCE_HOST='db1', SOURCE_USER='repl',
SOURCE_PASSWORD='<secret>', SOURCE_AUTO_POSITION=1, SOURCE_SSL=1;
START REPLICA;
SHOW REPLICA STATUS\G
Reading SHOW REPLICA STATUS
| Field | Healthy | If not |
|---|---|---|
Replica_IO_Running | Yes | Connection/auth problem; see Last_IO_Error |
Replica_SQL_Running | Yes | Applier stopped on an error; see Last_SQL_Error |
Seconds_Behind_Source | Small | Applier slow; NULL means not running |
Retrieved_Gtid_Set vs Executed_Gtid_Set | Close | Received but not yet applied |
Settings worth knowing
| Setting | Recommended | Why |
|---|---|---|
binlog_format | ROW (default) | Deterministic; statement format can diverge |
sync_binlog | 1 | Binlog matches committed data after a crash |
read_only / super_read_only | ON on replicas | Stops accidental writes that break replication |
replica_parallel_workers | > 1 (default 4) | Apply in parallel |
binlog_expire_logs_seconds | ≥ backup interval + margin | Keep binlogs for PITR and lagging replicas |
Fixing a stopped applier
Read Last_SQL_Error first. Skipping a transaction (injecting an empty GTID) makes the replica silently different from the source; prefer re-cloning the replica (CLONE INSTANCE or a fresh backup) when data has diverged.
Common mistakes
- Binlogs purged before replicas read them.
ReferenceConfiguration & state1 section · depth on demand
Replication commands
SHOW BINARY LOG STATUS; -- MySQL 8.4 (SHOW MASTER STATUS earlier)
SHOW REPLICA STATUS\G -- Replica_IO_Running, Replica_SQL_Running, Seconds_Behind_Source
SHOW BINARY LOGS;
PURGE BINARY LOGS BEFORE NOW() - INTERVAL 7 DAY;
Use row-based format (binlog_format=ROW) and GTIDs for easier failover. Set binlog_expire_logs_seconds so logs do not fill the disk, but keep them long enough for point-in-time recovery and lagging replicas.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.