生成新的binlog
1 | flush logs; |
清空binlog
1 | reset master; |
格式
Binlog files start with a Binlog File Header followed by a series of Binlog Event
Header + Event + Event + Event
A binlog file starts with a Binlog File Header
[ fe 'bin' ]
Event
EventHeader + Event
Binlog Event header
The binlog event header starts each event and is either 13 or 19 bytes long, depending on the binlog version.
1 | 4 timestamp |
1 | +---------+---------+---------+------------+-------------+-------+ |
Fields
- timestamp (4) – seconds since unix epoch
- event_type (1) – see Binlog Event Type
- server_id (4) – server-id of the originating mysql-server. Used to filter out events in circular replication.
- event_size (4) – size of the event (header, post-header, body)
- log_pos (4) – position of the next event
- flags (2) – see Binlog Event Flag
Table 14.5 Binlog Versions
Binlog version | MySQL Version |
---|---|
1 | MySQL 3.23 - < 4.0.0 |
2 | MySQL 4.0.0 - 4.0.1 |
3 | MySQL 4.0.2 - < 5.0.0 |
4 | MySQL 5.0.0+ |
目前市面上都是V4了,故EventHeader的长度可以默认为19
Binlog::FORMAT_DESCRIPTION_EVENT:
FORMAT_DESCRIPTION_EVENT 是binlog文件的第一个 Event
1 | //https://sourcegraph.com/github.com/mysql/mysql-server/-/blob/libbinlogevents/src/binlog_event.cpp#L84:1 |
A format description event is the first event of a binlog for binlog-version 4. It describes how the other events are layed out.
Row Based Replication Events
In Row Based replication the changed rows are sent to the slave which removes side-effects and makes it more reliable. Now all statements can be sent with RBR though. Most of the time you will see RBR and SBR side by side.
- TABLE_MAP_EVENT
- DELETE_ROWS_EVENTv0
- UPDATE_ROWS_EVENTv0
- WRITE_ROWS_EVENTv0
- DELETE_ROWS_EVENTv1
- UPDATE_ROWS_EVENTv1
- WRITE_ROWS_EVENTv1
- DELETE_ROWS_EVENTv2
- UPDATE_ROWS_EVENTv2
- WRITE_ROWS_EVENTv2
1 | 00000004 87 77 d4 5e|0f|01 00 00 00|79 00 00 00|7d 00 00 |.w.^.....y...}..| |
Event Header
timestamp: 87 77 d4 5e #1590982535
event type: 0f # FORMAT_DESCRIPTION_EVENT
server-id: 01 00 00 00 #1
event-size: 79 00 00 00 #121
log pos: 7d 00 00 00 #125 position of the next event
flag: 00 00 #0
Event Body (FORMAT_DESCRIPTION_EVENT)
binlog-version: 04 00 #4
mysql-server version: 38 2e 30 2e 32 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 #8.0.20
create timestamp: 87 77 d4 5e #1590982535
event header length: 13 #19 固定值
event type header lengths: 00 0d 00 08 00 00 00 00 04 00 04 00 00 00 # 长度(15-1)
data length: 61 #97 ==> #check sum block = event-size - event header length - data length = 121-19-97 = 5
skip: 121(eventSize)-19(header)-2(binlogVersion)-50(serverVersion)-4(time)-1(headerLength)-14(typeHeaderLength)-1(dataLength)-5(checkSumBlock) = 25
check sum type: 01 #1 CRC32
1 | post-header: |
1 | +=====================================+ |