@@ -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,12 @@ 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 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
32
35
33
36
masterID uint32
34
37
@@ -62,6 +65,15 @@ func NewBinlogSyncer(serverID uint32, flavor string) *BinlogSyncer {
62
65
return b
63
66
}
64
67
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
+
65
77
func (b * BinlogSyncer ) Close () {
66
78
b .m .Lock ()
67
79
defer b .m .Unlock ()
@@ -222,11 +234,9 @@ func (b *BinlogSyncer) EnableSemiSync() error {
222
234
}
223
235
224
236
_ , err := b .c .Execute (`SET @rpl_semi_sync_slave = 1;` )
225
-
226
237
if err != nil {
227
238
b .semiSyncEnabled = true
228
239
}
229
-
230
240
return errors .Trace (err )
231
241
}
232
242
@@ -402,7 +412,10 @@ func (b *BinlogSyncer) writeBinlogDumpMariadbGTIDCommand(gset GTIDSet) error {
402
412
func (b * BinlogSyncer ) writeRegisterSlaveCommand () error {
403
413
b .c .ResetSequence ()
404
414
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 )
406
419
pos := 4
407
420
408
421
data [pos ] = COM_REGISTER_SLAVE
@@ -411,9 +424,10 @@ func (b *BinlogSyncer) writeRegisterSlaveCommand() error {
411
424
binary .LittleEndian .PutUint32 (data [pos :], b .serverID )
412
425
pos += 4
413
426
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 ))
415
429
pos ++
416
- n := copy (data [pos :], b . host )
430
+ n := copy (data [pos :], hostname )
417
431
pos += n
418
432
419
433
data [pos ] = uint8 (len (b .user ))
0 commit comments