mysql.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package database
  2. import (
  3. "bytes"
  4. "duoduo/conf"
  5. "fmt"
  6. _ "github.com/go-sql-driver/mysql" //加载mysql
  7. "github.com/jinzhu/gorm"
  8. "log"
  9. "strconv"
  10. )
  11. var Eloquent *gorm.DB
  12. var LyMysql *gorm.DB
  13. var BzMysql *gorm.DB
  14. var (
  15. Host string
  16. Port int
  17. Name string
  18. Username string
  19. Password string
  20. )
  21. func Setup() {
  22. //加载配置
  23. confIni, errConf := conf.ConnIni()
  24. if errConf != nil {
  25. fmt.Println(errConf)
  26. }
  27. Host = confIni.MustValue("mysql", "mysql_host")
  28. Port = confIni.MustInt("mysql", "mysql_port")
  29. Name = confIni.MustValue("mysql", "mysql_name")
  30. Username = confIni.MustValue("mysql", "mysql_username")
  31. Password = confIni.MustValue("mysql", "mysql_password")
  32. var err error
  33. conn := GetMysqlConnect()
  34. log.Println(conn)
  35. var db Database
  36. db = new(Mysql)
  37. Eloquent, err = db.Open("mysql", conn)
  38. if err != nil {
  39. log.Fatalf("mysql connect error %v", err)
  40. } else {
  41. log.Printf("mysql connect success!")
  42. }
  43. if Eloquent.Error != nil {
  44. log.Fatalf("database error %v", Eloquent.Error)
  45. }
  46. Eloquent.LogMode(true)
  47. //加载领养数据库链接
  48. Host = confIni.MustValue("ly-mysql", "mysql_host")
  49. Port = confIni.MustInt("ly-mysql", "mysql_port")
  50. Name = confIni.MustValue("ly-mysql", "mysql_name")
  51. Username = confIni.MustValue("ly-mysql", "mysql_username")
  52. Password = confIni.MustValue("ly-mysql", "mysql_password")
  53. conn = GetMysqlConnect()
  54. log.Println(conn)
  55. //var db Database
  56. db = new(Mysql)
  57. LyMysql, err = db.Open("mysql", conn)
  58. if err != nil {
  59. log.Fatalf("mysql connect error %v", err)
  60. } else {
  61. log.Printf("mysql connect success!")
  62. }
  63. if LyMysql.Error != nil {
  64. log.Fatalf("database error %v", Eloquent.Error)
  65. }
  66. LyMysql.LogMode(true)
  67. //加载壁纸数据库链接
  68. Host = confIni.MustValue("bz-mysql", "mysql_host")
  69. Port = confIni.MustInt("bz-mysql", "mysql_port")
  70. Name = confIni.MustValue("bz-mysql", "mysql_name")
  71. Username = confIni.MustValue("bz-mysql", "mysql_username")
  72. Password = confIni.MustValue("bz-mysql", "mysql_password")
  73. conn = GetMysqlConnect()
  74. log.Println(conn)
  75. //var db Database
  76. db = new(Mysql)
  77. BzMysql, err = db.Open("mysql", conn)
  78. if err != nil {
  79. log.Fatalf("mysql connect error %v", err)
  80. } else {
  81. log.Printf("mysql connect success!")
  82. }
  83. if BzMysql.Error != nil {
  84. log.Fatalf("database error %v", Eloquent.Error)
  85. }
  86. sql := BzMysql.DB()
  87. sql.SetMaxOpenConns(100) //设置数据库连接池最大连接数
  88. sql.SetMaxIdleConns(20)
  89. BzMysql.LogMode(true)
  90. }
  91. func GetMysqlConnect() string {
  92. var conn bytes.Buffer
  93. conn.WriteString(Username)
  94. conn.WriteString(":")
  95. conn.WriteString(Password)
  96. conn.WriteString("@tcp(")
  97. conn.WriteString(Host)
  98. conn.WriteString(":")
  99. conn.WriteString(strconv.Itoa(Port))
  100. conn.WriteString(")")
  101. conn.WriteString("/")
  102. conn.WriteString(Name)
  103. conn.WriteString("?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms")
  104. return conn.String()
  105. }
  106. type Database interface {
  107. Open(dbType string, conn string) (db *gorm.DB, err error)
  108. }
  109. type Mysql struct {
  110. }
  111. func (*Mysql) Open(dbType string, conn string) (db *gorm.DB, err error) {
  112. eloquent, err := gorm.Open(dbType, conn)
  113. return eloquent, err
  114. }