Pcornel
Browse topics
Options
Recent
Favorites
Databases · Technical reference

MySQL Binary Log and Replication

Direct answer

The binary log records data changes in MySQL/MariaDB; replicas read it to replicate, and it enables point-in-time recovery.

When to use this

Use it to set up replication or PITR, or when a replica stops.

First 30 seconds

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.

Operational verification

primary writes binlog → replica IO thread copies → SQL thread applies

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
MariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformMariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
Key commandSHOW BINARY LOG STATUS; -- MySQL 8.4 (SHOW MASTER STATUS earlier)
Scope / prerequisiteUse it to set up replication or PITR, or when a replica stops.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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.

Key points

Use row-based format and GTIDs. Keep binlogs long enough for replicas and backups (expire settings). Replica errors show in SHOW REPLICA STATUS.

Key checks
  • SHOW VARIABLES LIKE 'log_bin'; — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -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

FieldHealthyIf not
Replica_IO_RunningYesConnection/auth problem; see Last_IO_Error
Replica_SQL_RunningYesApplier stopped on an error; see Last_SQL_Error
Seconds_Behind_SourceSmallApplier slow; NULL means not running
Retrieved_Gtid_Set vs Executed_Gtid_SetCloseReceived but not yet applied

Settings worth knowing

SettingRecommendedWhy
binlog_formatROW (default)Deterministic; statement format can diverge
sync_binlog1Binlog matches committed data after a crash
read_only / super_read_onlyON on replicasStops accidental writes that break replication
replica_parallel_workers> 1 (default 4)Apply in parallel
binlog_expire_logs_seconds≥ backup interval + marginKeep 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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

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
Terminology layer

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.

Aliases that resolve here
mysql binary log and replicationmysql replicationbinlog gtid
Confirmation

Confirm action

Continue?