package util
import (
"fmt"
"om-admin/config"
"github.com/go-mysql-org/go-mysql/canal"
"github.com/siddontang/go/log"
"github.com/sirupsen/logrus"
)
func BinlogSetup() {
conf := config.Conf.Database
// 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 := canal.NewDefaultConfig()
cfg.Addr = fmt.Sprintf("%s:%d", conf.Host, conf.Port)
cfg.User = conf.Username
cfg.Password = conf.Password
cfg.Dump.ExecutionPath = ""
cfg.IncludeTableRegex = []string{conf.Database + "\\.sys_users", conf.Database + "\\.sys_roles"}
c, err := canal.NewCanal(cfg)
if err != nil {
logrus.WithField("BINLOG", err.Error()).Error("init")
}
// Register a handler to handle RowsEvent
c.SetEventHandler(&MyEventHandler{})
// Start canal
c.Run()
}
type MyEventHandler struct {
canal.DummyEventHandler
}
func (h *MyEventHandler) OnRow(e *canal.RowsEvent) error {
fmt.Println("数据库:", e.Table.Schema, e.Table.Name, e.Rows)
log.Infof("%s %v\n", e.Action, e.Rows)
return nil
}
踩坑
- 官方demo报错
exec: “mysqldump”: executable file not found in $PATH
不设置的话必须给个空子串,否则会空指针