flipped-aurora/gin-vue-admin · critical
panic(err) — MSSQL database connection failed
Error message
panic(err) — MSSQL database connection failed
What it means
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.
Source
Thrown at server/initialize/gorm_mssql.go:60
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
sqlDB.SetConnMaxLifetime(time.Duration(m.ConnMaxLifetime) * time.Second)
return db
}
}
// GormMssqlByConfig 初始化Mysql数据库用过传入配置
func GormMssqlByConfig(m config.Mssql) *gorm.DB {
if m.Dbname == "" {
return nil
}
mssqlConfig := sqlserver.Config{
DSN: m.Dsn(), // DSN data source name
DefaultStringSize: 191, // string 类型字段的默认长度
}
// 数据库配置
general := m.GeneralDB
if db, err := gorm.Open(sqlserver.New(mssqlConfig), internal.Gorm.Config(general)); err != nil {
panic(err)
} else {
db.InstanceSet("gorm:table_options", "ENGINE=InnoDB")
sqlDB, _ := db.DB()
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
sqlDB.SetConnMaxLifetime(time.Duration(m.ConnMaxLifetime) * time.Second)
return db
}
}
View on GitHub (pinned to 3136500ef3)
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
Example fix
// before (config.yaml) dbname: mssql host: 127.0.0.1 port: 1433 config: encrypt=true // after dbname: mssql host: 192.168.1.10 port: 1433 config: encrypt=disable
Defensive patterns
Strategy: retry
Validate before calling
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() } Try / catch
// wrap bootstrap with recover so a failed datasource yields a clean message
defer func() { if r := recover(); r != nil { log.Fatalf("dblist init failed: %v", r) } }()
_ := initialize.DBList() Prevention
- 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
When it happens
Trigger: 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().
Common situations: 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.
Related errors
- db no init
- panic(err) — MySQL database connection failed
- panic(err) — Oracle database connection failed
- 存在相同api
- 存在相同api路径
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/9913ef73c15668af.
Report an issue: GitHub.