package mysql import ( "fmt" _ "github.com/go-sql-driver/mysql" //加载mysql驱动 "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/schema" "log" "net/url" ) var ProdReadOnlyDB *gorm.DB var ProdWriteDB *gorm.DB var TestDB *gorm.DB func init() { var err error options := "?charset=utf8mb4&parseTime=True&loc=Local&time_zone=" + url.QueryEscape("'+8:00'") dsn := "read_only:hilo1632@(rm-eb3w787dzn9c8g07vuo.mysql.dubai.rds.aliyuncs.com)/hilo" + options ProdReadOnlyDB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{ NamingStrategy: schema.NamingStrategy{SingularTable: true}, }) if err != nil { log.Fatalf("mysql connect error %v", err) } else { log.Println("mysql connect success") } if ProdReadOnlyDB.Error != nil { fmt.Printf("database error %v", ProdReadOnlyDB.Error) } dsn = "root:yX0jPAhO0I4s2zlA@(47.244.34.27:3306)/hilo" + options TestDB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{ NamingStrategy: schema.NamingStrategy{SingularTable: true}, }) if err != nil { log.Fatalf("mysql connect error %v", err) } else { log.Println("mysql connect success") } if TestDB.Error != nil { fmt.Printf("database error %v", TestDB.Error) } dsn = "nextvideo:ihlUwI4nhi9W88MI@(rm-eb3w787dzn9c8g07vuo.mysql.dubai.rds.aliyuncs.com)/hilo" + options ProdWriteDB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{ NamingStrategy: schema.NamingStrategy{SingularTable: true}, }) if err != nil { log.Fatalf("mysql connect error %v", err) } else { log.Println("mysql connect success") } if ProdWriteDB.Error != nil { fmt.Printf("database error %v", ProdWriteDB.Error) } }