Skip to content

Commit 2818d7e

Browse files
committed
This fixes issue #34. Using the wrong hostname.
When registering as a slave we are using the wrong hostname. It should be our hostname, not the hostname name of the server we are connecting to. Fixed issue with using the wrong variable.
1 parent e17f653 commit 2818d7e

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

replication/binlogsyncer.go

+23-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package replication
33
import (
44
"encoding/binary"
55
"fmt"
6+
"os"
67
"sync"
78
"time"
89

@@ -25,10 +26,12 @@ type BinlogSyncer struct {
2526
c *client.Conn
2627
serverID uint32
2728

28-
host string
29-
port uint16
30-
user string
31-
password string
29+
// LocalHost is the name of you want to present to the MySQL master. If it is not set it will default to os.Hostname()
30+
LocalHost string
31+
host string
32+
port uint16
33+
user string
34+
password string
3235

3336
masterID uint32
3437

@@ -62,6 +65,15 @@ func NewBinlogSyncer(serverID uint32, flavor string) *BinlogSyncer {
6265
return b
6366
}
6467

68+
func (b *BinlogSyncer) LocalHostname() string {
69+
70+
if b.LocalHost == "" {
71+
h, _ := os.Hostname()
72+
return h
73+
}
74+
return b.LocalHost
75+
}
76+
6577
func (b *BinlogSyncer) Close() {
6678
b.m.Lock()
6779
defer b.m.Unlock()
@@ -222,11 +234,9 @@ func (b *BinlogSyncer) EnableSemiSync() error {
222234
}
223235

224236
_, err := b.c.Execute(`SET @rpl_semi_sync_slave = 1;`)
225-
226237
if err != nil {
227238
b.semiSyncEnabled = true
228239
}
229-
230240
return errors.Trace(err)
231241
}
232242

@@ -402,7 +412,10 @@ func (b *BinlogSyncer) writeBinlogDumpMariadbGTIDCommand(gset GTIDSet) error {
402412
func (b *BinlogSyncer) writeRegisterSlaveCommand() error {
403413
b.c.ResetSequence()
404414

405-
data := make([]byte, 4+1+4+1+len(b.host)+1+len(b.user)+1+len(b.password)+2+4+4)
415+
hostname := b.LocalHostname()
416+
417+
// This should be the name of slave host not the host we are connecting to.
418+
data := make([]byte, 4+1+4+1+len(hostname)+1+len(b.user)+1+len(b.password)+2+4+4)
406419
pos := 4
407420

408421
data[pos] = COM_REGISTER_SLAVE
@@ -411,9 +424,10 @@ func (b *BinlogSyncer) writeRegisterSlaveCommand() error {
411424
binary.LittleEndian.PutUint32(data[pos:], b.serverID)
412425
pos += 4
413426

414-
data[pos] = uint8(len(b.host))
427+
// This should be the name of slave hostname not the host we are connecting to.
428+
data[pos] = uint8(len(hostname))
415429
pos++
416-
n := copy(data[pos:], b.host)
430+
n := copy(data[pos:], hostname)
417431
pos += n
418432

419433
data[pos] = uint8(len(b.user))

0 commit comments

Comments
 (0)