网络编程
位置:首页>> 网络编程>> 数据库>> MySQL如何查询Binlog 生成时间

MySQL如何查询Binlog 生成时间

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

标签:mysql,Binlog,生成时间

前言

本篇文章介绍如何查询 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

0
投稿

猜你喜欢

  • 严格控制Session可以将不需要Session的内容(比如帮助画面,访问者区域,等等)移动到关闭Session的独立ASP应用程序中。在基
  • 今天因为程序里面要把写入数据库的html源代码以html源编码的形式显示在页面里面,而不要被浏览器解释成。。找了N久都不知道怎么做后来看了一
  • 井字棋,英文名叫Tic-Tac-Toe,是一种在3*3格子上进行的连珠游戏,和五子棋类似,由于棋盘一般不画边框,格线排成井字故得名。游戏需要
  • 将list拼接为一个字符串在 python 中如果想将 list 拼接为一个字符串,可使用 join() 方法。join() 方法描述将序列
  • 本文实例讲述了python中while循环语句用法。分享给大家供大家参考。具体如下:number = 1while number <
  • 在采集网页信息的时候,经常需要伪造报头来实现采集脚本的有效执行下面,我们将使用urllib2的header部分伪造报头来实现采集信息方法1、
  • 一天不小心把ROOT的权限改到最小了(只能登录,什么都做不了),这可急死我了.重装的话太麻烦,而且里面有很多的用户,一个个重新弄不知道到什么
  • 定义切片区别于数组,是引用类型, 不是值类型。数组是固定长度的,而切片长度是可变的,我的理解是:切片是对数组一个片段的引用。var s1 [
  • 本文实例讲述了Flask框架Flask-Login用法。分享给大家供大家参考,具体如下:Flask-Login插件中带了6种信号,可以基于其
  • 在使用ValuesQuerySet存放查询结果时,有时需要转为json,但并不能直接使用json.dumps()直接转,而是需要经过下面一个
  • 实现了宽度、高度、透明度的渐变,还能以高度宽度中点为中心,还扩展成以任意点为中心渐变(实例中以点击点为中心)。<!DOCTYPE ht
  • 我就废话不多说了,直接上代码吧!#coding=utf-8'''openCV中最核心的的类是Mat,他是matrix
  • 本文实例讲述了Python实现的各种常见分布算法。分享给大家供大家参考,具体如下:#-*- encoding:utf-8 -*-import
  • 1.python实现对doc文档的读取#读取docx中的文本代码示例import docx#获取文档对象file=docx.Document
  • 程序需求:输入用户名,密码认证成功显示欢迎信息输入错误三次后锁定用户流程图:好像画的不咋地查看代码:#!/usr/bin/env pytho
  • 感谢Mr.Cool 给asp之家投递精彩的文章!站长你好,我经常光临你的站点,因为我也特别喜欢ASP写网页,你的站点上提供的代码比较适用哈,
  • 什么是POP3、SMTP和MAP?POP3是Post Office Protocol 3的简称,即邮局协议的第三个版本,他是规定怎样将个人计
  • 1. Python 的参数传递Python的参数传递,无法控制引用传递还是值传递。对于不可变对象(数字、字符、元组等)的参数,更类似值传递;
  • python协程只能运行在事件循环中,但是一旦事件循环运行,又会阻塞当前任务。所以只能在当前进程中再开一个线程,这个线程的主要任务是运行事件
  • 学习编写简练、优化的CSS需要大量的实践和一种不自觉的强迫性清洁的渴望。然而让你的CSS保持整洁并不仅仅是你对清洁的疯狂的心理需求,尤其对于
手机版 网络编程 asp之家 www.aspxhome.com