flipped-aurora/gin-vue-admin · critical

panic(err) — MySQL database connection failed

Error message

panic(err) — MySQL database connection failed

What it means

initMysqlDatabase calls gorm.Open to establish the MySQL connection using the configured DSN; if gorm.Open returns an error (driver missing, bad DSN, unreachable server, auth failure), the initializer calls panic(err), crashing the server at startup. This is an intentional fail-fast: the app cannot serve without its primary database. The panic aborts GormMysql/GormMysqlByConfig and therefore the whole GVA initialization.

Source

Thrown at server/initialize/gorm_mysql.go:42

func GormMysqlByConfig(m config.Mysql) *gorm.DB {
	return initMysqlDatabase(m)
}

// initMysqlDatabase 初始化Mysql数据库的辅助函数
func initMysqlDatabase(m config.Mysql) *gorm.DB {
	if m.Dbname == "" {
		return nil
	}

	mysqlConfig := mysql.Config{
		DSN:                       m.Dsn(), // DSN data source name
		DefaultStringSize:         191,     // string 类型字段的默认长度
		SkipInitializeWithVersion: false,   // 根据版本自动配置
	}
	// 数据库配置
	general := m.GeneralDB
	if db, err := gorm.Open(mysql.New(mysqlConfig), internal.Gorm.Config(general)); err != nil {
		panic(err)
	} else {
		db.InstanceSet("gorm:table_options", "ENGINE="+m.Engine)
		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

  1. Verify MySQL is reachable from the server host: mysql -h <host> -P <port> -u <user> -p and check docker/k8s service ordering/healthchecks.
  2. Check the db-mysql block in config.yaml: path, port, config (charset/parseTime/loc), username, password, dbname; fix typos and special characters in the password.
  3. Confirm the mysql driver is registered (the gorm.io/driver/mysql blank import in gorm_mysql.go) and go.mod is in sync (go mod tidy).
  4. If the DB may start after the app, add retry/backoff around gorm.Open or a readiness gate instead of instant panic.
  5. Inspect the wrapped err text — MySQL driver errors (1045 access denied, 2003 can't connect) identify auth vs network directly.

Example fix

// before
if db, err := gorm.Open(mysql.New(mysqlConfig), internal.Gorm.Config(general)); err != nil {
    panic(err)
}
// after
if db, err := gorm.Open(mysql.New(mysqlConfig), internal.Gorm.Config(general)); err != nil {
    // log the underlying driver reason before failing fast
    global.GVA_LOG.Error("mysql connect failed", zap.Error(err))
    panic(fmt.Sprintf("mysql connect failed: %v", err))
}
Defensive patterns

Strategy: validation

Validate before calling

// before starting the app, verify connectivity
net.DialTimeout("tcp", fmt.Sprintf("%s:%s", cfg.Path, cfg.Port), 3*time.Second)
// and preflight the DSN:
db, err := sql.Open("mysql", dsn); if err == nil { err = db.Ping() }; db.Close()

Try / catch

// fail fast with context instead of bare panic
if db, err := gorm.Open(mysql.New(mysqlConfig), internal.Gorm.Config(general)); err != nil {
    log.Fatalf("mysql init failed: %v", err) // preserves stack + message
}

Prevention

When it happens

Trigger: gorm.Open(mysql.New(mysqlConfig), internal.Gorm.Config(general)) returns err at startup — e.g. mysql.go:/bin/sh-missing driver registration (blank import absent), malformed host/port/user/password/dbname in config.yaml (system.db-mysql), MySQL server down or firewall-blocked, wrong credentials, or TLS/charset params invalid in the DSN.

Common situations: Deploying without the MySQL container/service up (docker-compose ordering); typos in config.yaml db host/path (e.g. wrong parseTime/loc params); MySQL 8 caching_sha2_password with an old driver; database user lacking connect grants; k8s pod starting before the DB service is ready.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/6877f2ee0fec8eab. Report an issue: GitHub.