mysql.go 1.67 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12
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"
)

13 14 15
var ProdReadOnlyDB *gorm.DB
var ProdWriteDB *gorm.DB
var TestDB *gorm.DB
hujiebin's avatar
hujiebin committed
16 17 18 19

func init() {
	var err error
	options := "?charset=utf8mb4&parseTime=True&loc=Local&time_zone=" + url.QueryEscape("'+8:00'")
hujiebin's avatar
1  
hujiebin committed
20
	dsn := "hilo_read:hilo_TX_readonly!@#$@(de-cynosdbmysql-grp-rv5zqms3.sql.tencentcdb.com:25642)/hilo" + options
hujiebin's avatar
hujiebin committed
21

22
	ProdReadOnlyDB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
hujiebin's avatar
hujiebin committed
23 24 25 26 27 28 29 30
		NamingStrategy: schema.NamingStrategy{SingularTable: true},
	})
	if err != nil {
		log.Fatalf("mysql connect error %v", err)
	} else {
		log.Println("mysql connect success")
	}

31 32 33 34
	if ProdReadOnlyDB.Error != nil {
		fmt.Printf("database error %v", ProdReadOnlyDB.Error)
	}

hujiebin's avatar
1  
hujiebin committed
35
	dsn = "hilo_test:cPsTMSA9szQ6B9Y2zFXSvpDdduB8kZxC@(hk-cynosdbmysql-grp-a3wqck8p.sql.tencentcdb.com:22303)/hilo" + options
36 37 38 39 40 41 42 43 44 45 46 47 48 49

	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)
	}

hujiebin's avatar
1  
hujiebin committed
50
	dsn = "hilo_master:o8NNd8F7e6On2RqIgOhsy1PsiSxROT3n@(de-cynosdbmysql-grp-rv5zqms3.sql.tencentcdb.com:25642)/hilo" + options
51 52 53 54 55 56 57 58 59 60 61 62

	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)
hujiebin's avatar
hujiebin committed
63 64
	}
}