MySQL binary log analysis

 The main role of MySQL binary logs, there are three: data recovery, namely the primary synchronization server logs copied from the audit determines whether the database injection attacks. Binary log files can not be opened directly, the need for analysis tools.

1, first of all, start the binary log. Log_bin parameters, dynamic parameters, it is necessary in the configuration file is provided below. It may be directly "log_bin =" parameter setting back the file path. Parameter view the path to datadir.

[root@localhost mysql]# cat /etc/my.cnf
# Remove leading # to turn on a very important data integrity option: logging

# changes to the binary log between backups.
log_bin

...

mysql> show variables like 'datadir';
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| datadir | /var/lib/mysql/ |
+---------------+-----------------+
1 row in set (0.00 sec)

mysql>

 

2, the binary log file list is as follows,

[root@localhost mysql]# ll

-rw-rw---- 1 mysql mysql 215978 2月 28 15:30 localhost-bin.000001
-rw-rw---- 1 mysql mysql 120 2月 28 15:30 localhost-bin.000002

-rw-rw---- 1 mysql mysql 115 3月 2 13:33 localhost-bin.index

 

3, analysis tools must MySQL binary log mysqlbinlog tool that comes through.

Example 1, starting from the most recent log file 000002 of view, there is no record, but records the file header information.
[MySQL the root @ localhost] # the mysqlbinlog --start-position = 0 localhost-bin.000002
Warning: Option 'Start-position': Adjusted unsigned value to 0. 4
/ * 50530 SESSION.PSEUDO_SLAVE_MODE the SET @@. 1 * = /;!
! / 40019 * @@ session.max_insert_delayed_threads the SET = 0 * /;
/ * 50003 = @@ completion_type @OLD_COMPLETION_TYPE the SET, completion_type = 0 * /;!
DELIMITER / * * /;!
# AT. 4
# 200228 15:30:30 the above mentioned id 1 end_log_pos 120 CRC32 Server 0xda43dea1 Start: v binlog 4, Server v 5.6.27-log the Created the Startup AT 15:30:30 200228
# Warning:. the this binlog IS WAS either in use or not Closed Properly
ROLLBACK / * * /! ;
BINLOG '
lsFYXg8BAAAAdAAAAHgAAAABAAQANS42LjI3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAACWwVheEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAaHe
Q9o=
'/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@localhost mysql]#

 

Example 2, the latest log file 120 is Position 000002, and the file length just 120Bytes.
[root @ localhost MySQL] -p-uroot-# MySQL
...
MySQL> Show Master Status;
+ ---------------------- + ----- ----- ------------------ + ---------- + -------------- + + ---------
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+ ---------------------- + ----- ----- ------------------ + ---------- + -------------- + + ---------
| localhost-bin.000002 | 120 | | | |
+ ---------------------- + ---- + ------------------ + ------ + -------------- --------- + ----------
. 1 in Row SET (0.01 sec)

mysql>
...

 

 Example 3, history log file Position 000001 to 215486 to start  information are as follows.

[root@localhost mysql]# mysqlbinlog --start-position=215486 localhost-bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
...
# at 215486
#200228 15:21:48 server id 1 end_log_pos 215585 CRC32 0x20df1851 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1582874508/*!*/;
SET @@session.pseudo_thread_id=1/*!*/;
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 latin1 *//*!*/;
SET @@session.character_set_client=8,@@session.collation_connection=8,@@session.collation_server=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 215585
...
# at 215955
#200228 15:30:27 server id 1 end_log_pos 215978 CRC32 0x8374e4e4 Stop
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@localhost mysql]#

 

例4、将上述历史日志文件000001的Position +1变成215487开始得到的信息如下。
[root@localhost mysql]# mysqlbinlog --start-position=215487 localhost-bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
...
ERROR: Error in Log_event::read_log_event(): 'read error', data_len: 553648128, event_type: 1
ERROR: Could not read entry at offset 215487: Error in log format or read error.
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@localhost mysql]#

Here to see the error message, because the starting position is not a record of 215,487, so the start-position indicates the start position of a physical record in a binary file.

 

Guess you like

Origin www.cnblogs.com/orange-CC/p/12378142.html