Skip to content

Commit 7d97353

Browse files
author
Guillaume Lefranc
committed
Named connections support #16
1 parent 7e2cd93 commit 7d97353

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

monitor.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ func (server *ServerMonitor) refresh() error {
149149
server.SlaveGtid = sv["GTID_SLAVE_POS"]
150150
sid, _ := strconv.ParseUint(sv["SERVER_ID"], 10, 0)
151151
server.ServerID = uint(sid)
152+
err = dbhelper.SetDefaultMasterConn(server.Conn, masterConn)
153+
if err != nil {
154+
return err
155+
}
152156
slaveStatus, err := dbhelper.GetSlaveStatus(server.Conn)
153157
if err != nil {
154158
return err

provision.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os/exec"
77

88
"github.com/spf13/cobra"
9+
"github.com/tanji/mariadb-tools/dbhelper"
910
)
1011

1112
var (
@@ -21,6 +22,7 @@ func init() {
2122
provisionCmd.Flags().StringVar(&destination, "destination", "", "Source server")
2223
bootstrapCmd.Flags().BoolVar(&cleanall, "clean-all", false, "Reset all slaves and binary logs before bootstrapping")
2324
bootstrapCmd.Flags().StringVar(&prefMaster, "prefmaster", "", "Preferred server for master initialization")
25+
bootstrapCmd.Flags().StringVar(&masterConn, "master-connection", "", "Connection name to use for multisource replication")
2426
}
2527

2628
var bootstrapCmd = &cobra.Command{
@@ -36,9 +38,22 @@ var bootstrapCmd = &cobra.Command{
3638
log.Fatal(err)
3739
}
3840
for _, server := range servers {
39-
server.Conn.Exec("RESET MASTER")
40-
server.Conn.Exec("STOP SLAVE")
41-
server.Conn.Exec("RESET SLAVE ALL")
41+
err = dbhelper.SetDefaultMasterConn(server.Conn, masterConn)
42+
if err != nil {
43+
log.Fatal(err)
44+
}
45+
err = dbhelper.ResetMaster(server.Conn)
46+
if err != nil {
47+
log.Fatal(err)
48+
}
49+
err = dbhelper.StopAllSlaves(server.Conn)
50+
if err != nil {
51+
log.Fatal(err)
52+
}
53+
err = dbhelper.ResetAllSlaves(server.Conn)
54+
if err != nil {
55+
log.Fatal(err)
56+
}
4257
}
4358
} else {
4459
err := topologyInit()
@@ -70,9 +85,15 @@ var bootstrapCmd = &cobra.Command{
7085
if key == masterKey {
7186
continue
7287
} else {
73-
stmt := "CHANGE MASTER TO master_host='" + servers[masterKey].IP + "', master_port=" + servers[masterKey].Port + ", master_user='" + rplUser + "', master_password='" + rplPass + "', master_use_gtid=current_pos"
74-
server.Conn.Exec(stmt)
75-
server.Conn.Exec("START SLAVE")
88+
stmt := "CHANGE MASTER '" + masterConn + "' TO master_host='" + servers[masterKey].IP + "', master_port=" + servers[masterKey].Port + ", master_user='" + rplUser + "', master_password='" + rplPass + "', master_use_gtid=current_pos"
89+
_, err := server.Conn.Exec(stmt)
90+
if err != nil {
91+
log.Fatal(stmt, err)
92+
}
93+
_, err = server.Conn.Exec("START SLAVE '" + masterConn + "'")
94+
if err != nil {
95+
log.Fatal("Start slave: ", err)
96+
}
7697
}
7798
}
7899
log.Printf("INFO : Environment bootstrapped with %s as master", servers[masterKey].URL)

repmgr.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ var (
7373
faillimit int
7474
failtime int64
7575
checktype string
76+
masterConn string
7677
)
7778

7879
func init() {
@@ -108,6 +109,7 @@ func initRepmgrFlags(cmd *cobra.Command) {
108109
cmd.Flags().BoolVar(&readonly, "readonly", true, "Set slaves as read-only after switchover")
109110
cmd.Flags().StringVar(&logfile, "logfile", "", "Write MRM messages to a log file")
110111
cmd.Flags().IntVar(&timeout, "connect-timeout", 5, "Database connection timeout in seconds")
112+
cmd.Flags().StringVar(&masterConn, "master-connection", "", "Connection name to use for multisource replication")
111113
viper.BindPFlags(cmd.Flags())
112114
preScript = viper.GetString("pre-failover-script")
113115
postScript = viper.GetString("post-failover-script")
@@ -119,6 +121,7 @@ func initRepmgrFlags(cmd *cobra.Command) {
119121
readonly = viper.GetBool("readonly")
120122
logfile = viper.GetString("logfile")
121123
timeout = viper.GetInt("connect-timeout")
124+
masterConn = viper.GetString("master-connection")
122125
}
123126

124127
var failoverCmd = &cobra.Command{

topology.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ func newServerList() error {
4242
if verbose {
4343
log.Printf("DEBUG: Checking if server %s is slave", servers[k].URL)
4444
}
45-
4645
servers[k].refresh()
4746
if servers[k].UsingGtid != "" {
4847
if verbose {

0 commit comments

Comments
 (0)