广告
返回顶部
首页 > 资讯 > 数据库 >MySQL如何查询Binlog生成时间
  • 273
分享到

MySQL如何查询Binlog生成时间

mysql查询Binlog生成时间mysql查询Binlog 2023-03-19 17:03:58 273人浏览 安东尼
摘要

目录前言脚本介绍使用案例1. 查询 binlog index 文件2. 使用脚本查询时间前言 本篇文章介绍如何查询 Binlog 的生成时间。云上 RDS 有日志管理,但是自建实例没

前言

本篇文章介绍如何查询 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 生成时间内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

您可能感兴趣的文档:

--结束END--

本文标题: MySQL如何查询Binlog生成时间

本文链接: https://www.lsjlt.com/news/200407.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • MySQL如何查询Binlog 生成时间
    目录前言脚本介绍使用案例1. 查询 binlog index 文件2. 使用脚本查询时间前言 本篇文章介绍如何查询 Binlog 的生成时间。云上 RDS 有日志管理,但是自建实例没有,该脚本可用于自建实例闪回定位 Bi...
    99+
    2023-03-19
    mysql查询Binlog
  • MySQL如何查询Binlog生成时间
    目录前言脚本介绍使用案例1. 查询 binlog index 文件2. 使用脚本查询时间前言 本篇文章介绍如何查询 Binlog 的生成时间。云上 RDS 有日志管理,但是自建实例没...
    99+
    2023-03-19
    mysql查询Binlog 生成时间 mysql查询Binlog
  • MySQL怎么查询Binlog生成时间
    这篇文章主要介绍“MySQL怎么查询Binlog生成时间”,在日常操作中,相信很多人在MySQL怎么查询Binlog生成时间问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”MySQL怎么查询Binlog生成时间...
    99+
    2023-07-05
  • PHP如何生成时间戳?php时间戳怎么查询
    PHP 时间戳查询是一种在 PHP 编程语言中使用的日期和时间处理方式。 时间戳是指自 1970 年 1 月 1 日之后所经过的秒数,通常用于表示某个事件发生的时间或日期。通过使用 PHP 时间戳查询,您可以方便地获取特定的日期和时间,对于...
    99+
    2023-05-14
    php
  • mysql如何查询时间区间
    mysql如何查询时间区间?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。mysql如何查询时间区间1.查询昨天的数据 s...
    99+
    2022-10-18
  • 如何统计MySQL查询时间
    本文主要给大家简单讲讲如何统计MySQL查询时间,相关专业术语大家可以上网查查或者找一些相关书籍补充一下,这里就不涉猎了,我们就直奔主题吧,希望如何统计MySQL查询时间这篇文章可以给大家带来一些实际帮助。...
    99+
    2022-10-18
  • mysql如何查询日期与时间
    前言: 在项目开发中,一些业务表字段经常使用日期和时间类型,而且后续还会牵涉到这类字段的查询。关于日期及时间的查询等各类需求也很多,本篇文章简单讲讲日期及时间字段的规范化查询方法。 1.日期和时间类型概览 MySQ...
    99+
    2022-05-21
    mysql 查询日期 mysql 查询时间
  • Mysql 如何查询时间段交集
    Mysql 查询时间段交集 使用场景 数据库表有两个字段starttime,endtime。现在给出(a,b)的时间段,查出和(starttime,endtime)时间段有交集的数据...
    99+
    2022-11-12
  • Mysql中如何使用时间查询
    这篇文章主要介绍了Mysql中如何使用时间查询的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Mysql中如何使用时间查询文章都会有所收获,下面我们一起来看看吧。一、使用等号查询...
    99+
    2023-03-20
    mysql
  • mysql如何设置时间查询条件
    这篇“mysql如何设置时间查询条件”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“mysq...
    99+
    2022-10-19
  • php时间戳如何查询
    本篇内容主要讲解“php时间戳如何查询”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“php时间戳如何查询”吧!获取当前时间戳在PHP中获取当前时间戳非常简单,只需使用time()函数即可:$ti...
    99+
    2023-07-05
  • mysql中如何查询一段时间的日期
    这篇文章给大家分享的是有关mysql中如何查询一段时间的日期的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。 mysql中查询一段时间的日期,要把没数据...
    99+
    2022-10-18
  • 如何读取mysql binlog开始和结束时间
    本篇文章给大家分享的是有关如何读取mysql binlog开始和结束时间,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。mysql binlog...
    99+
    2022-10-18
  • 如何在mysql中查询日期及时间字段
    如何在mysql中查询日期及时间字段,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。在项目开发中,一些业务表字段经常使用日期和时...
    99+
    2022-10-18
  • mongodb如何查询时间条件
    在 MongoDB 中,您可以使用 `$gt`、`$lt`、`$gte`、`$lte` 操作符来查询时间条件。这些操作符分别表示大于...
    99+
    2023-08-31
    mongodb
  • php如何查询时间范围
    小编今天带大家了解php如何查询时间范围,文中知识点介绍的非常详细。觉得有帮助的朋友可以跟着小编一起浏览文章的内容,希望能够帮助更多想解决这个问题的朋友找到问题的答案,下面跟着小编一起深入学习“php如何查询时间范围”的知识吧。php查询时...
    99+
    2023-06-26
  • php如何查询当前时间
    这篇文章主要介绍“php如何查询当前时间”,在日常操作中,相信很多人在php如何查询当前时间问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”php如何查询当前时间”的疑惑有所帮助!接下来,请跟着小编一起来学习吧...
    99+
    2023-06-30
  • mysql如何查询临时表
    这篇文章主要讲解了“mysql如何查询临时表”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“mysql如何查询临时表”吧! 本教程...
    99+
    2022-10-19
  • MySQL中如何查询当前时间间隔前1天的数据
    这篇文章主要介绍“MySQL中如何查询当前时间间隔前1天的数据”,在日常操作中,相信很多人在MySQL中如何查询当前时间间隔前1天的数据问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”MySQL中如何查询当前时...
    99+
    2023-06-21
  • 如何查询linux启动的时间
    可以使用以下命令查询Linux启动的时间:1. 使用`uptime`命令:直接在终端输入`uptime`命令,它会显示系统启动时间以...
    99+
    2023-09-13
    linux
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作