GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - 2tvenom/myreplication: Golang MySql binary log replication listener

BranchesTags
Open more actions menu

Latest commit

 

History

29 Commits
29 Commits

Folders and files

NameName
Last commit message
Last commit date
example
example
 
 
tests
tests
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
binlog_dump.go
binlog_dump.go
 
 
binlog_dump_test.go
binlog_dump_test.go
 
 
capability.go
capability.go
 
 
connect_db.go
connect_db.go
 
 
connect_db_test.go
connect_db_test.go
 
 
connection.go
connection.go
 
 
const.go
const.go
 
 
event_log.go
event_log.go
 
 
event_log_test.go
event_log_test.go
 
 
field_list.go
field_list.go
 
 
field_list_test.go
field_list_test.go
 
 
handshake.go
handshake.go
 
 
handshake_test.go
handshake_test.go
 
 
init_db.go
init_db.go
 
 
init_db_test.go
init_db_test.go
 
 
ok_packet.go
ok_packet.go
 
 
pack.go
pack.go
 
 
pack_test.go
pack_test.go
 
 
passwd.go
passwd.go
 
 
protocol.go
protocol.go
 
 
protocol_test.go
protocol_test.go
 
 
query.go
query.go
 
 
query_test.go
query_test.go
 
 
register_slave.go
register_slave.go
 
 
register_slave_test.go
register_slave_test.go
 
 
result_set.go
result_set.go
 
 
result_set_test.go
result_set_test.go
 
 

Repository files navigation

Go MySql binary log replication listener

Pure Go Implementation of MySQL replication protocol. This allow you to receive event like insert, update, delete with their datas and raw SQL queries. This code has been developed and maintained by Ven at January 2015.

Installation

go get github.com/2tvenom/myreplication

Test

The project is test with:

  • Go 1.3.3
  • MySQL 5.5, 5.6 and 5.7 (beta)
  • Docker 1.4.1 build 5bc2ff8 (functional tests)

It's not tested in real production situation.

Unit tests

go test

Docker tests

Functonal tests with Docker. Test statement based and row based replication. MySql versions 5.5, 5.6, 5.7.

cd tests
sudo ./test.sh

MySQL server settings

In your MySQL server configuration file you need to enable replication:

[mysqld]
server-id		 = 1
log_bin			 = /var/log/mysql/mysql-bin.log
expire_logs_days = 10
max_binlog_size  = 100M
binlog-format    = row #Row based replication

Example

package main

import (
	"fmt"
	"myreplication"
)

var (
	host     = "localhost"
	port     = 3307
	username = "admin"
	password = "admin"
)

func main() {
	newConnection := myreplication.NewConnection()
	serverId := uint32(2)
	err := newConnection.ConnectAndAuth(host, port, username, password)

	if err != nil {
		panic("Client not connected and not autentificate to master server with error:" + err.Error())
	}
	//Get position and file name
	pos, filename, err := newConnection.GetMasterStatus()

	if err != nil {
		panic("Master status fail: " + err.Error())
	}

	el, err := newConnection.StartBinlogDump(pos, filename, serverId)

	if err != nil {
		panic("Cant start bin log: " + err.Error())
	}
	events := el.GetEventChan()
	go func() {
		for {
			event := <-events

			switch e := event.(type) {
			case *myreplication.QueryEvent:
				//Output query event
				println(e.GetQuery())
			case *myreplication.IntVarEvent:
				//Output last insert_id  if statement based replication
				println(e.GetValue())
			case *myreplication.WriteEvent:
				//Output Write (insert) event
				println("Write", e.GetTable())
				//Rows loop
				for i, row := range e.GetRows() {
					//Columns loop
					for j, col := range row {
						//Output row number, column number, column type and column value
						println(fmt.Sprintf("%d %d %d %v", i, j, col.GetType(), col.GetValue()))
					}
				}
			case *myreplication.DeleteEvent:
				//Output delete event
				println("Delete", e.GetTable())
				for i, row := range e.GetRows() {
					for j, col := range row {
						println(fmt.Sprintf("%d %d %d %v", i, j, col.GetType(), col.GetValue()))
					}
				}
			case *myreplication.UpdateEvent:
				//Output update event
				println("Update", e.GetTable())
				//Output old data before update
				for i, row := range e.GetRows() {
					for j, col := range row {
						println(fmt.Sprintf("%d %d %d %v", i, j, col.GetType(), col.GetValue()))
					}
				}
				//Output new
				for i, row := range e.GetNewRows() {
					for j, col := range row {
						println(fmt.Sprintf("%d %d %d %v", i, j, col.GetType(), col.GetValue()))
					}
				}
			default:
			}
		}
	}()
	err = el.Start()
	println(err.Error())
}

Links

  • MySql documentation http://dev.mysql.com/doc/internals/en/client-server-protocol.html
  • Python implementation. MySql 5.6 checksum compatibility https://github.com/noplay/python-mysql-replication
  • Go MySql client. Password encryption method https://github.com/ziutek/mymysql
  • Java implementation. Decimal type parsing https://github.com/whitesock/open-replicator
  • Ruby implementation. Decimal type parsing https://github.com/jeremycole/mysql_binlog
  • Docker files https://github.com/docker-library/mysql

Licence

WTFPL

About

Golang MySql binary log replication listener

Resources

Readme
WTFPL license
Activity

Stars

194 stars

Watchers

19 watching

Forks

43 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.