Skip to content

Instantly share code, notes, and snippets.

@insom
Last active March 8, 2022 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save insom/d9d998af867e72dd805b4a36ec41e9cc to your computer and use it in GitHub Desktop.
Save insom/d9d998af867e72dd805b4a36ec41e9cc to your computer and use it in GitHub Desktop.
Add new statement-based replication records to a MySQL binary log
1b 7e 27 62 = Timestamp
13 = Type code
01 = Server ID
00 00 00 30 = Event Length
0000 00be = Next Position
0100 = Flags
---
00 00 6d = Table ID
00 00 = Reserved
00 00 00 01 00 = ??
03 = Length of DB Name
66 6f 6f 00 = 'foo'
03 = Length of Table Name
62 61 72 00 = 'bar'
02 = Number of Columns
03 = First Column Type (Long) (0 meta length)
0f = Second Column Type (Varchar) (2 bytes)
02 = Length of metadata
fc 03 = Second column metadata (1020 in hex)
03 = NULLable columns
62e1 6894 = CRC32
00000000: fe62 696e 84e6 1a62 0f01 0000 0077 0000 .bin...b.....w..
00000010: 007b 0000 0000 0004 0035 2e37 2e33 352d .{.......5.7.35-
00000020: 3338 2d6c 6f67 0000 0000 0000 0000 0000 38-log..........
00000030: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000040: 0000 0000 0000 0000 0000 0000 0000 0013 ................
00000050: 380d 0008 0012 0004 0404 0412 0000 5f00 8............._.
00000060: 041a 0800 0000 0808 0802 0000 000a 0a0a ................
00000070: 2a2a 0012 3400 010c 6eb7 2984 e61a 6223 **..4...n.)...b#
00000080: 0100 0000 1f00 0000 9a00 0000 8000 0000 ................
00000090: 0000 0000 0000 fc58 554c 84e6 1a62 2101 .......XUL...b!.
000000a0: 0000 0041 0000 00db 0000 0000 0001 e169 ...A...........i
000000b0: ac39 9776 11ec b8eb 0242 ac14 0002 0100 .9.v.....B......
000000c0: 0000 0000 0000 0200 0000 0000 0000 0001 ................
000000d0: 0000 0000 0000 00a3 387a 1084 e61a 6202 ........8z....b.
000000e0: 0100 0000 5b00 0000 3601 0000 0800 0200 ....[...6.......
000000f0: 0000 0000 0000 0300 0020 0000 0000 0000 ......... ......
00000100: 0120 00a0 5500 0000 0006 0373 7464 0421 . ..U......std.!
00000110: 0021 00e0 000c 0166 6f6f 0066 6f6f 0063 .!.....foo.foo.c
00000120: 7265 6174 6520 6461 7461 6261 7365 2066 reate database f
00000130: 6f6f af74 11ab 84e6 1a62 2101 0000 0041 oo.t.....b!....A
00000140: 0000 0077 0100 0000 0001 e169 ac39 9776 ...w.......i.9.v
00000150: 11ec b8eb 0242 ac14 0002 0200 0000 0000 .....B..........
00000160: 0000 0201 0000 0000 0000 0002 0000 0000 ................
00000170: 0000 0020 44cd ae84 e61a 6202 0100 0000 ... D.....b.....
00000180: 6200 0000 d901 0000 0000 0200 0000 0000 b...............
00000190: 0000 0000 0020 0000 0000 0000 0120 00a0 ..... ....... ..
000001a0: 5500 0000 0006 0373 7464 0421 0021 00e0 U......std.!.!..
000001b0: 000c 0166 6f6f 0000 6372 6561 7465 2074 ...foo..create t
000001c0: 6162 6c65 2066 6f6f 2e62 6172 2028 6964 able foo.bar (id
000001d0: 2069 6e74 2903 0025 9a84 e61a 6221 0100 int)..%....b!..
000001e0: 0000 4100 0000 1a02 0000 0000 01e1 69ac ..A...........i.
000001f0: 3997 7611 ecb8 eb02 42ac 1400 0203 0000 9.v.....B.......
00000200: 0000 0000 0002 0200 0000 0000 0000 0300 ................
00000210: 0000 0000 0000 2dc3 5c1f 84e6 1a62 0201 ......-.\....b..
00000220: 0000 004a 0000 0064 0200 0008 0002 0000 ...J...d........
00000230: 0000 0000 0000 0000 2000 0000 0000 0001 ........ .......
00000240: 2000 a055 0000 0000 0603 7374 6404 2100 ..U......std.!.
00000250: 2100 e000 0c01 666f 6f00 0042 4547 494e !.....foo..BEGIN
00000260: 9477 507b 84e6 1a62 0201 0000 0063 0000 .wP{...b.....c..
00000270: 00c7 0200 0000 0002 0000 0000 0000 0000 ................
00000280: 0000 2000 0000 0000 0001 2000 a055 0000 .. ....... ..U..
00000290: 0000 0603 7374 6404 2100 2100 e000 0c01 ....std.!.!.....
000002a0: 666f 6f00 0069 6e73 6572 7420 696e 746f foo..insert into
000002b0: 2066 6f6f 2e62 6172 2076 616c 7565 7320 foo.bar values
000002c0: 2831 2919 9648 0a84 e61a 6210 0100 0000 (1)..H....b.....
000002d0: 1f00 0000 e602 0000 0000 0d00 0000 0000 ................
000002e0: 0000 e660 e1e4 84e6 1a62 0401 0000 002f ...`.....b...../
000002f0: 0000 0015 0300 0000 0004 0000 0000 0000 ................
00000300: 006d 7973 716c 2d62 696e 2e30 3030 3030 .mysql-bin.00000
00000310: 33f3 714d ab 3.qM.
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
/* From https://stackoverflow.com/a/21001712 -- changed to not expect a NULL-terminated string */
unsigned int crc32b(unsigned char *message, int length) {
int i, j;
unsigned int byte, crc, mask;
i = 0;
crc = 0xFFFFFFFF;
while (i < length) {
byte = message[i]; // Get next byte.
crc = crc ^ byte;
for (j = 7; j >= 0; j--) { // Do eight times.
mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
i = i + 1;
}
return ~crc;
}
int main(int c, char** v) {
char header[1000];
char binlog_entry[1000];
int in, out;
in = open("mysql-bin.000002", O_RDONLY);
read(in, header, 154);
lseek(in, 612, SEEK_SET);
bzero(binlog_entry, 1000);
read(in, binlog_entry, 99);
out = open("output", O_CREAT|O_WRONLY, 0666);
write(out, header, 154);
lseek(out, 612, SEEK_SET);
write(out, binlog_entry, 99);
char sql[] = "INSERT INTO real_table_that_exists VALUES ('real_data_that_exists_blah')";
memcpy(binlog_entry + 65, sql, strlen(sql));
long length = 65 + strlen(sql) + 4;
long position = length + lseek(out, 0, SEEK_CUR);
memcpy(binlog_entry + 13, &position, 4);
memcpy(binlog_entry + 9, &length, 4);
unsigned int crc = crc32b(binlog_entry, length - 4);
memcpy(binlog_entry + length - 4, &crc, 4);
write(out, binlog_entry, length);
close(out);
close(in);
}
.PHONY: all
all: main
./main
mysqlbinlog --verify-binlog-checksum --start-position 711 output
main: main.c
gcc main.c -o main
1b7e 2762 = Timestamp
1e = Type Code (v2 rows event)
01 = Server ID
0000 0038 = Event Length
0000 00f6 = Next Position
0100 = Flags
00 = ??
00 00 6d = Table ID
00 00 = Reserved (Row Level Flags)
00 00 00 01 == Extra Data Length ??
00 = ?? Extra Data
02 = Column Count
00 02 = Columns used (bitmap)
ff fc = Columns null
0200 .m..............
000001e0: 0000 0e00 7468 6973 2069 7320 6120 7465 ....this is a te
000001f0: 7374
d6c8 5307 = Checksum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment