巧用python-mysql-replication寻找pos点
时间:2021-07-01 10:21:17
帮助过:14人阅读
!/usr/bin/python
# -*- coding:utf-8 -*-
import sys
import datetime
from pymysqlreplication
import BinLogStreamReader
from pymysqlreplication.row_event
import DeleteRowsEvent, UpdateRowsEvent, WriteRowsEvent
from pymysqlreplication.event
import RotateEvent,QueryEvent
reload(sys)
sys.setdefaultencoding(‘utf8‘)
conn_setting =
{
"host":
"127.0.0.1",
"port": 3306
,
"user":
"xx",
"passwd":
"xx"
}
stream =
BinLogStreamReader(
connection_settings=
conn_setting,
server_id=9999
,
log_file=
"mysql-bin.000009",
log_pos=4
,
resume_stream=
True,
blocking=
True
)
for binlogevent
in stream:
if isinstance(binlogevent, RotateEvent):
current_master_log_file=
binlogevent.next_binlog
print "Next binlog file: %s" %
(current_master_log_file)
if isinstance(binlogevent, WriteRowsEvent)
or isinstance(binlogevent, DeleteRowsEvent)
or isinstance(binlogevent, UpdateRowsEvent)
or isinstance(binlogevent, QueryEvent):
if binlogevent.packet.server_id == 123
:
current_datetime=
datetime.datetime.fromtimestamp(binlogevent.packet.timestamp)
print "数据写入时间: %s" %
(current_datetime)
start_binlog_file=
current_master_log_file
start_binlog_pos=
binlogevent.packet.log_pos
print "开始的binlog文件: %s" %
(start_binlog_file)
print "开始的binlog pos点: %s" %
(start_binlog_pos)
exit(1)
输出结果如下:
Next binlog file: mysql-bin.000001
数据写入时间: 2020-05-11 11:56:09
开始的binlog文件: mysql-bin.000001
开始的binlog pos点: 146147
我们自己动手来解析binlog看看输出的pos点是否准确:
可以看见是准确无误的。
总结:
线上一般会开启gtid或者使用其他高可用组件进行切换。python-mysql-replication是个好东西,建议同学们都学习使用一下。
巧用python-mysql-replication寻找pos点
标签:span 就是 resume stream passwd locking sql bsp writer