剖析MySQL Binlog日志的查看途径与内容解析
一、Binlog日志基本介绍
Binlog(二进制日志)会把MySQL数据库执行的所有对数据进行修改的操作记录下来,像INSERT、UPDATE、DELETE这类操作都涵盖在内。它在数据恢复、主从复制以及审计等方面起着至关重要的作用。
二、查看Binlog日志的办法
1. 开启Binlog日志功能
默认情况下,MySQL的Binlog日志功能可能没有开启。若要开启,需修改MySQL的配置文件,一般是my.cnf或者my.ini。在[mysqld]部分添加或修改如下配置:
log-bin=mysql-bin
这里的mysql-bin是日志文件的前缀,重启MySQL服务后,Binlog日志功能就会被开启。
2. 查看当前正在使用的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表示当前日志写入的位置。
3. 查看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
(2)在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校验和。
2. 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语句。
3. 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数据库中数据的变化历程,为数据库的维护和故障排查提供有力的支撑。
文章整理自互联网,只做测试使用。发布者:Lomu,转转请注明出处:https://www.it1024doc.com/12760.html