这里记录一下使用 go-mysql 里的示例时遇到的问题

示例代码

 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
package main

import (
	"github.com/go-mysql-org/go-mysql/canal"
	"github.com/siddontang/go-log/log"
)

type MyEventHandler struct {
	canal.DummyEventHandler
}

func (h *MyEventHandler) OnRow(e *canal.RowsEvent) error {
	//log.Infof("%s %v\n", e.Action, e.Rows)
	log.Infof("Action: %s,  rows: %#+v", e.Action, e.Rows)
	return nil
}

func (h *MyEventHandler) String() string {
	return "MyEventHandler"
}

func main() {
	cfg := canal.NewDefaultConfig()
	cfg.Addr = "127.0.0.1:8889"
	cfg.User = "root"
	cfg.Password = "123123"
	// We only care table canal_test in test db
	cfg.Dump.TableDB = "test"
	cfg.Dump.Tables = []string{"canal_test"}

	c, err := canal.NewCanal(cfg)
	if err != nil {
		log.Fatal(err)
	}

	// Register a handler to handle RowsEvent
	c.SetEventHandler(&MyEventHandler{})

	// Start canal
	c.Run()
}

启动是出现问题:

exec: “mysqldump”: executable file not found in $PATH

mysqldump的环境变量, 调整一下就行, 我用的 mamp, 所以直接

1
export PATH="$PATH:/Applications/MAMP/Library/bin/"

Access denied for user ‘root@localhost’ (using password:NO)

出现权限校验问题, 配置文件 my.cnf

1
2
[mysqld]
skip-grant-tables

canal.go:224 canal dump mysql err: exit status 2

没开启 binlog

我的 mysql 版本是 5.7.34, 同样调整配置文件 my.cnf

1
2
3
[mysqld]
log_bin=mysql-bin
server-id=1

mysql官方文档

参考资源: