MySQL如何查询Binlog 生成时间

 更新时间:2023年03月18日 16:32:10   作者:Bing@DBA  
这篇文章主要介绍了MySQL如何查询Binlog 生成时间,通过读取 Binlog FORMAT_DESCRIPTION_EVENT header 时间戳来实现读取 Binlog 生产时间,本文给大家详细讲解,需要的朋友可以参考下

前言

本篇文章介绍如何查询 Binlog 的生成时间。云上 RDS 有日志管理,但是自建实例没有,该脚本可用于自建实例闪回定位 Binlog 文件。

在这里插入图片描述

脚本介绍

直接上代码吧~

通过读取 Binlog FORMAT_DESCRIPTION_EVENT header 时间戳来实现读取 Binlog 生产时间。

# -*- coding: utf-8 -*-
import os
import sys
import math
import time
import struct
import argparse

binlog_quer_event_stern = 4
binlog_event_fix_part = 13
table_map_event_fix_length = 8
BINLOG_FILE_HEADER = b'\xFE\x62\x69\x6E'
binlog_event_header_len = 19


class BinlogTimestamp(object):
    def __init__(self, index_path):
        self.index_path = index_path

    def main(self):
        binlog_info_list = list()
        for file_path in self.reed_index_file():
            result = self.read_binlog_pos(file_path)
            binlog_info_list.append({
                'file_name': result[0],
                'binlog_size': result[2],
                'start_time': result[1]
            })
        # print
        i = 0

        while len(binlog_info_list) > i:
            if i + 1 == len(binlog_info_list):
                end_time = 'now'
            else:
                end_time = binlog_info_list[i + 1]['start_time']

            binlog_info_list[i]['end_time'] = end_time
            print(binlog_info_list[i])
            i += 1

    def read_binlog_pos(self, binlog_path):
        binlog_file_size = self.bit_conversion(os.path.getsize(binlog_path))
        file_name = os.path.basename(binlog_path)
        with open(binlog_path, 'rb') as r:
            # read BINLOG_FILE_HEADER
            if not r.read(4) == BINLOG_FILE_HEADER:
                print("Error: Is not a standard binlog file format.")
                sys.exit(0)

            # read binlog header FORMAT_DESCRIPTION_EVENT
            read_byte = r.read(binlog_event_header_len)
            result = struct.unpack('=IBIIIH', read_byte)
            type_code, event_length, event_timestamp, next_position = result[1], result[3], result[0], result[4]
            binlog_start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(event_timestamp))

        return file_name, binlog_start_time, binlog_file_size

    def reed_index_file(self):
        """
        读取 mysql-bin.index 文件
        select @@log_bin_index;
        :return:
        """
        with open(self.index_path) as r:
            content = r.readlines()

        return [x.replace('\n', '') for x in content]

    @staticmethod
    def bit_conversion(size, dot=2):
        size = float(size)
        if 0 <= size < 1:
            human_size = str(round(size / 0.125, dot)) + ' b'
        elif 1 <= size < 1024:
            human_size = str(round(size, dot)) + ' B'
        elif math.pow(1024, 1) <= size < math.pow(1024, 2):
            human_size = str(round(size / math.pow(1024, 1), dot)) + ' KB'
        elif math.pow(1024, 2) <= size < math.pow(1024, 3):
            human_size = str(round(size / math.pow(1024, 2), dot)) + ' MB'
        elif math.pow(1024, 3) <= size < math.pow(1024, 4):
            human_size = str(round(size / math.pow(1024, 3), dot)) + ' GB'
        elif math.pow(1024, 4) <= size < math.pow(1024, 5):
            human_size = str(round(size / math.pow(1024, 4), dot)) + ' TB'
        elif math.pow(1024, 5) <= size < math.pow(1024, 6):
            human_size = str(round(size / math.pow(1024, 5), dot)) + ' PB'
        elif math.pow(1024, 6) <= size < math.pow(1024, 7):
            human_size = str(round(size / math.pow(1024, 6), dot)) + ' EB'
        elif math.pow(1024, 7) <= size < math.pow(1024, 8):
            human_size = str(round(size / math.pow(1024, 7), dot)) + ' ZB'
        elif math.pow(1024, 8) <= size < math.pow(1024, 9):
            human_size = str(round(size / math.pow(1024, 8), dot)) + ' YB'
        elif math.pow(1024, 9) <= size < math.pow(1024, 10):
            human_size = str(round(size / math.pow(1024, 9), dot)) + ' BB'
        elif math.pow(1024, 10) <= size < math.pow(1024, 11):
            human_size = str(round(size / math.pow(1024, 10), dot)) + ' NB'
        elif math.pow(1024, 11) <= size < math.pow(1024, 12):
            human_size = str(round(size / math.pow(1024, 11), dot)) + ' DB'
        elif math.pow(1024, 12) <= size:
            human_size = str(round(size / math.pow(1024, 12), dot)) + ' CB'
        else:
            raise ValueError('bit_conversion Error')
        return human_size


