MySQL如何查詢Binlog 生成時(shí)間
前言
本篇文章介紹如何查詢 Binlog 的生成時(shí)間。云上 RDS 有日志管理,但是自建實(shí)例沒有,該腳本可用于自建實(shí)例閃回定位 Binlog 文件。

腳本介紹
直接上代碼吧~
通過讀取 Binlog FORMAT_DESCRIPTION_EVENT header 時(shí)間戳來實(shí)現(xiàn)讀取 Binlog 生產(chǎn)時(shí)間。
# -*- 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. 使用腳本查詢時(shí)間
腳本上傳到 MySQL 服務(wù)器后,指定 binlog index 文件位置即可:
python check_bintime.py /data/mysql_57/logs/mysql-bin.index


到此這篇關(guān)于MySQL如何查詢Binlog 生成時(shí)間的文章就介紹到這了,更多相關(guān)mysql查詢Binlog 生成時(shí)間內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mysql導(dǎo)入導(dǎo)出時(shí)遇到的問題解決
這篇文章主要給大家介紹了關(guān)于Mysql導(dǎo)入導(dǎo)出時(shí)遇到問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Mysql具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
MySQL字段類型與Java實(shí)體類類型對(duì)應(yīng)轉(zhuǎn)換關(guān)系詳解
這篇文章主要介紹了MySQL字段類型與Java實(shí)體類類型對(duì)應(yīng)轉(zhuǎn)換關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
保證MySQL與Redis數(shù)據(jù)一致性的6種實(shí)現(xiàn)方案
這篇文章將聚焦在一個(gè)非常重要且復(fù)雜的問題上:MySQL與Redis數(shù)據(jù)的一致性,當(dāng)我們?cè)趹?yīng)用中同時(shí)使用MySQL和Redis時(shí),如何保證兩者的數(shù)據(jù)一致性呢?下面就來分享幾種實(shí)用的解決方案,需要的朋友可以參考下2024-03-03
MySQL數(shù)據(jù)庫復(fù)合查詢操作實(shí)戰(zhàn)
mysql表的查詢都是對(duì)一張表進(jìn)行查詢,在實(shí)際開發(fā)中這遠(yuǎn)遠(yuǎn)不夠,下面這篇文章主要給大家介紹了關(guān)于MySQL數(shù)據(jù)庫復(fù)合查詢的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
mysqlreport顯示Com_中change_db占用比例高的問題的解決方法
最近公司的mysql服務(wù)器經(jīng)常出現(xiàn)阻塞狀態(tài)。動(dòng)不動(dòng)就重啟,給用戶訪問帶來了相當(dāng)?shù)牟槐恪?/div> 2009-05-05
MySQL誤刪后使用binlog恢復(fù)數(shù)據(jù)的實(shí)現(xiàn)方法
這篇文章主要介紹了MySQL誤刪后使用binlog恢復(fù)數(shù)據(jù)的實(shí)現(xiàn)方法,使用 binlog 恢復(fù)數(shù)據(jù)的預(yù)期效果是將誤刪的數(shù)據(jù)還原到誤刪之前的狀態(tài),以減少或消除數(shù)據(jù)丟失的影響,文中有相關(guān)的代碼示例和圖文介紹,需要的朋友可以參考下2024-05-05最新評(píng)論

