There is plenty of binlog documentation, but few people can name every byte. This post takes an 891-byte file from a minimal transaction and walks the event chain.
Why the format matters
- Estimating how many bytes a transaction occupies when chasing replica lag;
- Writing parsers that skip events by reading only the header;
- Reading the source: whether
log_posmeans “start” or “end” changes how you reason about writes.
Environment
Stock MySQL 8.0.44, separate datadir, started with explicit flags:
M=~/mysql-server-mysql-8.0.44/build/runtime_output_directory/mysqld
D=~/work/day1
rm -rf "$D"; mkdir -p "$D"
$M --no-defaults --initialize-insecure --datadir="$D/data" --log-error="$D/init.log"
nohup $M --no-defaults \
--datadir="$D/data" --socket="$D/mysql.sock" --port=3405 --server-id=1 \
--log-bin="$D/data/mysql-bin" --binlog-format=ROW \
--log-error="$D/error.log" --pid-file="$D/mysqld.pid" > "$D/out.log" 2>&1 &
Create a file containing DDL and DML:
RESET MASTER;
CREATE DATABASE d1;
CREATE TABLE d1.t(id INT PRIMARY KEY, v VARCHAR(20)) ENGINE=InnoDB;
INSERT INTO d1.t VALUES (1,'a'),(2,'b');
FLUSH LOGS;
FLUSH LOGS leaves an empty file behind, so analyse the second newest one.
The 19-byte header
Little-endian:
timestamp(4) | type(1) | server_id(4) | event_size(4) | log_pos(4) | flags(2)
type selects the parser for the body; event_size includes the header, body and CRC32 checksum.
A 40-line parser
#!/usr/bin/env python3
import struct, sys
TYPES = {2:"QUERY", 3:"STOP", 4:"ROTATE", 15:"FORMAT_DESC", 16:"XID",
19:"TABLE_MAP", 30:"WRITE_ROWS", 33:"GTID", 34:"ANONYMOUS_GTID",
35:"PREVIOUS_GTIDS"}
d = open(sys.argv[1], "rb").read()
print(f"size={len(d)} magic={d[:4]!r}")
off = 4
while off + 19 <= len(d):
ts, typ, sid, size, pos, flags = struct.unpack("<IBIIIH", d[off:off+19])
print(f"off={off:>7} {TYPES.get(typ,typ):<15} size={size:<6} log_pos={pos:<8} next={off+size}")
if size <= 0: break
off += size
print("end offset =", off)
Output
size=891 magic=b'\xfebin'
off= 4 FORMAT_DESC size=122 log_pos=126 next=126
off= 126 PREVIOUS_GTIDS size=31 log_pos=157 next=157
off= 157 ANONYMOUS_GTID size=77 log_pos=234 next=234
off= 234 QUERY size=102 log_pos=336 next=336
off= 336 ANONYMOUS_GTID size=77 log_pos=413 next=413
off= 413 QUERY size=148 log_pos=561 next=561
off= 561 ANONYMOUS_GTID size=79 log_pos=640 next=640
off= 640 QUERY size=71 log_pos=711 next=711
off= 711 TABLE_MAP size=53 log_pos=764 next=764
off= 764 WRITE_ROWS size=49 log_pos=813 next=813
off= 813 XID size=31 log_pos=844 next=844
off= 844 ROTATE size=47 log_pos=891 next=891
end offset = 891
Event by event
The 891 bytes hold three transactions and one ROTATE:
| offset | Event | Meaning |
|---|---|---|
| 4 | FORMAT_DESC | format description; must be read first (version, event format, checksum) |
| 126 | PREVIOUS_GTIDS | executed GTID set at the end of the previous file (empty: 8 bytes + 19 header + 4 CRC) |
| 157 | ANONYMOUS_GTID + QUERY(102) | transaction 1: CREATE DATABASE (DDL autocommits) |
| 336 | ANONYMOUS_GTID + QUERY(148) | transaction 2: CREATE TABLE |
| 561 | ANONYMOUS_GTID + QUERY(71) + TABLE_MAP + WRITE_ROWS + XID | transaction 3: BEGIN + two rows + commit |
| 844 | ROTATE | not a user transaction: FLUSH LOGS records the next file |
Six conclusions
- The first four bytes are the magic
fe 62 69 6e(\xfeBin); - The first event must be the FDE — without it the rest cannot be decoded or verified;
log_posis the end offset of the event, solog_pos = off + sizealways holds; the finalROTATEgives844 + 47 = 891, the file size;- DDL is
GTID + QUERYwith noXID; DML isGTID + QUERY(BEGIN) + TABLE_MAP + WRITE_ROWS + XID; FLUSH LOGSappends aROTATEto the old file and starts an empty new one;- Because
autocommitis on, the three statements are three transactions (three GTIDs); wrapped inBEGIN ... COMMITthey would share one GTID and oneXID.
Three easy-to-miss details
| Detail | Explanation |
|---|---|
ANONYMOUS_GTID is 77 or 79 bytes | variable-length fields such as transaction_length and commit timestamps |
| Every event includes 4 bytes of CRC | default binlog_checksum=CRC32; SQL text length does not match event size |
XID is always 31 bytes | 19 header + 8 xid + 4 CRC |
Cross-check with mysqlbinlog
mysqlbinlog -v --base64-output=decode-rows "$FILE" \
| grep -nE "CREATE DATABASE|CREATE TABLE|BEGIN|Write_rows|Xid" | head
QUERY(102) maps to CREATE DATABASE, QUERY(148) to CREATE TABLE, QUERY(71) to BEGIN, WRITE_ROWS(49) to the two inserts.
Five self-test questions
- What are the first four bytes? — the binlog magic, identifying the file type;
- Why must the first event be the FDE? — later events depend on its format and checksum description;
- Does
log_posequaloff + size? — yes; the final ROTATE proves it (844 + 47 = 891); - Which events correspond to
CREATE DATABASE/TABLE? —QUERY; ROW inserts areTABLE_MAP + WRITE_ROWS, wrapped byBEGINandXID; - Where does a transaction start and end? — it starts at the GTID event; DML ends at
XID, DDL has noXID.
Next: before any of this reaches the binlog, events land in the binlog cache.
Follow this series
MySQL 8.0 source reading, one verifiable result at a time.