Skip to content

How MygramDB Reads the Binlog ​

MygramDB connects to MySQL or MariaDB as a replica, receives the raw binlog stream, and decodes it with libmybinlog, a separate library that turns binlog bytes into typed events and row values. This page follows one change from the source server to the search index and explains what happens when an event cannot be applied.

What is the binlog?

The binlog is the log in which MySQL and MariaDB record every committed change. In ROW format each entry names the table and carries the changed rows. MygramDB needs three things from it: which rows changed, their new values, and a position (a GTID) to resume from after a restart. See the glossary.

The Decode Path ​

libmybinlog has no network code. MygramDB owns the connection, authentication and the dump request through the MySQL C client API, and passes each event the connection delivers to libmybinlog's parsers. MygramDB then filters the decoded events to the configured tables and applies them to the index and document store.

Opening the Stream ​

The two servers use different dump commands, so MygramDB has one stream implementation per server type. Detection uses SELECT @@GLOBAL.gtid_binlog_pos, a variable only MariaDB has.

MySQLMariaDB
Dump commandCOM_BINLOG_DUMP_GTIDCOM_BINLOG_DUMP
How the start position is sentGTID set in the dump request, encoded by libmybinlog@slave_connect_state session variable, set before the dump
Checksum requested@source_binlog_checksum='CRC32'@master_binlog_checksum='CRC32'
Other session setupHeartbeat every 3 secondsHeartbeat every 3 seconds, GTID capability, strict GTID mode

MariaDB omits its GTID events unless the replica advertises GTID capability (@mariadb_slave_capability = 4). MygramDB sets it so that every transaction arrives with the GTID it can record. Strict GTID mode and the heartbeat are best-effort: if the server rejects them, the stream still opens.

What libmybinlog Does ​

StepWhat happensIf it fails
Framing and checksumReads the event header, checks the CRC32 trailer, and re-derives the checksum algorithm at each FORMAT_DESCRIPTION eventReconnects from the last processed GTID
TABLE_MAPReads column types and, for monitored tables, the column names, signedness and ENUM/SET labelsStops with 2017 for an unknown column type or for missing or malformed metadata
GTIDReads the GTID that precedes each transactionA complete but invalid GTID event (for example transaction number 0) is reported as 2017; a truncated one is reported as a generic binlog error. Both reconnect from the last processed GTID
ROWS eventsDecodes WRITE_ROWS, UPDATE_ROWS and DELETE_ROWS into row values for monitored tablesA row-decoding failure is reported as 2008 and follows normal reconnect handling
QUERYProvides statement text for transaction boundaries and DDL on configured tablesA statement that may change rows but cannot be recognized stops with 2017

Row values are rendered as the same text the initial snapshot produces, so a row indexed from the snapshot and the same row updated from the binlog yield one document. Column types that cannot be rendered identically on both paths are refused in the configuration; see Supported Column Types.

Event Disposition ​

Skipping an event is safe only if it cannot carry a row or a position. libmybinlog classifies every event type once, and MygramDB reduces that to three actions.

ActionEvent types
Decode and applyTABLE_MAP, GTID events, XID, QUERY, and uncompressed WRITE_ROWS/UPDATE_ROWS/DELETE_ROWS
SkipStream framing (ROTATE, FORMAT_DESCRIPTION), informational events such as HEARTBEAT, ROWS_QUERY, PREVIOUS_GTIDS and MariaDB ANNOTATE_ROWS, and a type this build does not know when the source marks it ignorable (LOG_EVENT_IGNORABLE_F)
Stop (fail closed)ANONYMOUS_GTID, XA_PREPARE, INCIDENT, EXECUTE_LOAD_QUERY, MySQL TRANSACTION_PAYLOAD and PARTIAL_UPDATE_ROWS, MariaDB compressed events, and any unknown type that is not marked ignorable

A stopped reader reports error 2017 and keeps the last applied GTID. The event stays in the binlog, so reconnecting from that position meets it again; changing the server setting that produced it does not remove it. Recovery is a SYNC that restarts the reader past the event. See Recovering from an undecodable binlog event for the procedure.

Checksums ​

Each binlog event can end with a 4-byte CRC32 trailer. A file declares whether its events carry one in its FORMAT_DESCRIPTION event, so libmybinlog re-derives the algorithm at every file boundary. It never reads the binlog_checksum server variable for this, because older files written under a previous setting can still be streamed.

MygramDB asks the source for CRC32 when it opens the stream, and refuses a source whose binlog_checksum is not CRC32 at startup. A frame that fails verification or arrives truncated is not applied; the reader reconnects from the last processed GTID.

GTIDs ​

MySQLMariaDB
One GTIDuuid:gno or uuid:tag:gnodomain-server-seq
Event before each transactionGTID or GTID_TAGGEDGTID (MariaDB type 162)
PositionSet of intervals per UUID and tagHighest sequence number per domain

MySQL 8.4 and later can add a tag to a GTID. A tagged transaction arrives as the same GTID with the tag set, and libmybinlog stores tags lower-cased. Tagged and untagged GTIDs from one UUID are tracked as separate entries, and a tagged set uses a different binary encoding in the dump request than an untagged one. MariaDB positions are compared by sequence number per domain; server ids differ legitimately after a failover and are ignored. For start positions and resume behavior, see GTID Position and Recovery.

A transaction's GTID becomes the applied position only when its commit is processed, after every row change in it has been applied. A transaction cut off by a disconnect is replayed from the previous position, not skipped.

Compressed Events ​

Both servers can compress the binlog, and libmybinlog can expand both kinds. MygramDB does not apply compressed events and refuses them.

What is compressedServer settingMygramDB
A whole MySQL transaction (TRANSACTION_PAYLOAD)binlog_transaction_compression=ONRefused at startup; stops with 2017 if it appears later
MariaDB QUERY and rows eventslog_bin_compress=ONRefused at startup; stops with 2017 if it appears later
One MariaDB COMPRESSED VARCHAR/TEXT column in an ordinary rows eventColumn attributeInflated before indexing

MySQL PARTIAL_JSON row values (binlog_row_value_options) are handled the same way as compression: refused at startup and fail-closed at runtime.

Server Settings Checked at Startup ​

libmybinlog holds the rules for which server variables a binlog reader depends on, and MygramDB evaluates them against SHOW VARIABLES before it starts reading. The checked variables are log_bin, gtid_mode (MySQL), binlog_format, binlog_row_image, binlog_checksum, binlog_row_metadata, the compression and PARTIAL_JSON settings above, and log_replica_updates (log_slave_updates on MariaDB) when the source is itself a replica. A violation refuses startup with 2008 and names the variable. The required values and their reasons are in MySQL Server Requirements.

A compression or PARTIAL_JSON setting enabled after startup is caught when the first affected event arrives, because the event classification above fails closed.

Building with libmybinlog ​

The build fetches libmybinlog from GitHub at a pinned release tag set in third_party/CMakeLists.txt, so a normal make needs no extra step. For an offline build, point MYBINLOG_SOURCE_DIR at a local checkout:

bash
make MYBINLOG_SOURCE_DIR=/path/to/libmybinlog

See Build from Source for the prerequisites.

Relationship to mysql-event-stream ​

mysql-event-stream is a standalone change-data-capture engine with Node.js and Python bindings. It implements its own replication connection and reads the wire format through the same libmybinlog. MygramDB and mysql-event-stream therefore share one set of decoding rules (checksums, GTIDs, row values, event disposition) while keeping separate connection code.

See Also ​