package service import ( "agent-go/entity" "gorm.io/driver/mysql" "gorm.io/gorm" ) func InitDB() *gorm.DB { // 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情 dsn := "wmm:Superwmm.23@tcp(42.192.52.227:21306)/gormdb?charset=utf8mb4&parseTime=True&loc=Local" db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) if err != nil { panic(err) } // init the table db.AutoMigrate(&entity.ServerInfo{}) // cache this db to global entity.CONGIG.DB = db return db }