当前位置: 代码网 > it编程>数据库>Mysql > MySQL Binlog 日志查看方法及查看内容解析

MySQL Binlog 日志查看方法及查看内容解析

2025年06月30日 Mysql 我要评论
一、binlog 日志概述binlog(二进制日志)记录了 mysql 数据库执行的所有更改数据的操作,包括insert、update、delete等。它对于数据恢复、主从复制以及审计等方面有着至关重

一、binlog 日志概述

binlog(二进制日志)记录了 mysql 数据库执行的所有更改数据的操作,包括insert、update、delete等。它对于数据恢复、主从复制以及审计等方面有着至关重要的作用。

二、查看 binlog 日志方法

开启 binlog 日志功能

默认情况下,mysql 的 binlog 日志功能可能未开启。要开启它,需要修改 mysql 的配置文件(通常是my.cnf或my.ini)。在[mysqld]部分添加或修改以下配置:

log-bin=mysql-bin

这里mysql-bin是日志文件名前缀,重启 mysql 服务后,binlog 日志功能即开启。

查看当前正在使用的 binlog 日志文件

使用以下 sql 命令可以查看当前 mysql 正在写入的 binlog 日志文件名:

show master status;

执行结果类似如下:

+------------------+----------+--------------+------------------+-------------------+
| file | position | binlog_do_db | binlog_ignore_db | executed_gtid_set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 1234 | | | |
+------------------+----------+--------------+------------------+-------------------+

其中file列显示的mysql-bin.000003就是当前正在使用的 binlog 日志文件名,position表示当前日志写入的位置。

查看 binlog 日志内容

  1. 使用 mysqlbinlog 工具
    • 该工具随 mysql 安装包一同提供。在命令行中,使用以下语法查看 binlog 日志内容:
mysqlbinlog [选项] binlog文件名

例如,要查看mysql-bin.000003的内容,可以执行:

mysqlbinlog mysql-bin.000003
  • 常用选项:
    • --start-position和--stop-position:用于指定查看日志的位置范围。例如,只查看从位置 100 到 200 的内容:
mysqlbinlog --start-position=100 --stop-position=200 mysql-bin.000003
  • --start-datetime和--stop-datetime:根据时间范围查看日志。如查看 2025 - 04 - 01 10:00:00 到 2025 - 04 - 01 11:00:00 之间的日志:
mysqlbinlog --start-datetime="2025-04-01 10:00:00" --stop-datetime="2025-04-01 11:00:00" mysql-bin.000003
  1. 在 mysql 客户端中通过show binlog events命令
    • 语法如下:
show binlog events [in 'log_name'] [from pos] [limit [offset,] row_count];
  • 示例:查看mysql-bin.000003从位置 50 开始的 10 条日志事件:
show binlog events in'mysql-bin.000003' from 50 limit 0, 10;

三、binlog 日志内容解析

binlog 日志包含多个事件(event),每个事件记录了一次数据库操作。常见的事件类型及解析如下:

  1. format_desc 事件
    • 通常是 binlog 日志的第一个事件,用于描述该 binlog 日志的格式信息,包括日志版本、创建时间等。例如:
#120505 14:31:06 server id 1 end_log_pos 123 crc32 0xabcdef01 start: binlog v 4, server v 5.7.20 created 120505 14:31:06 at startup
  • 其中#120505 14:31:06是事件发生时间,server id 1是服务器 id,end_log_pos 123表示该事件结束的位置,crc32 0xabcdef01是 crc32 校验和。
  1. query 事件
    • 记录了一条 sql 查询语句,通常是insert、update、delete等更改数据的操作。例如:
#120505 14:32:00 server id 1 end_log_pos 256 crc32 0x12345678 query thread_id=3 exec_time=0 error_code=0
use testdb;
set timestamp=1336223520;
insert into users (name, age) values ('john', 25);
  • #120505 14:32:00是事件发生时间,server id 1是服务器 id,end_log_pos 256是事件结束位置。thread_id=3表示执行该查询的线程 id,exec_time=0是查询执行时间(秒),error_code=0表示执行无错误。下面的use testdb;指定了操作的数据库,set timestamp=1336223520;设置了时间戳,最后是具体的insert语句。
  1. row_event 系列事件(如 table_map 事件、write_rows 事件、update_rows 事件、delete_rows 事件)
    • 在基于行模式(row - based)的 binlog 记录中常见。
    • table_map 事件:用于映射表的结构信息,例如:
#120505 14:33:00 server id 1 end_log_pos 300 crc32 0x87654321 table_map: `testdb`.`users` mapped to number 123

这里表示testdb数据库中的users表被映射为编号 123。

  • write_rows 事件:记录了插入数据的操作。例如:
#120505 14:33:10 server id 1 end_log_pos 350 crc32 0x23456789 write_rows: table id 123 flags: stmt_end_f
### insert into `testdb`.`users`
### set
### @1=1 /* int meta=0 nullable=0 is_null=0 */,@2='jane' /* varchar(50) meta=50 nullable=0 is_null=0 */,@3=30 /* int meta=0 nullable=0 is_null=0 */

表示向testdb.users表插入了一条数据,数据的具体字段值以@符号表示。

  • update_rows 事件:记录更新数据的操作,会包含更新前和更新后的行数据。例如:
#120505 14:34:00 server id 1 end_log_pos 400 crc32 0x34567890 update_rows: table id 123 flags: stmt_end_f
### update `testdb`.`users`
### where
### @1=1 /* int meta=0 nullable=0 is_null=0 */,@2='jane' /* varchar(50) meta=50 nullable=0 is_null=0 */,@3=30 /* int meta=0 nullable=0 is_null=0 */
### set
### @1=1 /* int meta=0 nullable=0 is_null=0 */,@2='jane doe' /* varchar(50) meta=50 nullable=0 is_null=0 */,@3=31 /* int meta=0 nullable=0 is_null=0 */
  • delete_rows 事件:记录删除数据的操作,包含被删除行的信息。例如:
#120505 14:35:00 server id 1 end_log_pos 450 crc32 0x45678901 delete_rows: table id 123 flags: stmt_end_f
### delete from `testdb`.`users`
### where
### @1=1 /* int meta=0 nullable=0 is_null=0 */,@2='jane doe' /* varchar(50) meta=50 nullable=0 is_null=0 */,@3=31 /* int meta=0 nullable=0 is_null=0 */

通过上述方法和对内容的解析,能够深入了解 mysql 数据库中数据的变化历史,为数据库的维护和故障排查提供有力支持。

到此这篇关于mysql binlog 日志查看方法及查看内容解析的文章就介绍到这了,更多相关mysql 内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com