What is a Binlog?

A binlog (binary log) is a transaction log used by MySQL and MariaDB to record all changes made to the database. It is crucial for replication, point-in-time recovery, and auditing.

Key Features of Binlog

  1. Records Database Changes – Logs INSERT, UPDATE, DELETE statements, but not SELECT statements.
  2. Supports Replication – Used to synchronize changes between master and replica databases.
  3. Point-in-Time Recovery – Enables database restoration up to a specific transaction in case of failure.

Types of Events in a Binlog

  • DML Events: INSERT, UPDATE, DELETE
  • DDL Events: CREATE, ALTER, DROP
  • Transaction Control: BEGIN, COMMIT, ROLLBACK

Binlog Formats

  1. Statement-Based Logging (SBR) – Logs SQL statements (prone to non-deterministic issues).
  2. Row-Based Logging (RBR) – Logs actual row changes (more reliable for replication).
  3. Mixed Logging (MBR) – Uses both SBR and RBR depending on the operation.

Common Binlog Commands

  • Enable binlog: log_bin = mysql-bin (in MySQL config)
  • Check binlog status: SHOW BINARY LOGS;
  • View binlog contents: mysqlbinlog mysql-bin.000001