Skip to content

Commit 339da78

Browse files
committed
Merge pull request #36 from gdey/slave_hostname
This fixes issue #34. Using the wrong hostname.
2 parents e17f653 + fbc3c98 commit 339da78

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

replication/binlogsyncer.go

+28-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,11 @@ 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 string
30+
host string
31+
port uint16
32+
user string
33+
password string
3234

3335
masterID uint32
3436

@@ -62,6 +64,21 @@ func NewBinlogSyncer(serverID uint32, flavor string) *BinlogSyncer {
6264
return b
6365
}
6466

67+
// LocalHostname returns the hostname that register slave would register as.
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+
77+
// SetLocalHostname set's the hostname that register salve would register as.
78+
func (b *NewBinlogSyncer) SetLocalHostname(name string) {
79+
b.localhost = name
80+
}
81+
6582
func (b *BinlogSyncer) Close() {
6683
b.m.Lock()
6784
defer b.m.Unlock()
@@ -222,11 +239,9 @@ func (b *BinlogSyncer) EnableSemiSync() error {
222239
}
223240

224241
_, err := b.c.Execute(`SET @rpl_semi_sync_slave = 1;`)
225-
226242
if err != nil {
227243
b.semiSyncEnabled = true
228244
}
229-
230245
return errors.Trace(err)
231246
}
232247

@@ -402,7 +417,10 @@ func (b *BinlogSyncer) writeBinlogDumpMariadbGTIDCommand(gset GTIDSet) error {
402417
func (b *BinlogSyncer) writeRegisterSlaveCommand() error {
403418
b.c.ResetSequence()
404419

405-
data := make([]byte, 4+1+4+1+len(b.host)+1+len(b.user)+1+len(b.password)+2+4+4)
420+
hostname := b.LocalHostname()
421+
422+
// This should be the name of slave host not the host we are connecting to.
423+
data := make([]byte, 4+1+4+1+len(hostname)+1+len(b.user)+1+len(b.password)+2+4+4)
406424
pos := 4
407425

408426
data[pos] = COM_REGISTER_SLAVE
@@ -411,9 +429,10 @@ func (b *BinlogSyncer) writeRegisterSlaveCommand() error {
411429
binary.LittleEndian.PutUint32(data[pos:], b.serverID)
412430
pos += 4
413431

414-
data[pos] = uint8(len(b.host))
432+
// This should be the name of slave hostname not the host we are connecting to.
433+
data[pos] = uint8(len(hostname))
415434
pos++
416-
n := copy(data[pos:], b.host)
435+
n := copy(data[pos:], hostname)
417436
pos += n
418437

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

0 commit comments

Comments
 (0)