三種 Binlog 格式
| 格式 | 特點 | 適用場景 |
| STATEMENT | 記錄 SQL 文本,體積小 | 確定性函數、簡單業務 |
| ROW | 記錄行級變更,安全 | 非確定性函數、複雜觸發器 |
| MIXED | 自動切換,兼顧性能與安全 | 生產環境推薦默認 |
解析命令
mysqlbinlog --base64-output=DECODE-ROWS -v mysql-bin.000144 | more
--base64-output=DECODE-ROWS:解碼 ROW 格式事件為可讀 SQL
-v:顯示 STATEMENT 格式事件的 SQL 文本
輸出解讀示例
# at 171
#140216 23:59:26 server id 1 end_log_pos 260 Table_map: `billing`.`billing_account` mapped to number 216
#140216 23:59:26 server id 1 end_log_pos 418 Write_rows: table id 216 flags: STMT_END_F
### INSERT INTO billing.billing_account
### SET
### @1=80491
### @2=1000013580491
### @3=1000056980491
### @4=1
### @5=1
### @6='Integration-Tests'
### @7='2014-02-16 23:59:26'
...
# at 516
#140216 23:59:26 server id 1 end_log_pos 516 Table_map: `billing`.`credit_card_account` mapped to number 222
#140216 23:59:26 server id 1 end_log_pos 812 Write_rows: table id 222 flags: STMT_END_F
### INSERT INTO billing.credit_card_account
### SET ...
關鍵字段說明
Table_map:表名 ↔ 内部 ID 映射
Write_rows / Update_rows / Delete_rows:ROW 事件類型
@N:第 N 個列的值(順序對應表定義)
end_log_pos:事件結束位置,用於斷點恢復
實用技巧
- 過濾特定表:
mysqlbinlog --database=billing --table=billing_account ...
- 指定位置範圍:
--start-position=100 --stop-position=10000
- 導出 SQL 文件:
mysqlbinlog ... > replay.sql 供審計/回放
- MIXED 格式下同時看到
Query (STATEMENT) 與 Write_rows (ROW) 事件交織