{"record":{"id":"9913ef73c15668af","repo":"flipped-aurora/gin-vue-admin","slug":"panic-err-mssql-database-connection-failed","errorCode":null,"errorMessage":"panic(err) — MSSQL database connection failed","messagePattern":"panic\\(err\\) — MSSQL database connection failed","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"server/initialize/gorm_mssql.go","lineNumber":60,"sourceCode":"\t\tsqlDB.SetMaxOpenConns(m.MaxOpenConns)\n\t\tsqlDB.SetConnMaxLifetime(time.Duration(m.ConnMaxLifetime) * time.Second)\n\t\treturn db\n\t}\n}\n\n// GormMssqlByConfig 初始化Mysql数据库用过传入配置\nfunc GormMssqlByConfig(m config.Mssql) *gorm.DB {\n\tif m.Dbname == \"\" {\n\t\treturn nil\n\t}\n\tmssqlConfig := sqlserver.Config{\n\t\tDSN:               m.Dsn(), // DSN data source name\n\t\tDefaultStringSize: 191,     // string 类型字段的默认长度\n\t}\n\t// 数据库配置\n\tgeneral := m.GeneralDB\n\tif db, err := gorm.Open(sqlserver.New(mssqlConfig), internal.Gorm.Config(general)); err != nil {\n\t\tpanic(err)\n\t} else {\n\t\tdb.InstanceSet(\"gorm:table_options\", \"ENGINE=InnoDB\")\n\t\tsqlDB, _ := db.DB()\n\t\tsqlDB.SetMaxIdleConns(m.MaxIdleConns)\n\t\tsqlDB.SetMaxOpenConns(m.MaxOpenConns)\n\t\tsqlDB.SetConnMaxLifetime(time.Duration(m.ConnMaxLifetime) * time.Second)\n\t\treturn db\n\t}\n}\n","sourceCodeStart":42,"sourceCodeEnd":70,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/initialize/gorm_mssql.go#L42-L70","documentation":"GormMssqlByConfig opens a GORM connection to a SQL Server instance using the go-gorm/sqlserver driver; if gorm.Open returns an error (unreachable host, bad DSN, auth failure, driver problem) the function panics with the raw error. It is invoked while building the DBList, so this failure aborts the whole application bootstrap.","triggerScenarios":"Called from initialize DBList for an 'mssql' db-list entry when: wrong host/port in config, SQL Server not reachable from the network/container, invalid credentials in the DSN, TLS negotiation failure, or the sqlserver driver can't parse m.Dsn().","commonSituations":"SQL Server container not started or still initializing (it takes long on first boot); firewall/security-group blocking 1433; SQL Server configured for Windows-only auth while DSN uses SQL auth; encrypt=true TLS mismatch with self-signed certs on newer drivers.","solutions":["Verify connectivity: test the host:port (nc -vz host 1433) and check the container/service is fully started","Validate DSN fields in config (host, port, user, password, database, encrypt/disable) against the sqlserver driver's DSN format","Test credentials with sqlcmd or another client using the same user/password","Fix the error then restart; ensure the db name in db-list matches the intended datasource"],"exampleFix":"// before (config.yaml)\ndbname: mssql\n  host: 127.0.0.1\n  port: 1433\n  config: encrypt=true\n// after\ndbname: mssql\n  host: 192.168.1.10\n  port: 1433\n  config: encrypt=disable","handlingStrategy":"retry","validationCode":"func mssqlReachable(host string, port int) error { conn, err := net.DialTimeout(\"tcp\", fmt.Sprintf(\"%s:%d\", host, port), 3*time.Second); if err != nil { return err }; return conn.Close() }","typeGuard":null,"tryCatchPattern":"// wrap bootstrap with recover so a failed datasource yields a clean message\ndefer func() { if r := recover(); r != nil { log.Fatalf(\"dblist init failed: %v\", r) } }()\n_ := initialize.DBList()","preventionTips":["Add readiness probes for SQL Server before app startup (docker healthcheck / depends_on condition)","Validate DSN credentials with sqlcmd during deployment","Keep the sqlserver driver version compatible with your SQL Server TLS settings (encrypt parameter)","CI smoke test: attempt gorm.Open against a throwaway SQL Server instance before release"],"tags":["gorm","mssql","panic","database-connection"],"backgroundTag":"database-connection-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}