Skip to content

Instantly share code, notes, and snippets.

@mahadirz
Last active January 22, 2022 18:53
Show Gist options
  • Save mahadirz/3c507bd9b94d9e31bf4892dd0a2cf250 to your computer and use it in GitHub Desktop.
Save mahadirz/3c507bd9b94d9e31bf4892dd0a2cf250 to your computer and use it in GitHub Desktop.
mysql_binlog_tutorial
# this is a partial code
# skip to 462
# https://dev.mysql.com/doc/internals/en/query-event.html
file_pointer = 462
print("==================")
print("file_pointer", file_pointer)
f.seek(file_pointer)
header = Header()
header.unpack(f.read(header_length))
file_pointer += header_length
print(header)
post_header = f.read(13)
unpacked = struct.unpack('<IIBHH', post_header)
"""
uint32_t thread_id;
uint32_t query_exec_time;
size_t db_len;
uint16_t error_code;
uint16_t status_vars_len;
"""
print(f"thread_id : {unpacked[0]} "
f"query_exec_time: {unpacked[1]} "
f"db_len: {unpacked[2]} "
f"error_code: {unpacked[3]} "
f"status_vars_len: {unpacked[4]} "
f"")
# Q_FLAGS2_CODE
key = f.read(1)
unpacked = struct.unpack('<B', key)[0]
print("Key", unpacked)
value = f.read(4)
unpacked = struct.unpack('<I', value)[0]
print("Q_FLAGS2_CODE", unpacked)
# Q_SQL_MODE_CODE
key = f.read(1)
unpacked = struct.unpack('<B', key)[0]
print("Key", unpacked)
value = f.read(8)
unpacked = struct.unpack('<Q', value)[0]
print("Q_SQL_MODE_CODE", unpacked)
# Q_TIME_ZONE_CODE
key = f.read(1)
unpacked = struct.unpack('<B', key)[0]
print("key", unpacked)
value = f.read(1)
lenx = struct.unpack('<B', value)[0]
timezone = f.read(lenx)
print("Timezone", timezone.decode())
# Q_CHARSET_CODE
key = f.read(1)
unpacked = struct.unpack('<B', key)[0]
print("key", unpacked)
value = f.read(6)
unpacked = struct.unpack('<HHH', value)
# 2-byte character_set_client + 2-byte collation_connection + 2-byte collation_server
print(f"character_set_client: {unpacked[0]} "
f"collation_connection: {unpacked[1]} "
f"collation_server: {unpacked[2]}")
# Q_UPDATED_DB_NAMES
key = f.read(1)
unpacked = struct.unpack('<B', key)[0]
print("key", unpacked)
lenx = struct.unpack('<B', f.read(1))[0]
# read by byte until find terminate char
for i in range(lenx):
string = b''
while True:
char = f.read(1)
if char == b'\0':
break
string += char
print("Database", string)
# Get DB Name
database = f.read(11)
print("schema:", database.decode())
f.read(1)
# Get the query
query = f.read(header.log_pos - f.tell() - 4)
print("query:", query.decode())
crc32 = struct.unpack("<I", f.read(4))[0]
print("crc32:", hex(crc32))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment