对MySQL binlog日志解析,统计每张表的DML次数
想要获取每天数据库每张表的DML的次数,统计热度表,可以使用该脚本
# coding:utf-8
# 解析binlog,统计热度表,表的DML个数 import sys
import os # mysqlbinlog解析binlog日志
def binlog_output():
binlog_file = sys.argv[1]
file_num = binlog_file.split('.')[1] binlog_log = 'binlog_%s.log' % file_num
os.system('/usr/local/mysql5637/mysql/bin/mysqlbinlog -v --base64-output=decode-rows %s > %s' %(binlog_file, binlog_log)) return binlog_log # 对
def binlog_parse(binlog_log): delete_count = 0
update_count = 0
insert_count = 0
update_li = []
insert_li = []
delete_li = []
stop_time_li = [] binlog_f = open(binlog_log, 'r',encoding='utf-8')
for line in binlog_f.readlines():
if line.startswith('### INSERT INTO'):
insert_count += 1
insert_li.append(line[16:].replace('`', '').strip())
if line.startswith('### UPDATE'):
update_count += 1
update_li.append(line[11:].replace('`', '').strip())
if line.startswith('### DELETE FROM'):
delete_count += 1
delete_li.append(line[16:].replace('`','').strip())
if 'Start: binlog' in line:
start_time = line.split('server')[0].replace('#', '')
if 'end_log_pos' in line:
stop_time_li.append(line)
binlog_f.close()
stop_time = stop_time_li[-1].split('server')[0].replace('#', '') start_time = '20' + start_time[:2] + '-' + start_time[2:4] + '-' + start_time[4:]
stop_time = '20' + stop_time[:2] + '-' + stop_time[2:4] + '-' + stop_time[4:] return delete_count,update_count,insert_count,update_li,insert_li,delete_li,start_time,stop_time # 对库和表进统计排序
def DbTableCount(arr):
result = {}
for i in set(arr):
result[i] = arr.count(i)
sort_li = sorted(result.items(), key=lambda x: x[1], reverse=True) return sort_li if __name__ == '__main__': binlog_log = binlog_output()
delete_count, update_count, insert_count, update_li, insert_li, delete_li, start_time,stop_time= binlog_parse(binlog_log)
sort_update_li = DbTableCount(delete_li)
sort_inser_li = DbTableCount(insert_li)
sort_delete_li = DbTableCount(update_li) # 删除解析日志
os.system('rm -f %s' % binlog_log) # 收集时间
print('开始时间: ;', start_time)
print('结束时间: ;', stop_time) # 统计DML总次数
print('\nDML总次数: ', insert_count+update_count+delete_count) # 统计DML次数
print(' Insert总次数: ', insert_count)
print(' Delete总次数: ', delete_count)
print(' Update总次数: ', update_count) # Insert统计
print('\nInsert 统计: ')
for i in sort_inser_li:
print(' DB.Table:', i[0], ', Count:', i[1]) # Delete统计
print('\nDelete 统计: ')
for i in sort_delete_li:
print(' DB.Table:', i[0], ', Count:', i[1]) # Update统计
print('\nUpdate 统计: ')
for i in sort_update_li:
print(' DB.Table:', i[0], ', Count:', i[1])
对MySQL binlog日志解析,统计每张表的DML次数的更多相关文章
- 阿里云mysql数据库恢复总结,mysql binlog日志解析
由于意外..阿里云mysql中有一张表被全部删除了,深吸三口气候,开始解决. 首先用凌晨的自动备份的,进行全量恢复,然后找binlog日志(见下文),查找从全量备份到数据删除之间的记录 这导致了一个问 ...
- Mysql binlog日志解析
1. 摘要: Mysql日志抽取与解析正如名字所将的那样,分抽取和解析两个部分.这里Mysql日志主要是指binlog日志.二进制日志由配置文件的log-bin选项负责启用,Mysql服务器将在数据根 ...
- 转载:阿里canal实现mysql binlog日志解析同步redis
from: http://www.cnblogs.com/duanxz/p/5062833.html 背景 早期,阿里巴巴B2B公司因为存在杭州和美国双机房部署,存在跨机房同步的业务需求.不过早期的数 ...
- Mysql Binlog日志详解
一.Mysql Binlog格式介绍 Mysql binlog日志有三种格式,分别为Statement,MiXED,以及ROW! 1.Statement:每一条会修改数据的sql都会记录在 ...
- MySQL binlog格式解析
MySQL binlog格式解析 binlog想必大家都不陌生,在主从复制或者某些情况下的数据恢复会用到.由于binlog是二进制数据,要查看一般都借助mysqlbinlog工具.这篇笔记分析了b ...
- 看数据库的文件大小 MySQL Binlog日志的生成和清理规则
小结: 1.避免并行大大事务对磁盘.内存的消耗: MySQL数据文件导致实例空间满的解决办法_空间/内存_常见问题_云数据库 RDS 版-阿里云 https://help.aliyun.com/kno ...
- 【转载】mysql binlog日志自动清理及手动删除
说明:当开启mysql数据库主从时,会产生大量如mysql-bin.00000* log的文件,这会大量耗费您的硬盘空间.mysql-bin.000001mysql-bin.000002mysql-b ...
- mysql binlog日志自动清理及手动删除
说明:当开启mysql数据库主从时,会产生大量如mysql-bin.00000* log的文件,这会大量耗费您的硬盘空间.mysql-bin.000001mysql-bin.000002mysql-b ...
- 自动清理MySQL binlog日志
开启MySQL binlog日志的服务器,如果不设置自动清理日志,默认binlog日志一直保留着,时间一长,服务器磁盘空间被binlog日志占满,导致MySQL数据库出错. 使用下面方法可以安全清理b ...
随机推荐
- 快速质因数分解及素性测试&ABC142D
首先,这个整数的标准分解非常的显然易见对吧: 一般我们要把一个数分解成这个样子我们可以这样写: #include<cstdio> ],w[],k; void factorize(int n ...
- SQL修改数据表字段长度
alter table m_Assysn_t nocheck CONSTRAINT allAlter Table m_Assysn_t ALTER column ppid VARCHAR(150)al ...
- HDU 1171 Big Event in HDU (动态规划、01背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- RSA加密 抛异常 algid parse error, not a sequence
JDK1.8环境 参考:BouncyCastle的使用:https://blog.csdn.net/qq_29583513/article/details/78866461 可解决 公钥解密 私钥加密 ...
- MyBatis时间排序问题
在数据中create_time字段是DateTime类型, 逆向工程后实体类中对应的成员变量类型为Date 时间排序代码为: 测试结果: 时间排序错乱. 解决方法: 1,在数据库创建varchar类型 ...
- Python 入门之 软件开发规范
Python 入门之 软件开发规范 1.软件开发规范 -- 分文件 (1)为什么使用软件开发规范: 当几百行--大几万行代码存在于一个py文件中时存在的问题: 不便于管理 修改 可读性差 加载速度慢 ...
- ubuntu下python3虚拟环境的配置
安装相关包 sudo pip3 install virtualenv # 虚拟环境包 sudo pip3 install virtualenvwrapper # 虚拟环境管理包 创建虚拟环境目录 su ...
- 洛谷 - P3469 - BLO-Blockade - 割点
https://www.luogu.org/problem/P3469 翻译:一个原本连通的无向图,可以删除图中的一个点,求因为删除这个点所导致的不连通的有序点对的数量.或者说,删去这个点之后,各个连 ...
- Codeforces 1215C. Swap Letters
传送门 好像是个挺显然的贪心 首先每次交换当然要尽量一次交换就多两个相同的位置 即 优先把 $\begin{bmatrix}a\\ b\end{bmatrix}$ 和 $\begin{bmatrix} ...
- Linux文档整理之【Jenkins+Docker自动化部署.Net Core】
这次整理的文档是Jenkins+Docker实现自动化部署,很早之前就写的,今天有时间就搬到博客园做个记录. Jenkins是基于Java开发的一种持续集成工具,主要用于持续.自动的构建/测试软件等相 ...