From 69742b935ab5a9187474b83d445cca50b87ff150 Mon Sep 17 00:00:00 2001 From: Granty1 Date: Sat, 25 Apr 2020 20:54:49 +0800 Subject: [PATCH] Handle nil pointer dereference panic after db connect fail --- server/initialize/mysql.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/server/initialize/mysql.go b/server/initialize/mysql.go index 9b4d71cb..ee61ff9a 100644 --- a/server/initialize/mysql.go +++ b/server/initialize/mysql.go @@ -4,13 +4,15 @@ import ( "gin-vue-admin/global" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/mysql" + "os" ) //初始化数据库并产生数据库全局变量 func Mysql() { admin := global.GVA_CONFIG.Mysql if db, err := gorm.Open("mysql", admin.Username+":"+admin.Password+"@("+admin.Path+")/"+admin.Dbname+"?"+admin.Config); err != nil { - global.GVA_LOG.Error("DEFAULTDB数据库启动异常", err) + global.GVA_LOG.Error("MySQL启动异常", err) + os.Exit(0) } else { global.GVA_DB = db global.GVA_DB.DB().SetMaxIdleConns(admin.MaxIdleConns)