Documentation
¶
Overview ¶
Package binlog implements mysql binlog replication protocol.
This library is mainly aimed to provide RBR event parsing.
to connect to mysql server:
bl, err := binlog.Dial("tcp", "localhost:3306", 5*time.Second) if err != nil { return err } if bl.IsSSLSupported() { if err = bl.UpgradeSSL(tlsConfig); err != nil { return err } } if err := bl.Authenticate("root", "secret"); err != nil { return err }
to get binlog events from server:
serverID := 0 // use non-zero to wait for more data after end of last log if serverID !=0 { // send heartbeatEvent when there is no more data if err := bl.SetHeartbeatPeriod(30 * time.Second); err != nil { return err } } if err := bl.Seek(serverID, "binlog.000001", 4); err != nil { return err } for { e, err := bl.NextEvent() if err != nil { if err == io.EOF { break } return err } re, ok := e.Data.(binlog.RowsEvent) if !ok { continue } fmt.Sprintf("Table: %s.%s\n", re.TableMap.SchemaName, re.TableMap.TableName) if e.IsWriteRows() { fmt.Println("Action: insert") else if e.IsUpdateRows() { fmt.Println("Action: update") else if e.IsDeleteRows() { fmt.Println("Action: delete") } for { row, _, err := bl.NextRow() if err != nil { if err == io.EOF { break } return err } for i, v := range row { col := d.Columns()[i] fmt.Sprintf("col=%s ordinal=%d value=%v\n", col.Name, col.Ordinal, v) } } }
this package also supports the following:
- dump to local directory
- resume dump from where it left
- read binlog files from dump directory as if it is server
for example usage see cmd/binlog/main.go
Index ¶
- Variables
- type Column
- type ColumnType
- type Decimal
- type Enum
- type Event
- type EventHeader
- type EventType
- type FormatDescriptionEvent
- type HeartbeatEvent
- type IncidentEvent
- type IntVarEvent
- type JSON
- type Local
- func (bl *Local) ListFiles() ([]string, error)
- func (bl *Local) MasterStatus() (file string, pos uint32, err error)
- func (bl *Local) NextEvent() (Event, error)
- func (bl *Local) NextRow() (values []interface{}, valuesBeforeUpdate []interface{}, err error)
- func (bl *Local) RemoveFirstFile() error
- func (bl *Local) Seek(serverID uint32, fileName string, position uint32) error
- type QueryEvent
- type RandEvent
- type Remote
- func (bl *Remote) Authenticate(username, password string) error
- func (bl *Remote) Close() error
- func (bl *Remote) Dump(dir string) error
- func (bl *Remote) IsSSLSupported() bool
- func (bl *Remote) ListFiles() ([]string, error)
- func (bl *Remote) MasterStatus() (file string, pos uint32, err error)
- func (bl *Remote) NextEvent() (Event, error)
- func (bl *Remote) NextRow() (values []interface{}, valuesBeforeUpdate []interface{}, err error)
- func (bl *Remote) Seek(serverID uint32, fileName string, position uint32) error
- func (bl *Remote) SetHeartbeatPeriod(d time.Duration) error
- func (bl *Remote) UpgradeSSL(tlsConfig *tls.Config) error
- type RotateEvent
- type RowsEvent
- type RowsQueryEvent
- type Set
- type StopEvent
- type TableMapEvent
- type UnknownEvent
- type UserVarEvent
Constants ¶
This section is empty.
Variables ¶
var ErrMalformedPacket = errors.New("binlog: malformed packet")
ErrMalformedPacket used to indicate malformed packet.
Functions ¶
This section is empty.
Types ¶
type Column ¶
type Column struct { Ordinal int Type ColumnType Nullable bool Unsigned bool Meta uint16 Charset uint64 // value zero means unknown. // following are populated only if // system variable binlog_row_metadata==FULL Name string Values []string // permitted values for Enum and Set type. }
Column captures column info for TableMapEvent and RowsEvent.
type ColumnType ¶
type ColumnType uint8
ColumnType used in TableMapEvent and RowsEvent.
const ( TypeDecimal ColumnType = 0x00 TypeTiny ColumnType = 0x01 // int8 or uint8. TINYINT TypeShort ColumnType = 0x02 // int16 or uint16. SMALLINT TypeLong ColumnType = 0x03 // int32 or uint32. INT TypeFloat ColumnType = 0x04 // float32. FLOAT TypeDouble ColumnType = 0x05 // float64. DOUBLE TypeNull ColumnType = 0x06 TypeTimestamp ColumnType = 0x07 TypeLongLong ColumnType = 0x08 // int64 or uint64. BIGINT TypeInt24 ColumnType = 0x09 // int32 or uint32. MEDIUMINT TypeDate ColumnType = 0x0a // time.Time(UTC). DATE TypeTime ColumnType = 0x0b TypeDateTime ColumnType = 0x0c TypeYear ColumnType = 0x0d // int. YEAR TypeNewDate ColumnType = 0x0e TypeVarchar ColumnType = 0x0f // string. VARCHAR TypeBit ColumnType = 0x10 // uint64. BIT TypeTimestamp2 ColumnType = 0x11 // time.Time(LOCAL). TIMESTAMP TypeDateTime2 ColumnType = 0x12 // time.Time(UTC). DATETIME TypeTime2 ColumnType = 0x13 // time.Duration. TIME TypeJSON ColumnType = 0xf5 // JSON, JSON TypeNewDecimal ColumnType = 0xf6 // Decimal. DECIMAL NUMERIC TypeEnum ColumnType = 0xf7 // Enum. ENUM TypeSet ColumnType = 0xf8 // Set. SET TypeTinyBlob ColumnType = 0xf9 TypeMediumBlob ColumnType = 0xfa TypeLongBlob ColumnType = 0xfb TypeBlob ColumnType = 0xfc // []byte or string. TINYBLOB BLOB MEDIUMBLOB LONGBLOB TINYTEXT TEXT MEDIUMTEXT LONGTEXT TypeVarString ColumnType = 0xfd TypeString ColumnType = 0xfe // string. CHAR TypeGeometry ColumnType = 0xff )
ColumnType Constants
https://dev.mysql.com/doc/internals/en/com-query-response.html#packet-Protocol::ColumnType
func (ColumnType) String ¶
func (t ColumnType) String() string
type Decimal ¶
type Decimal string
A Decimal represents a MySQL Decimal/Numeric literal.
https://dev.mysql.com/doc/refman/8.0/en/fixed-point-types.html
func (Decimal) MarshalJSON ¶
type Enum ¶
type Enum struct { // index value. refers to a position in list of permitted values. // begins with 1. // 0 means empty string invalid value. Val uint16 // list of permitted values. // will be populated only if system // variable binlog_row_metadata==FULL Values []string }
Enum represents value of TypeEnum.
https://dev.mysql.com/doc/refman/8.0/en/enum.html
func (Enum) MarshalJSON ¶
type Event ¶
type Event struct { Header EventHeader Data interface{} // one of XXXEvent }
Event represents Binlog Event.
type EventHeader ¶
type EventHeader struct { Timestamp uint32 // seconds since unix epoch EventType EventType // binlog event type ServerID uint32 // server-id of the originating mysql-server EventSize uint32 // size of the event (header + post-header + body) LogFile string // logfile of the next event NextPos uint32 // position of the next event Flags uint16 // flags }
EventHeader represents Binlog Event Header.
https://dev.mysql.com/doc/internals/en/binlog-event-header.html https://dev.mysql.com/doc/internals/en/event-header-fields.html
type EventType ¶
type EventType uint8
EventType represents Binlog Event Type.
const ( UNKNOWN_EVENT EventType = 0x00 // should never occur. used when event cannot be recognized. START_EVENT_V3 EventType = 0x01 // descriptor event written to binlog beginning. deprecated. QUERY_EVENT EventType = 0x02 // written when an updating statement is done. STOP_EVENT EventType = 0x03 // written when mysqld stops. ROTATE_EVENT EventType = 0x04 // written when mysqld switches to a new binary log file. INTVAR_EVENT EventType = 0x05 // if stmt uses AUTO_INCREMENT col or LAST_INSERT_ID(). LOAD_EVENT EventType = 0x06 // used for LOAD DATA INFILE statements in MySQL 3.23. SLAVE_EVENT EventType = 0x07 // not used. CREATE_FILE_EVENT EventType = 0x08 // used for LOAD DATA INFILE statements in MySQL 4.0 and 4.1. APPEND_BLOCK_EVENT EventType = 0x09 // used for LOAD DATA INFILE statements in MySQL 4.0 and 4.1. EXEC_LOAD_EVENT EventType = 0x0a // used for LOAD DATA INFILE statements in MySQL 4.0 and 4.1. DELETE_FILE_EVENT EventType = 0x0b // used for LOAD DATA INFILE statements in MySQL 4.0 and 4.1. NEW_LOAD_EVENT EventType = 0x0c // used for LOAD DATA INFILE statements in MySQL 4.0 and 4.1. RAND_EVENT EventType = 0x0d // if stmt uses RAND(). USER_VAR_EVENT EventType = 0x0e // if stmt uses a user variable. FORMAT_DESCRIPTION_EVENT EventType = 0x0f // descriptor event written to binlog beginning. XID_EVENT EventType = 0x10 // for XA commit transaction. BEGIN_LOAD_QUERY_EVENT EventType = 0x11 // used for LOAD DATA INFILE statements in MySQL 5.0. EXECUTE_LOAD_QUERY_EVENT EventType = 0x12 // used for LOAD DATA INFILE statements in MySQL 5.0. TABLE_MAP_EVENT EventType = 0x13 // precedes rbr event. contains table definition. WRITE_ROWS_EVENTv0 EventType = 0x14 // logs inserts of rows in a single table. UPDATE_ROWS_EVENTv0 EventType = 0x15 // logs updates of rows in a single table. DELETE_ROWS_EVENTv0 EventType = 0x16 // logs deletions of rows in a single table. WRITE_ROWS_EVENTv1 EventType = 0x17 // logs inserts of rows in a single table. UPDATE_ROWS_EVENTv1 EventType = 0x18 // logs updates of rows in a single table. DELETE_ROWS_EVENTv1 EventType = 0x19 // logs inserts of rows in a single table. INCIDENT_EVENT EventType = 0x1a // used to log an out of the ordinary event that occurred on the master. HEARTBEAT_EVENT EventType = 0x1b // to signal that master is still alive. not written to file. IGNORABLE_EVENT EventType = 0x1c ROWS_QUERY_EVENT EventType = 0x1d WRITE_ROWS_EVENTv2 EventType = 0x1e // logs inserts of rows in a single table. UPDATE_ROWS_EVENTv2 EventType = 0x1f // logs updates of rows in a single table. DELETE_ROWS_EVENTv2 EventType = 0x20 // logs inserts of rows in a single table. GTID_EVENT EventType = 0x21 ANONYMOUS_GTID_EVENT EventType = 0x22 PREVIOUS_GTIDS_EVENT EventType = 0x23 )
Event Type Constants.
https://dev.mysql.com/doc/internals/en/binlog-event-type.html https://dev.mysql.com/doc/internals/en/event-meanings.html
func (EventType) IsDeleteRows ¶
IsDeleteRows tells if this EventType DELETE_ROWS_EVENT. MySQL has multiple versions of DELETE_ROWS_EVENT.
func (EventType) IsUpdateRows ¶
IsUpdateRows tells if this EventType UPDATE_ROWS_EVENT. MySQL has multiple versions of UPDATE_ROWS_EVENT.
func (EventType) IsWriteRows ¶
IsWriteRows tells if this EventType WRITE_ROWS_EVENT. MySQL has multiple versions of WRITE_ROWS_EVENT.
type FormatDescriptionEvent ¶
type FormatDescriptionEvent struct { BinlogVersion uint16 // version of this binlog format ServerVersion string // version of the MySQL Server that created the binlog CreateTimestamp uint32 // seconds since Unix epoch when the binlog was created EventHeaderLength uint8 // length of the Binlog Event Header of next events EventTypeHeaderLengths []byte // post-header lengths for different event-types }
FormatDescriptionEvent is written to the beginning of the each binary log file. This event is used as of MySQL 5.0; it supersedes START_EVENT_V3.
https://dev.mysql.com/doc/internals/en/format-description-event.html
type HeartbeatEvent ¶
type HeartbeatEvent struct{}
HeartbeatEvent sent by a master to a slave to let the slave know that the master is still alive. Not written to log files.
type IncidentEvent ¶
IncidentEvent used to log an out of the ordinary event that occurred on the master. It notifies the slave that something happened on the master that might cause data to be in an inconsistent state.
type IntVarEvent ¶
type IntVarEvent struct { // Type indicates subtype. // // INSERT_ID_EVENT(0x02) indicates the value to use for an AUTO_INCREMENT column in the next statement. // // LAST_INSERT_ID_EVENT(0x01) indicates the value to use for the LAST_INSERT_ID() function in the next statement. Type uint8 Value uint64 }
IntVarEvent written every time a statement uses an AUTO_INCREMENT column or the LAST_INSERT_ID() function. It precedes other events for the statement. This is written only before a QUERY_EVENT and is not used with row-based logging.
type JSON ¶
type JSON struct{ Val interface{} }
Json represents value of TypeJSON
https://dev.mysql.com/doc/refman/8.0/en/json.html
func (JSON) MarshalJSON ¶
type Local ¶
type Local struct {
// contains filtered or unexported fields
}
Local represents connection to local dump directory.
func (*Local) MasterStatus ¶
MasterStatus provides status information about the binary log files in dump directory.
func (*Local) NextEvent ¶
NextEvent return next binlog event.
return io.EOF when there are no more Events
func (*Local) NextRow ¶
NextRow returns next row for RowsEvent. Returns io.EOF when there are no more rows. valuesBeforeUpdate should be used only for events UPDATE_ROWS_EVENTv1, UPDATE_ROWS_EVENTv2.
func (*Local) RemoveFirstFile ¶
RemoveFirstFile deletes the first binary log file from dump directory.
type QueryEvent ¶
type QueryEvent struct { SlaveProxyID uint32 ExecutionTIme uint32 ErrorCode uint16 StatusVars []byte Schema string Query string }
QueryEvent is written when an updating statement is done. The query event is used to send text query right the binlog.
type RandEvent ¶
RandEvent is written every time a statement uses the RAND() function. It precedes other events for the statement. Indicates the seed values to use for generating a random number with RAND() in the next statement. This is written only before a QUERY_EVENT and is not used with row-based logging.
type Remote ¶
type Remote struct {
// contains filtered or unexported fields
}
Remote represents connection to MySQL server.
func (*Remote) Authenticate ¶
Authenticate sends the credentials to MySQL.
func (*Remote) IsSSLSupported ¶
IsSSLSupported tells whether MySQL server supports SSL.
func (*Remote) ListFiles ¶
ListFiles lists the binary log files on the server, in the order they were created. It is equivalent to `SHOW BINARY LOGS` statement.
func (*Remote) MasterStatus ¶
MasterStatus provides status information about the binary log files of the server. It is equivalent to `SHOW MASTER STATUS` statement.
func (*Remote) NextEvent ¶
NextEvent return next binlog event.
return io.EOF when there are no more Events
func (*Remote) NextRow ¶
NextRow returns next row for RowsEvent. Returns io.EOF when there are no more rows. valuesBeforeUpdate should be used only for events UPDATE_ROWS_EVENTv1, UPDATE_ROWS_EVENTv2.
func (*Remote) Seek ¶
Seek requests binlog at fileName and position.
if serverID is zero, NextEvent return io.EOF when there are no more events. if serverID is non-zero, NextEvent waits for new events.
func (*Remote) SetHeartbeatPeriod ¶
SetHeartbeatPeriod configures the interval to send HeartBeatEvent in absence of data. This avoids connection timeout occurring in the absence of data. Setting interval to 0 disables heartbeats altogether.
Use this, if you are using non-zero serverID to Seek method. In this case, server sends heartbeatEvents when there are no more events.
type RotateEvent ¶
type RotateEvent struct { Position uint64 // position of next event NextBinlog string // name of next binlog file }
RotateEvent is written when mysqld switches to a new binary log file. This occurs when someone issues a FLUSH LOGS statement or the current binary log file becomes too large. The maximum size is determined by max_binlog_size.
type RowsEvent ¶
type RowsEvent struct { TableMap *TableMapEvent // associated TableMapEvent // contains filtered or unexported fields }
RowsEvent captures changed rows in a table.
see https://dev.mysql.com/doc/internals/en/rows-event.html
func (RowsEvent) ColumnsBeforeUpdate ¶
ColumnsBeforeUpdate returns columns after after update. returns nil, if rows were inserted.
type RowsQueryEvent ¶
type RowsQueryEvent struct {
Query string
}
RowsQueryEvent captures the query that caused the following ROWS_EVENT. see https://dev.mysql.com/doc/internals/en/rows-query-event.html
system variable binlog_rows_query_log_events must be ON for this event. see https://dev.mysql.com/doc/refman/5.7/en/replication-options-binary-log.html#sysvar_binlog_rows_query_log_events
type Set ¶
type Set struct { // set's numerical value with bits set corresponding // to the set members that make up the column value. // 0 means empty string invalid value. Val uint64 // list of permitted values. // will be populated only if system // variable binlog_row_metadata==FULL Values []string }
Set represents value of TypeSet.
https://dev.mysql.com/doc/refman/8.0/en/set.html
func (Set) MarshalJSON ¶
type TableMapEvent ¶
type TableMapEvent struct { SchemaName string TableName string Columns []Column // contains filtered or unexported fields }
TableMapEvent is first event used in Row Based Replication declares how a table that is about to be changed is defined.
Used for row-based binary logging. This event precedes each row operation event. It maps a table definition to a number, where the table definition consists of database and table names and column definitions. The purpose of this event is to enable replication when a table has different definitions on the master and slave.
Row operation events that belong to the same transaction may be grouped into sequences, in which case each such sequence of events begins with a sequence of TABLE_MAP_EVENT events: one per table used by events in the sequence.
see https://dev.mysql.com/doc/internals/en/table-map-event.html
type UnknownEvent ¶
type UnknownEvent struct{}
UnknownEvent should never occur. It is never written to a binary log. If an event is read from a binary log that cannot be recognized as something else, it is treated as UNKNOWN_EVENT.
type UserVarEvent ¶
type UserVarEvent struct { Name string Null bool Type uint8 Charset uint32 Value []byte Unsigned bool }
UserVarEvent is written every time a statement uses a user variable. It precedes other events for the statement. Indicates the value to use for the user variable in the next statement. This is written only before a QUERY_EVENT and is not used with row-based logging.