forked from dongwook-chan/go-mysql-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (44 loc) · 1.11 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"context"
"fmt"
"github.com/go-mysql-org/go-mysql/mysql"
"github.com/go-mysql-org/go-mysql/replication"
"os"
)
func stream() {
// Create a binlog syncer with a unique server id, the server id must be different from other MySQL's.
// flavor is mysql or mariadb
cfg := replication.BinlogSyncerConfig{
ServerID: 100,
Flavor: "mysql",
Host: "127.0.0.1",
Port: 3306,
User: "root",
Password: "",
}
syncer := replication.NewBinlogSyncer(cfg)
// Start sync with specified binlog file and position
streamer, _ := syncer.StartSync(mysql.Position{"mysql-bin.000003", 331})
// or you can start a gtid replication like
// streamer, _ := syncer.StartSyncGTID(gtidSet)
// the mysql GTID set likes this "de278ad0-2106-11e4-9f8e-6edd0ca20947:1-2"
// the mariadb GTID set likes this "0-1-100"
for {
ctx := context.Background()
ev, _ := streamer.GetEvent(ctx)
// Dump event
ev.Dump(os.Stdout)
for _, b := range ev.RawData {
if b >= 32 && b <= 126 {
fmt.Printf("%c", b)
} else {
fmt.Printf("\\x%02x", b)
}
}
fmt.Printf("\n")
}
}
func main() {
stream()
}