binlog日志解析

来源:互联网 发布:索尼z3v电信4g网络 编辑:程序博客网 时间:2024/05/22 13:45

对于细节不明白,可能参考:

http://blog.itpub.net/22664653/viewspace-1158547/

注:binlog日志信息

mysql> show variables like 'binlog_format';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | MIXED |
+---------------+-------+

1.登录数据库,然后解析binlog日志,方法如下,解析后把日志信息定向到某一个文件中:

适用场景:只需要知道是否执行过对某一个表的操作,或对某一个表的数据操作的次数,也可以结合mysqlbinlog工具得到某一条sql执行的具体时间

mysql -u root -p -e "show binlog events in 'mysql-bin.001853'" > binlog1853.txt

解析后的日志信息如下,能看到具体的sql语句信息、起始位置和结束位置,但是看不到时间信息,这一点比较不好。

mysql-bin.001853        205656898       Query   20    use `xxxx`; INSERT INTO ttttt_daily (deviceId,dayCurrent,dev
iceType,durationWorking,durationLinerWork,durationLinerMaintain,durationAnodeWork,durationAnodeMaintain,durationValleyUse,totalSave,greatFlow,flowOf
Day,settingTemp,doesOnAntiBact,doesOnSmartPower,doesOnMidWarm) values('0007A895C610','20161025','06001001',0.0,0.0,3.392,0.0,4.57,0,0.0,'[]',0,55,0,
0,0)


但是根据得到的日志名称和位置信息,可以使用mysqlbinlog工具根据起始和结束位置来进行分析,语法台下:

#mysqlbinlog --no-defaults --start-position=205656898 --stop-position=205657339  mysql-bin.001853

如下所示,使用mysqlbinlog工具解析出来的信息比较全面,包括如下信息:

(1) 具体的执行时间

(2) server-id

(3) mysql版本信息

(4) 起始和结束位置信息

(5) 数据库名称、

(6)执行的线程号,执行时间(精确到时,分,秒),错误码,具体的sql语句。

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#161026  1:08:21 server id 20  end_log_pos 120 CRC32 0xc27520ff         Start: binlog v 4, server v 5.6.19-log created 161026  1:08:21
BINLOG '
hZEPWA8UAAAAdAAAAHgAAAAAAAQANS42LjE5LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAf8g
dcI=
'/*!*/;
# at 205656898
#161026  2:20:32 server id 20  end_log_pos 205657339 CRC32 0xf46efa00   Query   thread_id=61565736      exec_time=0     error_code=0
use `bigdata`/*!*/;
SET TIMESTAMP=1477419632/*!*/;
SET @@session.pseudo_thread_id=61565736/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1075838976/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
INSERT INTO ttttt_daily (deviceId,dayCurrent,deviceType,durationWorking,durationLinerWork,durationLinerMaintain,durationAnodeWork,durationAnodeMaintain,durationValleyUse,totalSave,greatFlow,flowOfDay,settingTemp,doesOnAntiBact,doesOnSmartPower,doesOnMidWarm) values('0007A895C610','20161025','06001001',0.0,0.0,3.392,0.0,4.57,0,0.0,'[]',0,55,0,0,0)
/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;


2.根据时间信息使用mysqlbinlog解析binlog日志实例如下:

mysqlbinlog --no-defaults --start-datetime='2016-10-26 19:43:00' --stop-datetime='2016-10-26 20:43:00'  mysql-bin.001856

如下所示,使用mysqlbinlog工具解析出来的信息比较全面,包括如下信息:

(1) 具体的执行时间

(2) server-id

(3) mysql版本信息

(4) 起始和结束位置信息

(5) 数据库名称、

(6)执行的线程号,执行时间(精确到时,分,秒),错误码,具体的sql语句。

(7)SET INSERT_ID=25312142  ,日志中此处的作用是,产生新的ID,是表中的自增主键决定的。

