Skip to content

Commit ba6d354

Browse files
committed
Merge branch 'master' of github.com:go-mysql-org/go-mysql into prepare-1.10.0
2 parents 09b720d + 8b76415 commit ba6d354

File tree

23 files changed

+159
-808
lines changed

23 files changed

+159
-808
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ jobs:
1717
echo -n "mysqldump -V: " ; mysqldump -V
1818
1919
echo -e '[mysqld]\nserver-id=1\nlog-bin=mysql\nbinlog-format=row\ngtid-mode=ON\nenforce_gtid_consistency=ON\n' | sudo tee /etc/mysql/conf.d/replication.cnf
20+
21+
# bind to :: for dual-stack listening
22+
sudo sed -i 's/bind-address.*= 127.0.0.1/bind-address = ::/' /etc/mysql/mysql.conf.d/mysqld.cnf
23+
sudo sed -i 's/mysqlx-bind-address.*= 127.0.0.1/mysqlx-bind-address = ::/' /etc/mysql/mysql.conf.d/mysqld.cnf
24+
2025
sudo service mysql start
2126
2227
# apply this for mysql5 & mysql8 compatibility
@@ -109,5 +114,6 @@ jobs:
109114
uses: actions/setup-go@v5
110115
with:
111116
go-version: "1.22"
117+
112118
- name: Build on ${{ matrix.os }}/${{ matrix.arch }}
113119
run: GOARCH=${{ matrix.arch }} GOOS=${{ matrix.os }} go build ./...

