-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathmain.go
101 lines (86 loc) · 2.56 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// go-mysqlbinlog: a simple binlog tool to sync remote MySQL binlog.
// go-mysqlbinlog supports semi-sync mode like facebook mysqlbinlog.
// see http://yoshinorimatsunobu.blogspot.com/2014/04/semi-synchronous-replication-at-facebook.html
package main
import (
"context"
"flag"
"fmt"
"os"
"github.com/pingcap/errors"
"github.com/go-mysql-org/go-mysql/mysql"
"github.com/go-mysql-org/go-mysql/replication"
)
var (
host = flag.String("host", "127.0.0.1", "MySQL host")
port = flag.Int("port", 3306, "MySQL port")
user = flag.String("user", "root", "MySQL user, must have replication privilege")
password = flag.String("password", "", "MySQL password")
flavor = flag.String("flavor", "mysql", "Flavor: mysql or mariadb")
file = flag.String("file", "", "Binlog filename")
pos = flag.Int("pos", 4, "Binlog position")
gtid = flag.String("gtid", "", "Binlog GTID set that this slave has executed")
semiSync = flag.Bool("semisync", false, "Support semi sync")
backupPath = flag.String("backup_path", "", "backup path to store binlog files")
rawMode = flag.Bool("raw", false, "Use raw mode")
)
func main() {
flag.Parse()
cfg := replication.BinlogSyncerConfig{
ServerID: 101,
Flavor: *flavor,
Host: *host,
Port: uint16(*port),
User: *user,
Password: *password,
RawModeEnabled: *rawMode,
SemiSyncEnabled: *semiSync,
UseDecimal: true,
}
b := replication.NewBinlogSyncer(cfg)
pos := mysql.Position{Name: *file, Pos: uint32(*pos)}
if len(*backupPath) > 0 {
// Backup will always use RawMode.
err := b.StartBackup(*backupPath, pos, 0)
if err != nil {
fmt.Printf("Start backup error: %v\n", errors.ErrorStack(err))
return
}
} else {
var (
s *replication.BinlogStreamer
err error
)
if len(*gtid) > 0 {
gset, err := mysql.ParseGTIDSet(*flavor, *gtid)
if err != nil {
fmt.Printf("Failed to parse gtid %s with flavor %s, error: %v\n",
*gtid, *flavor, errors.ErrorStack(err))
}
s, err = b.StartSyncGTID(gset)
if err != nil {
fmt.Printf("Start sync by GTID error: %v\n", errors.ErrorStack(err))
return
}
} else {
s, err = b.StartSync(pos)
if err != nil {
fmt.Printf("Start sync error: %v\n", errors.ErrorStack(err))
return
}
}
for {
e, err := s.GetEvent(context.Background())
if err != nil {
// Try to output all left events
events := s.DumpEvents()
for _, e := range events {
e.Dump(os.Stdout)
}
fmt.Printf("Get event error: %v\n", errors.ErrorStack(err))
return
}
e.Dump(os.Stdout)
}
}
}