/*!*/;
# at 1631176
# at 1631208
#161026 19:56:16 server id 20  end_log_pos 1631208 CRC32 0x35286e4a     Intvar
SET INSERT_ID=25312142/*!*/;
#161026 19:56:16 server id 20  end_log_pos 1631458 CRC32 0xddcf8249     Query   thread_id=62752866      exec_time=0     error_code=0
SET TIMESTAMP=1477482976/*!*/;
insert into ac_bd_task_time_consuming(taskid,starttime,endtime) VALUES( 'collectAcBdUserOnlineLog-user', '2016-10-26 19:56:15.006', '2016-10-26 19:5
6:15.063')
/*!*/;
# at 1631458
#161026 19:56:16 server id 20  end_log_pos 1631489 CRC32 0x8560dd40     Xid = 2722390944
COMMIT/*!*/;
# at 1631489
#161026 19:56:12 server id 20  end_log_pos 1631574 CRC32 0xea1b6162     Query   thread_id=62780240      exec_time=5     error_code=0
SET TIMESTAMP=1477482972/*!*/;
BEGIN
/*!*/;
# at 1631574
#161026 19:56:12 server id 20  end_log_pos 1631768 CRC32 0x416300a2     Query   thread_id=62780240      exec_time=5     error_code=0
use `bigdata`/*!*/;
SET TIMESTAMP=1477482972/*!*/;
delete FROM xxxxx  WHERE device_type='18002001' AND start_time='2016-10-26 18:00:00' AND stat_name='all_sum'
/*!*/;


3.的其它使用方法:

mysqlbinlog --no-defaults -v mysql-bin.001856 -d bigdata > wxc1.txt 

此处-d的作用是只过滤出与bigdata数据库相关的sql语句,解析后的日志就不再解释,和上面查不多。

#161027  3:00:05 server id 20  end_log_pos 137742894 CRC32 0x45ef66da   Query   thread_id=63509417      exec_time=0     error_code=0
SET TIMESTAMP=1477508405/*!*/;
/*!\C utf8mb4 *//*!*/;
SET @@session.character_set_client=45,@@session.collation_connection=45,@@session.collation_server=33/*!*/;
BEGIN
/*!*/;
# at 137742894
#161027  3:00:05 server id 20  end_log_pos 137743002 CRC32 0x31f3323e   Table_map: `bigdata`.`xxxx` mapped to number 276
# at 137743002
#161027  3:00:05 server id 20  end_log_pos 137743147 CRC32 0xebbd14bc   Delete_rows: table id 276 flags: STMT_END_F
注释:ROWS_EVENT分为三种:WRITE_ROWS_EVENT,UPDATE_ROWS_EVENT,DELETE_ROWS_EVENT,分别对应insert,update和delete操作。
BINLOG '
Nf0QWBMUAAAAbAAAAJrKNQgAABQBAAAAAAEAB2JpZ2RhdGEAGHdhdGVyaGVhdGVySUlfZGFpbHlf
c3RhdAATCA8PDw8PBQUFBQUDBQEBDwMDARJgAPAAMAAwAB4ACAgICAgI8AD4/wc+MvMx
Nf0QWCAUAAAAkQAAACvLNQgAABQBAAAAAAEAAgAT////GAD4QzYLAAAAAAAMMDAwN0E4OTVDNzE1
CDA2MDAxMDAxCDIwMTYxMDI2AAAAAOD/70A/V1uxv+wdQAAAAAAAAAAAP1dbsb/sHUAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAltdAAAAAEYAAAAAvBS96w==
'/*!*/;
### DELETE FROM `bigdata`.`xxxx`
### WHERE
###   @1=734787
###   @2='0007A895C715'
###   @3='06001001'
###   @4=NULL
###   @5=NULL
###   @6='20161026'
###   @7=65535
###   @8=7.4812000000000002942
###   @9=0
###   @10=7.4812000000000002942
###   @11=0
###   @12=0
###   @13=0
###   @14=0
###   @15=0
###   @16='[]'
###   @17=0
###   @18=70
###   @19=0
# at 137743147
#161027  3:00:05 server id 20  end_log_pos 137743178 CRC32 0xaba9656f   Xid = 2732617931

注释:在事务提交时,不管是STATEMENT还是ROW格式的binlog,都会在末尾添加一个XID_EVENT事件代表事务的结束。该事件记录了该事务的ID,在MySQL进行崩溃恢复时,根据事务在binlog中的提交情况来决定是否提交存储引擎中状态为prepared的事务。
COMMIT/*!*/;

0 0