From 8cfb604a0d43e8b4891b71c274798c3ffd8a4cc7 Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Mon, 29 Aug 2022 22:31:14 +0800 Subject: [PATCH] set mysql connection --- config/config.yaml | 4 ++-- internal/api/user/user.go | 2 +- pkg/common/db/mysql.go | 9 +++++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index 5fbfe7492..d59b4a2a8 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -17,9 +17,9 @@ mysql: dbMysqlDatabaseName: openIM_v2 #默认即可 dbTableName: eMsg #默认即可 dbMsgTableNum: 1 - dbMaxOpenConns: 200 + dbMaxOpenConns: 2000 dbMaxIdleConns: 100 - dbMaxLifeTime: 120 + dbMaxLifeTime: 3600 mongo: dbUri: ""#当dbUri值不为空则直接使用该值 diff --git a/internal/api/user/user.go b/internal/api/user/user.go index bef11c3a9..ffaa5645f 100644 --- a/internal/api/user/user.go +++ b/internal/api/user/user.go @@ -368,7 +368,7 @@ func GetSelfUserInfo(c *gin.Context) { c.JSON(http.StatusOK, resp) } else { resp := api.GetSelfUserInfoResp{CommResp: api.CommResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}} - log.NewInfo(req.OperationID, "GetUserInfo api return ", resp) + log.NewError(req.OperationID, "GetUserInfo api return ", resp) c.JSON(http.StatusOK, resp) } } diff --git a/pkg/common/db/mysql.go b/pkg/common/db/mysql.go index f28be8dc1..f0829a5ee 100644 --- a/pkg/common/db/mysql.go +++ b/pkg/common/db/mysql.go @@ -69,6 +69,15 @@ func initMysqlDB() { panic(err.Error()) } + sqlDB, err := db.DB() + if err != nil { + panic(err.Error()) + } + + sqlDB.SetConnMaxLifetime(time.Second * time.Duration(config.Config.Mysql.DBMaxLifeTime)) + sqlDB.SetMaxOpenConns(config.Config.Mysql.DBMaxOpenConns) + sqlDB.SetMaxIdleConns(config.Config.Mysql.DBMaxIdleConns) + fmt.Println("open db ok ", dsn) db.AutoMigrate( &Register{},