mysql.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 ShMysql *gorm.DB
  15. var (
  16. Host string
  17. Port int
  18. Name string
  19. Username string
  20. Password string
  21. )
  22. func Setup() {
  23. //加载配置
  24. confIni, errConf := conf.ConnIni()
  25. if errConf != nil {
  26. fmt.Println(errConf)
  27. }
  28. Host = confIni.MustValue("mysql", "mysql_host")
  29. Port = confIni.MustInt("mysql", "mysql_port")
  30. Name = confIni.MustValue("mysql", "mysql_name")
  31. Username = confIni.MustValue("mysql", "mysql_username")
  32. Password = confIni.MustValue("mysql", "mysql_password")
  33. var err error
  34. conn := GetMysqlConnect()
  35. log.Println(conn)
  36. var db Database
  37. db = new(Mysql)
  38. Eloquent, err = db.Open("mysql", conn)
  39. if err != nil {
  40. log.Fatalf("mysql connect error %v", err)
  41. } else {
  42. log.Printf("mysql connect success!")
  43. }
  44. if Eloquent.Error != nil {
  45. log.Fatalf("database error %v", Eloquent.Error)
  46. }
  47. Eloquent.LogMode(true)
  48. //加载领养数据库链接
  49. Host = confIni.MustValue("ly-mysql", "mysql_host")
  50. Port = confIni.MustInt("ly-mysql", "mysql_port")
  51. Name = confIni.MustValue("ly-mysql", "mysql_name")
  52. Username = confIni.MustValue("ly-mysql", "mysql_username")
  53. Password = confIni.MustValue("ly-mysql", "mysql_password")
  54. conn = GetMysqlConnect()
  55. log.Println(conn)
  56. //var db Database
  57. db = new(Mysql)
  58. LyMysql, err = db.Open("mysql", conn)
  59. if err != nil {
  60. log.Fatalf("mysql connect error %v", err)
  61. } else {
  62. log.Printf("mysql connect success!")
  63. }
  64. if LyMysql.Error != nil {
  65. log.Fatalf("database error %v", Eloquent.Error)
  66. }
  67. LyMysql.LogMode(true)
  68. //加载壁纸数据库链接
  69. Host = confIni.MustValue("bz-mysql", "mysql_host")
  70. Port = confIni.MustInt("bz-mysql", "mysql_port")
  71. Name = confIni.MustValue("bz-mysql", "mysql_name")
  72. Username = confIni.MustValue("bz-mysql", "mysql_username")
  73. Password = confIni.MustValue("bz-mysql", "mysql_password")
  74. conn = GetMysqlConnect()
  75. log.Println(conn)
  76. //var db Database
  77. db = new(Mysql)
  78. BzMysql, err = db.Open("mysql", conn)
  79. if err != nil {
  80. log.Fatalf("mysql connect error %v", err)
  81. } else {
  82. log.Printf("mysql connect success!")
  83. }
  84. if BzMysql.Error != nil {
  85. log.Fatalf("database error %v", Eloquent.Error)
  86. }
  87. sql := BzMysql.DB()
  88. sql.SetMaxOpenConns(100) //设置数据库连接池最大连接数
  89. sql.SetMaxIdleConns(20)
  90. BzMysql.LogMode(true)
  91. //加载商户卡数据库链接
  92. Host = confIni.MustValue("sh-mysql", "mysql_host")
  93. Port = confIni.MustInt("sh-mysql", "mysql_port")
  94. Name = confIni.MustValue("sh-mysql", "mysql_name")
  95. Username = confIni.MustValue("sh-mysql", "mysql_username")
  96. Password = confIni.MustValue("sh-mysql", "mysql_password")
  97. conn = GetMysqlConnect()
  98. log.Println(conn)
  99. //var db Database
  100. db = new(Mysql)
  101. ShMysql, err = db.Open("mysql", conn)
  102. if err != nil {
  103. log.Fatalf("mysql connect error %v", err)
  104. } else {
  105. log.Printf("mysql connect success!")
  106. }
  107. if ShMysql.Error != nil {
  108. log.Fatalf("database error %v", Eloquent.Error)
  109. }
  110. //sqlSh := ShMysql.DB()
  111. //sqlSh.SetMaxOpenConns(100) //设置数据库连接池最大连接数
  112. //sqlSh.SetMaxIdleConns(20)
  113. ShMysql.LogMode(true)
  114. }
  115. func GetMysqlConnect() string {
  116. var conn bytes.Buffer
  117. conn.WriteString(Username)
  118. conn.WriteString(":")
  119. conn.WriteString(Password)
  120. conn.WriteString("@tcp(")
  121. conn.WriteString(Host)
  122. conn.WriteString(":")
  123. conn.WriteString(strconv.Itoa(Port))
  124. conn.WriteString(")")
  125. conn.WriteString("/")
  126. conn.WriteString(Name)
  127. conn.WriteString("?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms")
  128. return conn.String()
  129. }
  130. type Database interface {
  131. Open(dbType string, conn string) (db *gorm.DB, err error)
  132. }
  133. type Mysql struct {
  134. }
  135. func (*Mysql) Open(dbType string, conn string) (db *gorm.DB, err error) {
  136. eloquent, err := gorm.Open(dbType, conn)
  137. return eloquent, err
  138. }