O'Reilly logo

Understanding MySQL Internals by Sasha Pachev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Binary Log Format

Learning some details of the binary log format can reveal a lot about the replication internals. The code that deals with the binary logging is found in sql/log_event.h and sql/log_event.cc.

The binary log starts with a 4-byte magic number, which is set in the following line of sql/log_event.h:

	#define BINLOG_MAGIC        "\xfe\x62\x69\x6e"

The magic number is used by the code that reads the binary log for a quick sanity check to make sure a valid binary log is being used. It is also used by the Unix file utility, which identifies file types.

The magic number is followed by a sequence of event entries. All events have a common header with the fields listed in Table 12-2 in sequential order. The second field represents the type code of the event, and is explained in Table 12-3. All of the integers in the header are stored in the little-endian format (low byte first). The header is written out by Log_event::write_header( ) in sql/log_event.cc.

Table 12-2. Binary log event header

Size (bytes)

Description

4

Timestamp of the event. Number of seconds since the start of the year 1970 as returned by the libc call time().

1

The type code of the event. The values and the meaning of the code are explained in Table 12-3.

4

Server ID. Uniquely identifies the server among its replication peers. Mainly used for avoiding infinite update loops.

4

The length of the whole event, including the header, in bytes.

4

Offset of the event in the log in bytes

2

Event flags. For details see sql/log_event.h; search ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required