Replication Binary Logs

Nirbhay Choubey edited this page Feb 17, 2016 · 4 revisions

What are binary logs?

In MariaDB/MySQL database server, binary logs consists of events which describe the change in database state. These logs are normally used by an asynchronous slave server or Galera cluster node to replicate the changes to other server/nodes in order to distribute the load or simply create a backup of the database.

Vaquita provides a simple set of APIs to read replication events either via connecting to a MariaDB/MySQL database server (master) or directly from binary log files.

Read binary log events

Binary log events can be read either by connecting to master server or binary log file. When read, after having parsed the header, events are initially stored as RawEvents. Using RawEvent, while information about event's header like name, size, type, etc. can be retrieved, its body remains unparsed. Specific events can then be parsed out of RawEvent. The mechanism of having an intermediate type (RawEvent) provides an option of delaying the parsing of events. Thus, it is not required to parse each and every event.

dsn = "mysql://root@localhost:16001/test?BinlogSlaveId=100&BinlogDumpNonBlock=0"

// Open a connection to the master server
if err := b.Connect(dsn); err != nil {
        // error
}

// Close the binary log handle/connection
defer b.Close()

// Set the starting binary log file
b.SetFile("nirbhay-VirtualBox-1-bin.000002")

// Set the start position
b.SetPosition(4)

// Start the replication stream
if err := b.Begin(); err != nil {
        // error
}

// Get the next event
for b.Next() {
        re, err := b.RawEvent()
        if err != nil {
                // error
        }

        switch re.Type() {
        case mysql.QUERY_EVENT:
                ev := re.Event()
                switch v := ev.(type) {
                case *mysql.QueryEvent:
                        fmt.Println("Query: ", v.Query())
                default:
                }
        default:
        }
}

If BinlogVerifyChecksum is set to true, the checksum of individual binary log events is verified and the result is returns by Binlog.RawEvent(). Also, if BinlogDumpNonBlock is set to false, the connection blocks and waits for new events.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.