mysqlbinlog使用

定位位置(Position)

要执行精确的时间点恢复 (PITR),我们不仅需要知道删除了什么,还需要知道这个操作在 Binlog 文件中的精确位置(Pos),以便告诉 MySQL 在到达这个点之前停止重放日志

1
mysqlbinlog /var/lib/mysql/binlog.000002 | grep -e 'DROP TABLE user' -C 5
1
2
3
4
5
# at 23097 起始位置 (Start Position)
# TTTTTTTT 2025-10-15 05:00:05 server id 1 end_log_pos 23164 Query thread_id=1234 exec_time=0 error_code=0
DROP TABLE `user` /* generated by server */;
# at 23164 下一个事件的起始位置
# ...

导出安全的恢复 SQL 文件

1
mysqlbinlog --stop-position=23097  /var/lib/mysql/binlog.000002 > ~/safe_recovery.sql

执行时间点恢复 (PITR)

1
mysql -u 用户 -p 密码 数据库 < ~/safe_recovery.sql