MySQL Proxy: playing with binlogs

With code in hands I can now toss in a liitle lua script to dump a simple statement-based binlog:

local binlog = assert(require("mysql.binlog"))
local basedir = "/usr/local/mysql/data/"
local file    = "jan-kneschkes-macbook-pro-bin.001005"

while file do
    local f = assert(binlog.open(basedir .. file))

    file = nil

    for event in f:next() do
            if event.type == "QUERY_EVENT" then
                    print(("/* threadid: %d */ %s"):format(
                            event.query.thread_id,
                            event.query.query))
            elseif event.type == "ROTATE_EVENT" then
                    print(("/* rotate to: %s */"):format(
                            event.rotate.binlog_file))
                    file = event.rotate.binlog_file -- rotate to the next binlog
            elseif event.type == "XID_EVENT" then
                    print(("COMMIT /* xid = %d */"):format(event.xid.xid_id))
            elseif event.type == "FORMAT_DESCRIPTION_EVENT" then
                    print(("/* version: %d from %s created at %d */"):format(
                            event.format.binlog_version, 
                            event.format.master_version, 
                            event.format.created_ts))
            end
    end

    f:close()
end

Just a little code to run through all the rotated binlogs and dump them.

If you want you can add your own filters, toss out records and just analyse the binlogs.

Next step is finishing the row-based support making it easier to work with them.