@@ -3,6 +3,7 @@ package replication
3
3
import (
4
4
"encoding/binary"
5
5
"fmt"
6
+ "os"
6
7
"sync"
7
8
"time"
8
9
@@ -25,10 +26,11 @@ type BinlogSyncer struct {
25
26
c * client.Conn
26
27
serverID uint32
27
28
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
32
34
33
35
masterID uint32
34
36
@@ -62,6 +64,21 @@ func NewBinlogSyncer(serverID uint32, flavor string) *BinlogSyncer {
62
64
return b
63
65
}
64
66
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
+
65
82
func (b * BinlogSyncer ) Close () {
66
83
b .m .Lock ()
67
84
defer b .m .Unlock ()
@@ -222,11 +239,9 @@ func (b *BinlogSyncer) EnableSemiSync() error {
222
239
}
223
240
224
241
_ , err := b .c .Execute (`SET @rpl_semi_sync_slave = 1;` )
225
-
226
242
if err != nil {
227
243
b .semiSyncEnabled = true
228
244
}
229
-
230
245
return errors .Trace (err )
231
246
}
232
247
@@ -402,7 +417,10 @@ func (b *BinlogSyncer) writeBinlogDumpMariadbGTIDCommand(gset GTIDSet) error {
402
417
func (b * BinlogSyncer ) writeRegisterSlaveCommand () error {
403
418
b .c .ResetSequence ()
404
419
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 )
406
424
pos := 4
407
425
408
426
data [pos ] = COM_REGISTER_SLAVE
@@ -411,9 +429,10 @@ func (b *BinlogSyncer) writeRegisterSlaveCommand() error {
411
429
binary .LittleEndian .PutUint32 (data [pos :], b .serverID )
412
430
pos += 4
413
431
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 ))
415
434
pos ++
416
- n := copy (data [pos :], b . host )
435
+ n := copy (data [pos :], hostname )
417
436
pos += n
418
437
419
438
data [pos ] = uint8 (len (b .user ))
0 commit comments