From 9e2b9eb7c0c516a3595b7fb83495fe8343145bf8 Mon Sep 17 00:00:00 2001 From: xfwduke Date: Mon, 11 Mar 2024 10:04:44 +0800 Subject: [PATCH] =?UTF-8?q?fix(mysql):=20mysql-monitor=20db-up=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E9=87=8D=E8=BF=9E=20#3506?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../connection_collect.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/monitoriteminterface/connection_collect.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/monitoriteminterface/connection_collect.go index bba25710c5..1618fc63d4 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/monitoriteminterface/connection_collect.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/monitoriteminterface/connection_collect.go @@ -159,7 +159,7 @@ func connectDB(ip string, port int, ca *config.ConnectAuth) (*sqlx.DB, error) { ctx, cancel := context.WithTimeout(context.Background(), config.MonitorConfig.InteractTimeout) defer cancel() - return sqlx.ConnectContext( + db, err := sqlx.ConnectContext( ctx, "mysql", fmt.Sprintf( "%s:%s@tcp(%s:%d)/%s?parseTime=true&loc=%s&timeout=%s", @@ -169,4 +169,21 @@ func connectDB(ip string, port int, ca *config.ConnectAuth) (*sqlx.DB, error) { config.MonitorConfig.InteractTimeout, ), ) + if err != nil { + slog.Warn("first time connect failed", slog.String("error", err.Error())) + slog.Info("retry connect after 3 seconds") + time.Sleep(3 * time.Second) + return sqlx.ConnectContext( + ctx, + "mysql", fmt.Sprintf( + "%s:%s@tcp(%s:%d)/%s?parseTime=true&loc=%s&timeout=%s", + ca.User, ca.Password, ip, port, + "", + time.Local.String(), + config.MonitorConfig.InteractTimeout, + ), + ) + } + + return db, nil }