MySQL如何查询Binlog 生成时间
作者:Bing@DBA 发布时间:2024-01-19 23:34:57
标签:mysql,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]
})
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
来源:https://blog.csdn.net/qq_42768234/article/details/126970988


猜你喜欢
- 在Python 2.7中,一个float的repr返回最接近十七位数的十进制数;这足以精确地识别每个可能的IEEE浮点值.浮点数的str类似
- 图片的上传上传图片使用了表单提交, 下面是html部分, enctype="multipart/form-data"表示
- Python的装饰器可以实现在代码运行期间修改函数的上下文, 即可以定义函数在执行之前进行何种操作和函数执行后进行何种操作, 而函数本身并没
- 连接查询:是将两个查询(或表)的每一行,以“两两横同对接”的方式,所得到的所有行的结果,即一个表中的某行,跟另一个表中的某行。进行“横向对接
- 1、概述在 Go 里有很多种定时器的使用方法,像常规的 Timer、Ticker 对象,以及经常会看到的 time.After(d Dura
- 颜值打分定义可视化图像函数导入三维人脸关键点检测模型导入可视化函数和可视化样式将图像模型输入,获取预测结果BGR转RGB将RGB图像输入模型
- php数组中元素的存在方式是以键值对的方式('key'=>'value'),有时候我们需要根据键删除数
- python2.7中 集成了json的处理(simplejson),但在实际应用中,从mysql查询出来的数据,通常有日期格式,这时候,会报
- AlexNet介绍AlexNet是2012年ISLVRC 2012(ImageNet Large Scale Visual Recognit
- 一、准备工作首先准备两张表用于演示:CREATE TABLE `student_info` ( `id` int NOT NUL
- 对于Python而言代码缩进是一种语法,Python没有像其他语言一样采用{}或者begin...end分隔代码块,而是采用代码缩进和冒号来
- 一、前言return一直中,每中语言中其没没有很大差别,就不多说了。(shell语言return的是退出状态,可能差别是比较大的)最早看到y
- 本文研究的主要是django在接受post请求时显示403forbidden时的处理方法,具体代码如下。最近在做一个项目需要用到Django
- 我遇到过这种情况 就是在我的data中 会有数据调用data中的其他数据如图 我的alertInfoType需要拿到screeningCon
- 我就废话不多说了,直接上代码吧!import numpy as npimport matplotlib.pyplot as pltx = n
- 这篇文章主要介绍了如何通过python实现人脸识别验证,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- 前言随着人工智能的日益火热,计算机视觉领域发展迅速,尤其在人脸识别或物体检测方向更为广泛,今天就为大家带来最基础的人脸识别基础,从一个个函数
- 内置300余汉字点阵.纯ASP实现汉字验证码.不读数据库.多种属性自由调节,其中包括:生成的图片长和宽,字符数,背景显示效果(渐变,杂色,固
- 错误类型: Microsoft JET Database Engine (0x80004005) 不能使用 '';文件已在使
- 效果如图所示:测试sql语句如下:declare @tab table(Class varchar(20),Student varchar(