if __name__ == '__main__':
    file_name = sys.argv[1]

    bt = BinlogTimestamp(file_name)
    bt.main()


使用案例

1. 查询 binlog index 文件

在这里插入图片描述

2. 使用脚本查询时间

脚本上传到 MySQL 服务器后,指定 binlog index 文件位置即可:

python check_bintime.py /data/mysql_57/logs/mysql-bin.index

在这里插入图片描述

到此这篇关于MySQL如何查询Binlog 生成时间的文章就介绍到这了,更多相关mysql查询Binlog 生成时间内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Mysql导入导出时遇到的问题解决

    Mysql导入导出时遇到的问题解决

    这篇文章主要给大家介绍了关于Mysql导入导出时遇到问题的解决方法,文中通过示例代码介绍的非常详细,对大家学习或者使用Mysql具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-08-08
  • MySQL如何使用union all获得并集排序

    MySQL如何使用union all获得并集排序

    这篇文章主要介绍了MySQL如何使用union all获得并集排序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • MySQL字段类型与Java实体类类型对应转换关系详解

    MySQL字段类型与Java实体类类型对应转换关系详解

    这篇文章主要介绍了MySQL字段类型与Java实体类类型对应转换关系,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-06-06
  • 保证MySQL与Redis数据一致性的6种实现方案

    保证MySQL与Redis数据一致性的6种实现方案

    这篇文章将聚焦在一个非常重要且复杂的问题上:MySQL与Redis数据的一致性,当我们在应用中同时使用MySQL和Redis时,如何保证两者的数据一致性呢?下面就来分享几种实用的解决方案,需要的朋友可以参考下
    2024-03-03
  • MySql闪退和服务无法启动的解决方法

    MySql闪退和服务无法启动的解决方法

    今天小编就为大家分享一篇关于MySql闪退和服务无法启动的解决方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-02-02
  • SQL中字符串截取函数图文教程

    SQL中字符串截取函数图文教程

    在SQL的实际用途中,经常会碰到需要对查询结果值需要做字段的一些截取,下面这篇文章主要给大家介绍了关于SQL中字符串截取函数的相关资料,需要的朋友可以参考下
    2023-01-01
  • MySQL中describe命令的使用方法小结

    MySQL中describe命令的使用方法小结

    这篇文章主要介绍了MySQL中describe命令的使用方法小结,describe命令主要用于获取表或列等的各种信息,需要的朋友可以参考下
    2015-12-12
  • MySQL数据库复合查询操作实战

    MySQL数据库复合查询操作实战

    mysql表的查询都是对一张表进行查询,在实际开发中这远远不够,下面这篇文章主要给大家介绍了关于MySQL数据库复合查询的相关资料,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2023-05-05
  • mysqlreport显示Com_中change_db占用比例高的问题的解决方法

    mysqlreport显示Com_中change_db占用比例高的问题的解决方法

    最近公司的mysql服务器经常出现阻塞状态。动不动就重启,给用户访问带来了相当的不便。
    2009-05-05
  • MySQL误删后使用binlog恢复数据的实现方法

    MySQL误删后使用binlog恢复数据的实现方法

    这篇文章主要介绍了MySQL误删后使用binlog恢复数据的实现方法,使用 binlog 恢复数据的预期效果是将误删的数据还原到误删之前的状态,以减少或消除数据丢失的影响,文中有相关的代码示例和图文介绍,需要的朋友可以参考下
    2024-05-05

最新评论