Last active
April 17, 2018 15:02
-
-
Save zeayes/e21e62b725d1533c9d51d7399be7cbc1 to your computer and use it in GitHub Desktop.
parse mysql binlog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/binary" | |
"io" | |
"os" | |
) | |
const ( | |
UNKNOWN_EVENT = 0 | |
START_EVENT_V3 = 1 | |
QUERY_EVENT = 2 | |
STOP_EVENT = 3 | |
ROTATE_EVENT = 4 | |
INTVAR_EVENT = 5 | |
LOAD_EVENT = 6 | |
SLAVE_EVENT = 7 | |
CREATE_FILE_EVENT = 8 | |
APPEND_BLOCK_EVENT = 9 | |
EXEC_LOAD_EVENT = 10 | |
DELETE_FILE_EVENT = 11 | |
NEW_LOAD_EVENT = 12 | |
RAND_EVENT = 13 | |
USER_VAR_EVENT = 14 | |
FORMAT_DESCRIPTION_EVENT = 15 | |
XID_EVENT = 16 | |
BEGIN_LOAD_QUERY_EVENT = 17 | |
EXECUTE_LOAD_QUERY_EVENT = 18 | |
TABLE_MAP_EVENT = 19 | |
PRE_GA_WRITE_ROWS_EVENT = 20 | |
PRE_GA_UPDATE_ROWS_EVENT = 21 | |
PRE_GA_DELETE_ROWS_EVENT = 22 | |
WRITE_ROWS_EVENT = 23 | |
UPDATE_ROWS_EVENT = 24 | |
DELETE_ROWS_EVENT = 25 | |
INCIDENT_EVENT = 26 | |
HEARTBEAT_LOG_EVENT = 27 | |
IGNORABLE_LOG_EVENT = 28 | |
ROWS_QUERY_LOG_EVENT = 29 | |
// WRITE_ROWS_EVENT = 30 | |
// UPDATE_ROWS_EVENT = 31 | |
// DELETE_ROWS_EVENT = 32 | |
GTID_LOG_EVENT = 33 | |
ANONYMOUS_GTID_LOG_EVENT = 34 | |
PREVIOUS_GTIDS_LOG_EVENT = 35 | |
) | |
type EventHeader struct { | |
Timestamp uint32 | |
TypeCode uint8 | |
ServerID uint32 | |
EventLength uint32 | |
NextPosition uint32 | |
Flags uint16 | |
} | |
type FormatDescriptionEvent struct { | |
Version [2]byte | |
ServerVersion [50]byte | |
Timestamp uint32 | |
HeaderLength uint8 | |
} | |
func main() { | |
file, err := os.Open("mysql_binlog.000001") | |
if err != nil { | |
panic(err) | |
} | |
var offset int64 = 4 | |
for { | |
var header EventHeader | |
file.Seek(offset, 0) | |
err = binary.Read(file, binary.LittleEndian, &header) | |
if err == io.EOF { | |
break | |
} | |
if err != nil { | |
panic(err) | |
} | |
offset += int64(header.EventLength) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment