From a1feceafdc31c94539c78256c3d7c1d5ce23470d Mon Sep 17 00:00:00 2001 From: D3Hunter Date: Tue, 5 Jul 2022 09:57:00 +0800 Subject: [PATCH] cherry pick #35881 to release-6.1 Signed-off-by: ti-srebot --- br/pkg/lightning/common/util.go | 7 +++++-- br/pkg/lightning/common/util_test.go | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/br/pkg/lightning/common/util.go b/br/pkg/lightning/common/util.go index 40b7160ed5e30..67a26fb3ab411 100644 --- a/br/pkg/lightning/common/util.go +++ b/br/pkg/lightning/common/util.go @@ -21,9 +21,11 @@ import ( "encoding/json" "fmt" "io" + "net" "net/http" "net/url" "os" + "strconv" "strings" "syscall" "time" @@ -57,8 +59,9 @@ type MySQLConnectParam struct { } func (param *MySQLConnectParam) ToDSN() string { - dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8mb4&sql_mode='%s'&maxAllowedPacket=%d&tls=%s", - param.User, param.Password, param.Host, param.Port, + hostPort := net.JoinHostPort(param.Host, strconv.Itoa(param.Port)) + dsn := fmt.Sprintf("%s:%s@tcp(%s)/?charset=utf8mb4&sql_mode='%s'&maxAllowedPacket=%d&tls=%s", + param.User, param.Password, hostPort, param.SQLMode, param.MaxAllowedPacket, param.TLS) for k, v := range param.Vars { diff --git a/br/pkg/lightning/common/util_test.go b/br/pkg/lightning/common/util_test.go index cb13a10db9d5d..c7c95b44f69bf 100644 --- a/br/pkg/lightning/common/util_test.go +++ b/br/pkg/lightning/common/util_test.go @@ -99,6 +99,9 @@ func TestToDSN(t *testing.T) { }, } require.Equal(t, "root:123456@tcp(127.0.0.1:4000)/?charset=utf8mb4&sql_mode='strict'&maxAllowedPacket=1234&tls=cluster&tidb_distsql_scan_concurrency='1'", param.ToDSN()) + + param.Host = "::1" + require.Equal(t, "root:123456@tcp([::1]:4000)/?charset=utf8mb4&sql_mode='strict'&maxAllowedPacket=1234&tls=cluster&tidb_distsql_scan_concurrency='1'", param.ToDSN()) } type mockDriver struct {