README.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ This repo uses [Changelog](CHANGELOG.md).
2323
* [Incremental dumping](#canal)
2424
* [Client](#client)
2525
* [Fake server](#server)
26-
* [Failover](#failover)
2726
* [database/sql like driver](#driver)
2827

2928
## Replication
@@ -326,18 +325,6 @@ MySQL [(none)]>
326325
>
327326
> To customize server configurations, use ```NewServer()``` and create connection via ```NewCustomizedConn()```.
328327
329-
330-
## Failover
331-
332-
Failover supports to promote a new master and let replicas replicate from it automatically when the old master was down.
333-
334-
Failover supports MySQL >= 5.6.9 with GTID mode, if you use lower version, e.g, MySQL 5.0 - 5.5, please use [MHA](http://code.google.com/p/mysql-master-ha/) or [orchestrator](https://github.com/outbrain/orchestrator).
335-
336-
At the same time, Failover supports MariaDB >= 10.0.9 with GTID mode too.
337-
338-
Why only GTID? Supporting failover with no GTID mode is very hard, because replicas can not find the proper binlog filename and position with the new master.
339-
Although there are many companies use MySQL 5.0 - 5.5, I think upgrade MySQL to 5.6 or higher is easy.
340-
341328
## Driver
342329

343330
Driver is the package that you can use go-mysql with go database/sql like other drivers. A simple example:

canal/canal.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -481,18 +481,17 @@ func (c *Canal) prepareSyncer() error {
481481
if strings.Contains(c.cfg.Addr, "/") {
482482
cfg.Host = c.cfg.Addr
483483
} else {
484-
seps := strings.Split(c.cfg.Addr, ":")
485-
if len(seps) != 2 {
486-
return errors.Errorf("invalid mysql addr format %s, must host:port", c.cfg.Addr)
484+
host, port, err := net.SplitHostPort(c.cfg.Addr)
485+
if err != nil {
486+
return errors.Errorf("invalid MySQL address format %s, must host:port", c.cfg.Addr)
487487
}
488-
489-
port, err := strconv.ParseUint(seps[1], 10, 16)
488+
portNumber, err := strconv.ParseUint(port, 10, 16)
490489
if err != nil {
491490
return errors.Trace(err)
492491
}
493492

494-
cfg.Host = seps[0]
495-
cfg.Port = uint16(port)
493+
cfg.Host = host
494+
cfg.Port = uint16(portNumber)
496495
}
497496

498497
c.syncer = replication.NewBinlogSyncer(cfg)

canal/canal_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,30 @@ import (
1616
)
1717

1818
type canalTestSuite struct {
19+
addr string
1920
suite.Suite
2021
c *Canal
2122
}
2223

24+
type canalTestSuiteOption func(c *canalTestSuite)
25+
26+
func withAddr(addr string) canalTestSuiteOption {
27+
return func(c *canalTestSuite) {
28+
c.addr = addr
29+
}
30+
}
31+
32+
func newCanalTestSuite(opts ...canalTestSuiteOption) *canalTestSuite {
33+
c := new(canalTestSuite)
34+
for _, opt := range opts {
35+
opt(c)
36+
}
37+
return c
38+
}
39+
2340
func TestCanalSuite(t *testing.T) {
24-
suite.Run(t, new(canalTestSuite))
41+
suite.Run(t, newCanalTestSuite())
42+
suite.Run(t, newCanalTestSuite(withAddr(mysql.DEFAULT_IPV6_ADDR)))
2543
}
2644

2745
const (
@@ -37,6 +55,9 @@ const (
3755
func (s *canalTestSuite) SetupSuite() {
3856
cfg := NewDefaultConfig()
3957
cfg.Addr = fmt.Sprintf("%s:%s", *test_util.MysqlHost, *test_util.MysqlPort)
58+
if s.addr != "" {
59+
cfg.Addr = s.addr
60+
}
4061
cfg.User = "root"
4162
cfg.HeartbeatPeriod = 200 * time.Millisecond
4263
cfg.ReadTimeout = 300 * time.Millisecond

cmd/go-canal/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/go-mysql-org/go-mysql/canal"
1515
"github.com/go-mysql-org/go-mysql/mysql"
16+
"github.com/pingcap/errors"
1617
)
1718

1819
var (
@@ -41,6 +42,12 @@ var (
4142
func main() {
4243
flag.Parse()
4344

45+
err := mysql.ValidateFlavor(*flavor)
46+
if err != nil {
47+
fmt.Printf("Flavor error: %v\n", errors.ErrorStack(err))
48+
return
49+
}
50+
4451
cfg := canal.NewDefaultConfig()
4552
cfg.Addr = net.JoinHostPort(*host, strconv.Itoa(*port))
4653
cfg.User = *user

cmd/go-mysqlbinlog/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ func main() {
5050
MaxReconnectAttempts: 10,
5151
}
5252

53+
err := mysql.ValidateFlavor(*flavor)
54+
if err != nil {
55+
fmt.Printf("Flavor error: %v\n", errors.ErrorStack(err))
56+
return
57+
}
58+
5359
b := replication.NewBinlogSyncer(cfg)
5460

5561
pos := mysql.Position{Name: *file, Pos: uint32(*pos)}

cmd/go-mysqldump/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var (
1515
addr = flag.String("addr", "127.0.0.1:3306", "MySQL addr")
1616
user = flag.String("user", "root", "MySQL user")
1717
password = flag.String("password", "", "MySQL password")
18-
execution = flag.String("exec", "mysqldump", "mysqldump execution path")
18+
execution = flag.String("exec", "", "mysqldump/mariadb-dump execution path")
1919
output = flag.String("o", "", "dump output, empty for stdout")
2020

2121
dbs = flag.String("dbs", "", "dump databases, separated by comma")
@@ -30,7 +30,7 @@ func main() {
3030

3131
d, err := dump.NewDumper(*execution, *addr, *user, *password)
3232
if err != nil {
33-
fmt.Printf("Create Dumper error %v\n", errors.ErrorStack(err))
33+
fmt.Printf("Create Dumper error: %v\n", errors.ErrorStack(err))
3434
os.Exit(1)
3535
}
3636

dump/dumper.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7+
"net"
78
"os"
89
"os/exec"
910
"regexp"
@@ -51,13 +52,23 @@ type Dumper struct {
5152
}
5253

5354
func NewDumper(executionPath string, addr string, user string, password string) (*Dumper, error) {
54-
if len(executionPath) == 0 {
55-
return nil, nil
56-
}
55+
var path string
56+
var err error
5757

58-
path, err := exec.LookPath(executionPath)
59-
if err != nil {
60-
return nil, errors.Trace(err)
58+
if len(executionPath) == 0 { // No explicit path set
59+
path, err = exec.LookPath("mysqldump")
60+
if err != nil {
61+
path, err = exec.LookPath("mariadb-dump")
62+
if err != nil {
63+
// Using a new error as `err` will only mention mariadb-dump and not mysqldump
64+
return nil, errors.New("not able to find mysqldump or mariadb-dump in path")
65+
}
66+
}
67+
} else {
68+
path, err = exec.LookPath(executionPath)
69+
if err != nil {
70+
return nil, err
71+
}
6172
}
6273

6374
d := new(Dumper)
@@ -202,10 +213,14 @@ func (d *Dumper) Dump(w io.Writer) error {
202213
if strings.Contains(d.Addr, "/") {
203214
args = append(args, fmt.Sprintf("--socket=%s", d.Addr))
204215
} else {
205-
seps := strings.SplitN(d.Addr, ":", 2)
206-
args = append(args, fmt.Sprintf("--host=%s", seps[0]))
207-
if len(seps) > 1 {
208-
args = append(args, fmt.Sprintf("--port=%s", seps[1]))
216+
host, port, err := net.SplitHostPort(d.Addr)
217+
if err != nil {
218+
host = d.Addr
219+
}
220+
221+
args = append(args, fmt.Sprintf("--host=%s", host))
222+
if port != "" {
223+
args = append(args, fmt.Sprintf("--port=%s", port))
209224
}
210225
}
211226

failover/const.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

failover/doc.go

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)