Skip to content

Commit

Permalink
Merge branch 'master' of github.com:go-mysql-org/go-mysql into prepar…
Browse files Browse the repository at this point in the history
…e-1.10.0
  • Loading branch information
lance6716 committed Nov 20, 2024
2 parents 09b720d + 8b76415 commit ba6d354
Show file tree
Hide file tree
Showing 23 changed files with 159 additions and 808 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ jobs:
echo -n "mysqldump -V: " ; mysqldump -V
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
# bind to :: for dual-stack listening
sudo sed -i 's/bind-address.*= 127.0.0.1/bind-address = ::/' /etc/mysql/mysql.conf.d/mysqld.cnf
sudo sed -i 's/mysqlx-bind-address.*= 127.0.0.1/mysqlx-bind-address = ::/' /etc/mysql/mysql.conf.d/mysqld.cnf
sudo service mysql start
# apply this for mysql5 & mysql8 compatibility
Expand Down Expand Up @@ -109,5 +114,6 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: "1.22"

- name: Build on ${{ matrix.os }}/${{ matrix.arch }}
run: GOARCH=${{ matrix.arch }} GOOS=${{ matrix.os }} go build ./...
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ This repo uses [Changelog](CHANGELOG.md).
* [Incremental dumping](#canal)
* [Client](#client)
* [Fake server](#server)
* [Failover](#failover)
* [database/sql like driver](#driver)

## Replication
Expand Down Expand Up @@ -326,18 +325,6 @@ MySQL [(none)]>
>
> To customize server configurations, use ```NewServer()``` and create connection via ```NewCustomizedConn()```.

## Failover

Failover supports to promote a new master and let replicas replicate from it automatically when the old master was down.

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).

At the same time, Failover supports MariaDB >= 10.0.9 with GTID mode too.

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.
Although there are many companies use MySQL 5.0 - 5.5, I think upgrade MySQL to 5.6 or higher is easy.

## Driver

Driver is the package that you can use go-mysql with go database/sql like other drivers. A simple example:
Expand Down
13 changes: 6 additions & 7 deletions canal/canal.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,18 +481,17 @@ func (c *Canal) prepareSyncer() error {
if strings.Contains(c.cfg.Addr, "/") {
cfg.Host = c.cfg.Addr
} else {
seps := strings.Split(c.cfg.Addr, ":")
if len(seps) != 2 {
return errors.Errorf("invalid mysql addr format %s, must host:port", c.cfg.Addr)
host, port, err := net.SplitHostPort(c.cfg.Addr)
if err != nil {
return errors.Errorf("invalid MySQL address format %s, must host:port", c.cfg.Addr)
}

port, err := strconv.ParseUint(seps[1], 10, 16)
portNumber, err := strconv.ParseUint(port, 10, 16)
if err != nil {
return errors.Trace(err)
}

cfg.Host = seps[0]
cfg.Port = uint16(port)
cfg.Host = host
cfg.Port = uint16(portNumber)
}

c.syncer = replication.NewBinlogSyncer(cfg)
Expand Down
23 changes: 22 additions & 1 deletion canal/canal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,30 @@ import (
)

type canalTestSuite struct {
addr string
suite.Suite
c *Canal
}

type canalTestSuiteOption func(c *canalTestSuite)

func withAddr(addr string) canalTestSuiteOption {
return func(c *canalTestSuite) {
c.addr = addr
}
}

func newCanalTestSuite(opts ...canalTestSuiteOption) *canalTestSuite {
c := new(canalTestSuite)
for _, opt := range opts {
opt(c)
}
return c
}

func TestCanalSuite(t *testing.T) {
suite.Run(t, new(canalTestSuite))
suite.Run(t, newCanalTestSuite())
suite.Run(t, newCanalTestSuite(withAddr(mysql.DEFAULT_IPV6_ADDR)))
}

const (
Expand All @@ -37,6 +55,9 @@ const (
func (s *canalTestSuite) SetupSuite() {
cfg := NewDefaultConfig()
cfg.Addr = fmt.Sprintf("%s:%s", *test_util.MysqlHost, *test_util.MysqlPort)
if s.addr != "" {
cfg.Addr = s.addr
}
cfg.User = "root"
cfg.HeartbeatPeriod = 200 * time.Millisecond
cfg.ReadTimeout = 300 * time.Millisecond
Expand Down
7 changes: 7 additions & 0 deletions cmd/go-canal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/go-mysql-org/go-mysql/canal"
"github.com/go-mysql-org/go-mysql/mysql"
"github.com/pingcap/errors"
)

var (
Expand Down Expand Up @@ -41,6 +42,12 @@ var (
func main() {
flag.Parse()

err := mysql.ValidateFlavor(*flavor)
if err != nil {
fmt.Printf("Flavor error: %v\n", errors.ErrorStack(err))
return
}

cfg := canal.NewDefaultConfig()
cfg.Addr = net.JoinHostPort(*host, strconv.Itoa(*port))
cfg.User = *user
Expand Down
6 changes: 6 additions & 0 deletions cmd/go-mysqlbinlog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ func main() {
MaxReconnectAttempts: 10,
}

err := mysql.ValidateFlavor(*flavor)
if err != nil {
fmt.Printf("Flavor error: %v\n", errors.ErrorStack(err))
return
}

b := replication.NewBinlogSyncer(cfg)

pos := mysql.Position{Name: *file, Pos: uint32(*pos)}
Expand Down
4 changes: 2 additions & 2 deletions cmd/go-mysqldump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var (
addr = flag.String("addr", "127.0.0.1:3306", "MySQL addr")
user = flag.String("user", "root", "MySQL user")
password = flag.String("password", "", "MySQL password")
execution = flag.String("exec", "mysqldump", "mysqldump execution path")
execution = flag.String("exec", "", "mysqldump/mariadb-dump execution path")
output = flag.String("o", "", "dump output, empty for stdout")

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

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

Expand Down
35 changes: 25 additions & 10 deletions dump/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io"
"net"
"os"
"os/exec"
"regexp"
Expand Down Expand Up @@ -51,13 +52,23 @@ type Dumper struct {
}

func NewDumper(executionPath string, addr string, user string, password string) (*Dumper, error) {
if len(executionPath) == 0 {
return nil, nil
}
var path string
var err error

path, err := exec.LookPath(executionPath)
if err != nil {
return nil, errors.Trace(err)
if len(executionPath) == 0 { // No explicit path set
path, err = exec.LookPath("mysqldump")
if err != nil {
path, err = exec.LookPath("mariadb-dump")
if err != nil {
// Using a new error as `err` will only mention mariadb-dump and not mysqldump
return nil, errors.New("not able to find mysqldump or mariadb-dump in path")
}
}
} else {
path, err = exec.LookPath(executionPath)
if err != nil {
return nil, err
}
}

d := new(Dumper)
Expand Down Expand Up @@ -202,10 +213,14 @@ func (d *Dumper) Dump(w io.Writer) error {
if strings.Contains(d.Addr, "/") {
args = append(args, fmt.Sprintf("--socket=%s", d.Addr))
} else {
seps := strings.SplitN(d.Addr, ":", 2)
args = append(args, fmt.Sprintf("--host=%s", seps[0]))
if len(seps) > 1 {
args = append(args, fmt.Sprintf("--port=%s", seps[1]))
host, port, err := net.SplitHostPort(d.Addr)
if err != nil {
host = d.Addr
}

args = append(args, fmt.Sprintf("--host=%s", host))
if port != "" {
args = append(args, fmt.Sprintf("--port=%s", port))
}
}

Expand Down
11 changes: 0 additions & 11 deletions failover/const.go

This file was deleted.

8 changes: 0 additions & 8 deletions failover/doc.go

This file was deleted.

67 changes: 0 additions & 67 deletions failover/failover.go

This file was deleted.

Loading

0 comments on commit ba6d354

Please sign in to comment.