mysql
### binlog
#### 查找路径
1
2
mysql -uXXX -p
SHOW VARIABLES LIKE 'datadir';
#### 转化为sql
1
mysqlbinlog --base64-output=DECODE-ROWS -v binlog.000507 > output.sql
#### 脚本统计
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import re
from collections import defaultdict
def parse_sql_file(sql_file):
# 统计结果
stats = defaultdict(lambda: defaultdict(int))
# 正则匹配 UPDATE/INSERT/DELETE 语句
pattern = re.compile(r'^### (UPDATE|INSERT INTO|DELETE FROM) `(.*?)`\.`(.*?)`', re.IGNORECASE)
# 读取文件
with open(sql_file, 'r', encoding='utf-8') as file:
for line in file:
match = pattern.search(line)
if match:
operation = match.group(1).upper()
database = match.group(2)
table = match.group(3)
stats[f"`{database}`.`{table}`"][operation] += 1
return stats
def print_stats(stats):
for table, operations in stats.items():
print(f"Table: {table}")
for operation, count in operations.items():
print(f" {operation}: {count}")
# 替换为你的 output.sql 文件路径
sql_file = 'first_20M_binlog'
stats = parse_sql_file(sql_file)
print_stats(stats)
This post is licensed under CC BY 4.0 by the author.