Overview of the Binary Log
Introduction to the purpose and structure of the binary log, explaining how it records data changes (DML) and structure changes (DDL) for replication and recovery.
The binary log contains a record of all changes to the databases, both data and structure, as well as how long each statement took to execute. It consists of a set of binary log files and an index.
This means that statements such as CREATE, ALTER, INSERT, UPDATE and DELETE are logged, whereas statements that have no effect on the data, such as SELECT and SHOW, are not be logged. If you want to log these, too (at a cost in performance), use the general query log.
If a statement may potentially have an effect, but doesn't, such as an UPDATE or DELETE that returns no rows, it is still logged (this applies to the default statement-based logging, not to row-based logging - see Binary Log Formats).
The purpose of the binary log is:
To enable replication, where data is sent from one or more primary servers to one or more replica servers based on the contents of the binary log.
To assist in backup operations.
A MariaDB server with the binary log enabled runs slightly more slowly.
It is important to protect the binary log, as it may contain sensitive information, including passwords.
Binary logs are stored in a binary, not plain text, format, and so are not readable with a regular editor. However, MariaDB includes mariadb-binlog, a command-line tool for plain-text processing of binary logs.
Binary Log Checkpoints
When the binary log is enabled, it acts as the transaction coordinator for two-phase commit between the binary log and transactional storage engines such as InnoDB. Each such transaction gets an internal XID, which is written to the binary log when the transaction commits. This applies to ordinary transactions, not only to XA transactions started with XA START.
If the server crashes, InnoDB may hold transactions that are prepared but not yet committed. During crash recovery, the server scans the binary log for the XIDs of those transactions: a prepared transaction whose XID is in the binary log is committed, and one whose XID is missing is rolled back.
A binary log checkpoint records the oldest binary log file that crash recovery still needs to scan. A file is no longer needed once the storage engines have durably committed, in their own logs, all transactions written to it. Crash recovery starts from the checkpointed file instead of scanning every binary log file.
The server records checkpoints as Binlog_checkpoint events:
Every new binary log file starts with a
Binlog_checkpointevent, written after theFormat_descandGtid_listevents.After a rotation, the server asks the storage engines to make their commits durable in the background. When all transactions in the oldest still-needed file are durable, the server writes a new
Binlog_checkpointevent naming the next file still needed. A single binary log file can therefore contain more than one checkpoint event.During crash recovery, the server uses the last
Binlog_checkpointevent in the most recent binary log file and scans from the file it names onward.
A binary log file that crash recovery may still need cannot be purged. PURGE BINARY LOGS stops at such a file and returns a note, for example:
Automatic purging stops at the same point and writes the message to the error log.
Binlog_checkpoint events are used only for crash recovery on the server that wrote them; replication does not use them. They appear in SHOW BINLOG EVENTS output:
The Info column shows the file that crash recovery would start from.
In mariadb-binlog output, a checkpoint event appears as a line ending in Binlog checkpoint <file_name>.
See Also
BINLOG_CHECKPOINT_EVENT (replication protocol)
This page is licensed: CC BY-SA / Gnu FDL
Last updated
Was this helpful?

