From e9d84fc65f99f7358e4308c6517ea03e98a32417 Mon Sep 17 00:00:00 2001 From: ssongliu Date: Thu, 4 Dec 2025 14:28:08 +0800 Subject: [PATCH] fix: Fix the issue of remote database connection failure --- agent/app/dto/database.go | 16 +++++---- agent/app/service/database.go | 43 +++++++++++++----------- agent/app/service/database_mysql.go | 2 +- agent/app/service/database_postgresql.go | 6 +--- 4 files changed, 35 insertions(+), 32 deletions(-) diff --git a/agent/app/dto/database.go b/agent/app/dto/database.go index 66faf0e29..fb6bf3769 100644 --- a/agent/app/dto/database.go +++ b/agent/app/dto/database.go @@ -258,6 +258,7 @@ type DatabaseInfo struct { Version string `json:"version"` Address string `json:"address"` Port uint `json:"port"` + InitialDB string `json:"initialDB"` Username string `json:"username"` Password string `json:"password"` @@ -309,13 +310,14 @@ type DatabaseCreate struct { } type DatabaseUpdate struct { - ID uint `json:"id"` - Type string `json:"type" validate:"required"` - Version string `json:"version" validate:"required"` - Address string `json:"address"` - Port uint `json:"port"` - Username string `json:"username" validate:"required"` - Password string `json:"password"` + ID uint `json:"id"` + Type string `json:"type" validate:"required"` + Version string `json:"version" validate:"required"` + Address string `json:"address"` + Port uint `json:"port"` + InitialDB string `json:"initialDB"` + Username string `json:"username" validate:"required"` + Password string `json:"password"` SSL bool `json:"ssl"` RootCert string `json:"rootCert"` diff --git a/agent/app/service/database.go b/agent/app/service/database.go index 24c0ecd9e..476df7d72 100644 --- a/agent/app/service/database.go +++ b/agent/app/service/database.go @@ -119,9 +119,10 @@ func (u *DatabaseService) CheckDatabase(req dto.DatabaseCreate) bool { if req.Timeout == 0 { req.Timeout = 30 } + var err error switch req.Type { case constant.AppPostgresql: - _, err := postgresql.NewPostgresqlClient(pgclient.DBInfo{ + _, err = postgresql.NewPostgresqlClient(pgclient.DBInfo{ From: "remote", Address: req.Address, Port: req.Port, @@ -130,17 +131,15 @@ func (u *DatabaseService) CheckDatabase(req dto.DatabaseCreate) bool { Password: req.Password, Timeout: req.Timeout, }) - return err == nil case constant.AppRedis: - _, err := redisclient.NewRedisClient(redisclient.DBInfo{ + _, err = redisclient.NewRedisClient(redisclient.DBInfo{ Address: req.Address, Port: req.Port, Password: req.Password, Timeout: req.Timeout, }) - return err == nil case "mysql", "mariadb": - _, err := mysql.NewMysqlClient(client.DBInfo{ + _, err = mysql.NewMysqlClient(client.DBInfo{ From: "remote", Address: req.Address, Port: req.Port, @@ -154,10 +153,13 @@ func (u *DatabaseService) CheckDatabase(req dto.DatabaseCreate) bool { SkipVerify: req.SkipVerify, Timeout: req.Timeout, }) - return err == nil + } + if err != nil { + global.LOG.Errorf("check database connection failed, err: %v", err) + return false } - return false + return true } func (u *DatabaseService) Create(req dto.DatabaseCreate) error { @@ -174,12 +176,13 @@ func (u *DatabaseService) Create(req dto.DatabaseCreate) error { switch req.Type { case constant.AppPostgresql: if _, err := postgresql.NewPostgresqlClient(pgclient.DBInfo{ - From: "remote", - Address: req.Address, - Port: req.Port, - Username: req.Username, - Password: req.Password, - Timeout: req.Timeout, + From: "remote", + Address: req.Address, + Port: req.Port, + InitialDB: req.InitialDB, + Username: req.Username, + Password: req.Password, + Timeout: req.Timeout, }); err != nil { return err } @@ -275,12 +278,13 @@ func (u *DatabaseService) Update(req dto.DatabaseUpdate) error { switch req.Type { case constant.AppPostgresql: if _, err := postgresql.NewPostgresqlClient(pgclient.DBInfo{ - From: "remote", - Address: req.Address, - Port: req.Port, - Username: req.Username, - Password: req.Password, - Timeout: req.Timeout, + From: "remote", + Address: req.Address, + Port: req.Port, + InitialDB: req.InitialDB, + Username: req.Username, + Password: req.Password, + Timeout: req.Timeout, }); err != nil { return err } @@ -326,6 +330,7 @@ func (u *DatabaseService) Update(req dto.DatabaseUpdate) error { upMap["port"] = req.Port upMap["username"] = req.Username upMap["password"] = pass + upMap["initial_db"] = req.InitialDB upMap["timeout"] = req.Timeout upMap["description"] = req.Description upMap["ssl"] = req.SSL diff --git a/agent/app/service/database_mysql.go b/agent/app/service/database_mysql.go index b60ae442f..5790a71bb 100644 --- a/agent/app/service/database_mysql.go +++ b/agent/app/service/database_mysql.go @@ -115,7 +115,7 @@ func (u *MysqlService) Create(ctx context.Context, req dto.MysqlDBCreate) (*mode } if req.From == "local" && req.Username == "root" { - return nil, errors.New("Cannot set root as user name") + return nil, errors.New("cannot set root as user name") } cli, version, err := LoadMysqlClientByFrom(req.Database) diff --git a/agent/app/service/database_postgresql.go b/agent/app/service/database_postgresql.go index 86e6922f8..9bf6eca8c 100644 --- a/agent/app/service/database_postgresql.go +++ b/agent/app/service/database_postgresql.go @@ -21,7 +21,6 @@ import ( "github.com/1Panel-dev/1Panel/agent/utils/postgresql/client" _ "github.com/jackc/pgx/v5/stdlib" "github.com/jinzhu/copier" - "github.com/pkg/errors" ) type PostgresqlService struct{} @@ -138,10 +137,6 @@ func (u *PostgresqlService) Create(ctx context.Context, req dto.PostgresqlDBCrea return nil, buserr.WithDetail("ErrStructTransform", err.Error(), nil) } - if req.From == "local" && req.Username == "root" { - return nil, errors.New("Cannot set root as user name") - } - cli, err := LoadPostgresqlClientByFrom(req.Database) if err != nil { return nil, err @@ -185,6 +180,7 @@ func LoadPostgresqlClientByFrom(database string) (postgresql.PostgresqlClient, e dbInfo.Port = databaseItem.Port dbInfo.Username = databaseItem.Username dbInfo.Password = databaseItem.Password + dbInfo.InitialDB = databaseItem.InitialDB } else { app, err := appInstallRepo.LoadBaseInfo(databaseItem.Type, database) if err != nil {