mysqbinlog解析二進制日誌測試隨筆

在此之前需要了解二進制日誌的格式:

    row  --行

    statement  --語句

    Mixed  --混合

1、當前的binlog_format = statement

mysql> show variables like 'binlog_format';

+---------------+-----------+

| binlog_format | STATEMENT |

+---------------+-----------+

對庫表進行插入數據操作:

insert into tb2 values (3,'son')

查看當前格式下的二進制日誌內容:

# mysqlbinlog /mydata/master.000001

[root@localhost mydata]# mysqlbinlog --start-position=851 --stop-position=957

[root@localhost mydata]# mysqlbinlog --base64-output=decode-rows  --start-position=851 --stop-position=957 /mydata/master.000001

/*!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 851

#161118  0:59:38 server id 1  end_log_pos 957 CRC32 0xd89c9cd3 Query thread_id=2 exec_time=0 error_code=0

use `test`/*!*/;

SET TIMESTAMP=1479401978/*!*/;

SET @@session.pseudo_thread_id=2/*!*/;

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=8/*!*/;

SET @@session.lc_time_names=0/*!*/;

SET @@session.collation_database=DEFAULT/*!*/;

insert into tb2 values (3,'son')

/*!*/;

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 mydata]#

2、binlog_format=row

mysql> show variables like 'binlog_format';

+---------------+-------+

| binlog_format | ROW   |

+---------------+-------+

同樣對錶進行插入和刪除的操作

mysql> insert into tb2 values(2,'helen');

mysql> delete from tb2 where id = 1 ;

查看二進制日誌:   直觀的反饋就是你壓根就看不到你剛纔所執行過的語句,只有些許關鍵字如insert 、delete表達了你動作的性質 。

[root@localhost mydata]# mysqlbinlog --base64-output=decode-row --start-position=120  master.000001

/*!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 120

#161118  1:14:58 server id 1  end_log_pos 192 CRC32 0x8882b974 Query thread_id=1 exec_time=0 error_code=0

SET TIMESTAMP=1479402898/*!*/;

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 utf8 *//*!*/;

SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;

SET @@session.lc_time_names=0/*!*/;

SET @@session.collation_database=DEFAULT/*!*/;

BEGIN

/*!*/;

# at 192

#161118  1:14:58 server id 1  end_log_pos 241 CRC32 0xa120e7f3 Table_map: `test`.`tb2` mapped to number 71

# at 241

#161118  1:14:58 server id 1  end_log_pos 287 CRC32 0x6da9f0d9 Write_rows: table id 71 flags: STMT_END_F

# at 287

#161118  1:14:58 server id 1  end_log_pos 318 CRC32 0x952ced1d Xid = 16

COMMIT/*!*/;

# at 318

#161118  1:15:06 server id 1  end_log_pos 390 CRC32 0x37f19e7d Query thread_id=1 exec_time=0 error_code=0

SET TIMESTAMP=1479402906/*!*/;

BEGIN

/*!*/;

# at 390

#161118  1:15:06 server id 1  end_log_pos 439 CRC32 0x8f0bf21e Table_map: `test`.`tb2` mapped to number 71

# at 439

#161118  1:15:06 server id 1  end_log_pos 483 CRC32 0x9fe2e907 Write_rows: table id 71 flags: STMT_END_F

# at 483

#161118  1:15:06 server id 1  end_log_pos 514 CRC32 0xa7ed825e Xid = 17

COMMIT/*!*/;

# at 514

#161118  1:17:13 server id 1  end_log_pos 586 CRC32 0x3e2b7b53 Query thread_id=1 exec_time=0 error_code=0

SET TIMESTAMP=1479403033/*!*/;

BEGIN

/*!*/;

# at 586

#161118  1:17:13 server id 1  end_log_pos 635 CRC32 0xc5b403fd Table_map: `test`.`tb2` mapped to number 71

# at 635

#161118  1:17:13 server id 1  end_log_pos 679 CRC32 0xdc5e885f Delete_rows: table id 71 flags: STMT_END_F

# at 679

#161118  1:17:13 server id 1  end_log_pos 710 CRC32 0x2df182e5 Xid = 18

COMMIT/*!*/;

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 mydata]#

3、binlog_format = MIXED

mysql> show variables like 'binlog_format';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| binlog_format | MIXED |

+---------------+-------+

對test庫中的表tb2做了如下動作:

mysql> insert into tb2 values(2,'helen');

mysql> delete from tb2 where id = 1 ;

查看當前的二進制日誌:

[root@localhost mydata]# mysqlbinlog --base64-output=decode-rows  --start-position=120 /mydata/master.000001

/*!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 120

#161118  1:21:27 server id 1  end_log_pos 199 CRC32 0x6e07aaac Query thread_id=1 exec_time=0 error_code=0

SET TIMESTAMP=1479403287/*!*/;

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 utf8 *//*!*/;

SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;

SET @@session.lc_time_names=0/*!*/;

SET @@session.collation_database=DEFAULT/*!*/;

BEGIN

/*!*/;

# at 199

#161118  1:21:27 server id 1  end_log_pos 306 CRC32 0xbc338f2d Query thread_id=1 exec_time=0 error_code=0

use `test`/*!*/;

SET TIMESTAMP=1479403287/*!*/;

insert into tb2 values(2,'helen')

/*!*/;

# at 306

#161118  1:21:27 server id 1  end_log_pos 337 CRC32 0xe0c099a6 Xid = 12

COMMIT/*!*/;

# at 337

#161118  1:21:58 server id 1  end_log_pos 416 CRC32 0x21caca8f Query thread_id=1 exec_time=0 error_code=0

SET TIMESTAMP=1479403318/*!*/;

BEGIN

/*!*/;

# at 416

#161118  1:21:58 server id 1  end_log_pos 518 CRC32 0x08994991 Query thread_id=1 exec_time=0 error_code=0

SET TIMESTAMP=1479403318/*!*/;

delete from tb2 where id = 1

/*!*/;

# at 518

#161118  1:21:58 server id 1  end_log_pos 549 CRC32 0xb0068443 Xid = 13

COMMIT/*!*/;

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 mydata]#

總結:

binlog_format = MIXED     ,默認爲STATEMENT

expire_logs_days = 7 ,默認爲0

max_binlog_size = 100M  ,默認是1G

binlog_cache_size = 32M   ,默認值


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章