MySQL获取binlog的开始时间和结束时间(最新方法)
mysql数据库恢复到指定时间点时,我们必须通过MySQL全备+MySQL增量备份(可选)+MySQL的二进制日志(binlog)进行重放来恢复到指定时间点,实际的生产环境中,可能一段时间内生成了多个二进制日志文件(binlog), MySQL本身不会存储二进制日志文件(binlog)的开始时间和结束时间,如果要还原到某个时间点,我们需要知道还原后重放哪些二进制日志文件。那么就必须获取二进制日志(binlog)的开始时间和结束时间。那么我们如何获取MySQL二进制日志文件(binlog)的开始时间和结束时间呢?下面简单总结几个方法,以供参考。
1:通过xtrabackup_info文件获取
Xtrabckup还原全备或增量备份时,会生成一个xtrabackup_info文件,如下所示:
$morextrabackup_info uuid=3bd8a0f7-ea2f-11ed-9896-00505697b437 name= tool_name=xtrabackup tool_command=--defaults-file=/data/conf/my.cnf--login-path=****--backup--target-dir=/db_backup/mysql_backup/db_backup/backup_cycle_2023_04_30/full_backup_2023_05_04_11_53_25 tool_version=8.0.31-24 ibbackup_version=8.0.31-24 server_version=8.0.31 start_time=2023-05-0411:53:26 end_time=2023-05-0411:53:30 lock_time=0 binlog_pos=filename'mysql_binlog.000042',position'289',GTIDofthelastchange'd01ecb4f-c944-11ed-9896-00505697b437:1-111:100 0108-1953894,d01edb91-c944-11ed-9896-00505697b437:1-5,f8ef839e-c942-11ed-9bd2-00505697b437:1-13' innodb_from_lsn=0 innodb_to_lsn=824007891 partial=N incremental=N format=file compressed=N encrypted=N
如上所示,你可以看到一行关于binlog_pos的信息:binlog文件名为mysql_binlog.000042,起始位置为289,我们应用MySQL二进制日志(binlog)可以从这个二进制日志开始:
mysqlbinlog--start-position="289"./mysql_binlog.000042>restore.sql
这个方法有局限性,只能被动获取我们需要重放LIihEmKy二进制日志的文件名和开始位置。这里仅供参考。
2:通过mysqlbinlog解析获取
如下所示,我们想知道mysql_binlog.000042什么时候开始生成,什么时候开始结束的,其实MySQL二进制日志里面包含有这样的信息,如下所示:
$mysqlbinlogmysql_binlog.000042|head-10 #Thepropertermispseudo_replica_mode,butweusethiscompatibilityalias #tomakethestatementusableonserverversions8.0.24andolder. /*!50530SET@@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!50003SET@OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER/*!*/; #at4 #23050411:53:30serverid1end_log_pos126Start:binlogv4,serverv8.0.31created23050411:53:30 BINLOG' OixTZA8BAAAAegAAAH4AAAAAAAQAOC4wLjMxAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAEwANAAgAAAAABAAEAAAAYgAEGggAAAAICAgCAAAACgoKKioAEjQA
我们可以通过获取关键字Start: binlog获取这一行的信息,从而获取MySQL二进制日志(binlog)的开始时间,如下所示:
$mysqlbinlogmysql_binlog.000042|grep"Start:binlog" #23050411:53:30serverid1end_log_pos126Start:binlogv4,serverv8.0.31created23050411:53:30 $mysqlbinlogmysql_binlog.000042|grep"Start:binlog"|awk-F"serverid"'{print$1}' #23050411:53:30
如上所示,生成mysql_binlog.000042的时间点为230504 11:53:30,这里年份使用了缩写模式,即2023缩写为23,230504代表的是2023-05-04.
如果要获取二进制日志(binlog)的结束时间,这个还要看二进制日志是正常循环结束还是实例关闭结束,它们会对应不同的信息
STOP_EVENT
A STOP_EVENT has not payload or post-headerROTATE_EVENTThe rotate event is added to the binlog as last event to tell the reader what binlog to request next.
如下所示:
$mysqlbinlogmysql_binlog.000042|tail-10 #at533 #23050412:13:35serverid3end_log_pos560Xid=50948183 COMMIT/*!*/; #at560 #23050412:14:03serverid1end_log_pos606Rotatetomysql_binloLIihEmKyg.000043pos:4 SET@@SESSION.GTID_NEXT='AUTOMATIC'/*addedbymysqlbinlog*//*!*/; DELIMITER; #phpEndoflogfile /*!50003SETCOMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; /*!50530SET@@SESSION.PSEUDO_SLAVE_MODE=0*/;
$mysqlbinlogmysql_binlog.000042|grepRotate #23050412:14:03serverid1end_log_pos606Rotatetomysql_binlog.000043pos:4 $mysqlbinlogmysql_binlog.000042|grepRotate|awk-F"serverid"'{print$1}' #23050412:14:03开发者_Spark
如果MySQL关闭后,对应的MySQL二进制日志的格式如下所示:
$mysqlbinlogmysql_binlog.000045|tail-10 #at126 #23050417:02:23serverid10end_log_pos157CRC320x2d378ba5Previous-GTIDs #[empty] #at157 #23050417:08:11serverid10end_log_pos180CRC320xb6b08f2cStop SET@@SESSION.GTID_NEXT='AUTOMATICwww.devze.com'/*addedbymysqlbinlog*//*!*/; DELIMITER; #Endoflogfile /*!50003SETCOMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; /*!50530SET@@SESSION.PSEUDO_SLAVE_MODE=0*/;
$mysqlbinlogmysql_binlog.000045|grepStop|awk-F"serverid"'{print$1}'
方法3:stat命令查看创建时间
在linux平台,我们可以通过stat命令查看文件的创建时间,如下所示
$statmysql_binlog.000042 File:mysql_binlog.000042 Size:606blocks:8IOBlock:4096regularfile Device:fd08h/64776dInode:100663444Links:1 Access:(0640/-rw-r-----)Uid:(801/mysql)Gid:(800/mysql) Access:2023-05-0413:36:54.872910222+0800 Modify:2023-05-0412:14:18.712310369+0800 Change:2023-05-0412:14:18.712310369+0800 Birth:2023-05-0411:53:30.640989646+0800
如上所示,我们可以通过Birth字段信息,知道mysql_binlog.000042是2023-05-04 11:53:30创建的。但是我们没法获取二进制日志的结束时间,不过由于MySQL的二进制日志文件是有数字序列的,所以我们可以通过下一个二进制日志文件(binlog)的开始时间来判断上一个二进制日志文件(binlog)的结束时间。
$statmysql_binlog.000043 File:mysql_binlog.000043 Size:335Blocks:8IOBlock:4096regularfile Device:fd08h/64776dInode:100663430Links:1 Access:(0640/-rw-r-----)Uid:(801/mysql)Gid:(800/mysql) Access:2023-05-0413:36:58.524978710+0800 Modify:2023-05-0416:55:07.161545830+0800 Change:2023-05-0416:55:07.161545830+0800 Birth:2023-05-0412:14:18.713310387+0800
到此这篇关于MySQL如何获取binlog的开始时间和结束时间的文章就介绍到这了,更多相关mysql binlog开始时间和结束时间内容请搜索我们以前的文章或继续浏览下面的相关文js章希望大家以后多多支持我们!
精彩评论