MySQL如何查询Binlog 生成时间

作者:Bing@DBA 时间:2024-01-19 23:34:57 

前言

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

MySQL如何查询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 文件

MySQL如何查询Binlog 生成时间

2. 使用脚本查询时间

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

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

MySQL如何查询Binlog 生成时间

MySQL如何查询Binlog 生成时间

来源:https://blog.csdn.net/qq_42768234/article/details/126970988

标签:mysql,Binlog,生成时间
0
投稿

猜你喜欢

  • 使用 django orm 写 exists 条件过滤实例

    2022-01-20 02:42:48
  • 个人微信公众号接入ChatGPT的教程分享

    2023-04-08 00:34:02
  • 利用Python实现自动生成数据日报

    2022-08-26 11:12:48
  • Python pandas的八个生命周期总结

    2023-02-08 17:01:04
  • 搭建pypi私有仓库实现过程详解

    2023-02-02 09:14:35
  • Javascript——浅析注册事件

    2008-08-19 12:44:00
  • IE7兼容模式与兼容视图

    2010-06-28 18:48:00
  • 在cmd中运行.py文件: python的操作步骤

    2021-10-01 08:40:47
  • Ubuntu下MySQL安装及配置远程登录教程

    2024-01-23 23:55:33
  • Python 含参构造函数实例详解

    2023-02-06 13:16:48
  • Pytorch损失函数torch.nn.NLLLoss()的使用

    2021-02-07 16:08:57
  • 使用Python写一个小游戏

    2023-07-20 17:31:07
  • ORACLE 10g 安装教程[图文]

    2009-05-24 19:12:00
  • 5行Python代码实现电脑永不息屏

    2023-04-04 01:26:37
  • ORACLE 正则解决初使化数据格式不一致

    2009-05-24 19:44:00
  • 使用python批量修改文件名的方法(视频合并时)

    2022-08-24 13:19:07
  • Centos中安装多个mysql数据的配置实例

    2024-01-21 08:36:02
  • Python中try excpet BaseException(异常处理捕获)的使用

    2023-07-30 16:21:37
  • python Opencv计算图像相似度过程解析

    2022-02-02 03:56:52
  • Python游戏开发实例之graphics实现AI五子棋

    2022-12-15 10:22:27
  • asp之家 网络编程 m.aspxhome.com