db.go 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Pipe - A small and beautiful blogging platform written in golang.
  2. // Copyright (C) 2017-2018, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. package service
  17. import (
  18. "fmt"
  19. "github.com/astaxie/beego"
  20. "github.com/jinzhu/gorm"
  21. _ "github.com/jinzhu/gorm/dialects/mssql" // mssql
  22. _ "github.com/jinzhu/gorm/dialects/mysql" // mysql
  23. )
  24. var readDb *gorm.DB
  25. var writeDb *gorm.DB
  26. var readRemoteDb *gorm.DB
  27. var writeUserDb *gorm.DB
  28. var readUserDb *gorm.DB
  29. var readMiddleDb *gorm.DB
  30. var writeMiddleDb *gorm.DB
  31. var backupDb *gorm.DB
  32. var err error
  33. var blDb *gorm.DB
  34. func ConnectDB() {
  35. readHost := beego.AppConfig.String("readmysqlhost")
  36. readPort := beego.AppConfig.String("readmysqlport")
  37. readUser := beego.AppConfig.String("readmysqluser")
  38. readPass := beego.AppConfig.String("readmysqlpass")
  39. readName := beego.AppConfig.String("readmysqlname")
  40. writeHost := beego.AppConfig.String("writemysqlhost")
  41. writePort := beego.AppConfig.String("writemysqlport")
  42. writeUser := beego.AppConfig.String("writemysqluser")
  43. writePass := beego.AppConfig.String("writemysqlpass")
  44. writeName := beego.AppConfig.String("writemysqlname")
  45. readUserHost := beego.AppConfig.String("readuserhost")
  46. readUserPort := beego.AppConfig.String("readuserport")
  47. readUserUser := beego.AppConfig.String("readuseruser")
  48. readUserPass := beego.AppConfig.String("readuserpass")
  49. readUserName := beego.AppConfig.String("readusername")
  50. // readRemoteHost := beego.AppConfig.String("readremotehost")
  51. // readRemotePort := beego.AppConfig.String("readremoteport")
  52. // readRemoteUser := beego.AppConfig.String("readremoteuser")
  53. // readRemotePass := beego.AppConfig.String("readremotepass")
  54. // readRemoteName := beego.AppConfig.String("readremotename")
  55. writeUserHost := beego.AppConfig.String("writeuserhost")
  56. writeUserPort := beego.AppConfig.String("writeuserport")
  57. writeUserUser := beego.AppConfig.String("writeuseruser")
  58. writeUserPass := beego.AppConfig.String("writeuserpass")
  59. writeUserName := beego.AppConfig.String("writeusername")
  60. readMiddleHost := beego.AppConfig.String("readmiddlehost")
  61. readMiddlePort := beego.AppConfig.String("readmiddleport")
  62. readMiddleUser := beego.AppConfig.String("readmiddleuser")
  63. readMiddlePass := beego.AppConfig.String("readmiddlepass")
  64. readMiddleName := beego.AppConfig.String("readmiddlename")
  65. writeMiddleHost := beego.AppConfig.String("writemiddlehost")
  66. writeMiddlePort := beego.AppConfig.String("writemiddleport")
  67. writeMiddleUser := beego.AppConfig.String("writemiddleuser")
  68. writeMiddlePass := beego.AppConfig.String("writemiddlepass")
  69. writeMiddleName := beego.AppConfig.String("writemiddlename")
  70. // backupHost := beego.AppConfig.String("backuphost")
  71. // backupPort := beego.AppConfig.String("backupport")
  72. // backupUser := beego.AppConfig.String("backupuser")
  73. // backupPass := beego.AppConfig.String("backuppass")
  74. // backupName := beego.AppConfig.String("backupname")
  75. blHost := beego.AppConfig.String("blmysqlhost")
  76. blPort := beego.AppConfig.String("blmysqlport")
  77. blUser := beego.AppConfig.String("blmysqluser")
  78. blPass := beego.AppConfig.String("blmysqlpass")
  79. blName := beego.AppConfig.String("blmysqlname")
  80. fmt.Println(blHost)
  81. fmt.Println(blPort)
  82. fmt.Println(blUser)
  83. fmt.Println(blPass)
  84. fmt.Println(blName)
  85. rdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUser, readPass, readHost, readPort, readName)
  86. wdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUser, writePass, writeHost, writePort, writeName)
  87. // rudsn := fmt.Sprintf("server=%s;port=%s;database=%s;user id=%s;password=%s", readRemoteHost, readRemotePort, readRemoteName, readRemoteUser, readRemotePass)
  88. wudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUserUser, writeUserPass, writeUserHost, writeUserPort, writeUserName)
  89. rmdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readMiddleUser, readMiddlePass, readMiddleHost, readMiddlePort, readMiddleName)
  90. wmdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeMiddleUser, writeMiddlePass, writeMiddleHost, writeMiddlePort, writeMiddleName)
  91. rudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUserUser, readUserPass, readUserHost, readUserPort, readUserName)
  92. bldsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", blUser, blPass, blHost, blPort, blName)
  93. // bdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", backupUser, backupPass, backupHost, backupPort, backupName)
  94. readDb, err = gorm.Open("mysql", rdsn)
  95. if err != nil {
  96. //beego.Error(err)
  97. }
  98. readDb.DB().SetMaxIdleConns(10)
  99. readDb.DB().SetMaxOpenConns(100)
  100. readDb.LogMode(true)
  101. // backupDb, err = gorm.Open("mysql", bdsn)
  102. // if err != nil {
  103. // //beego.Error(err)
  104. // }
  105. // backupDb.DB().SetMaxIdleConns(10)
  106. // backupDb.DB().SetMaxOpenConns(100)
  107. // backupDb.LogMode(true)
  108. writeDb, err = gorm.Open("mysql", wdsn)
  109. if err != nil {
  110. //beego.Error(err)
  111. }
  112. writeDb.DB().SetMaxIdleConns(10)
  113. writeDb.DB().SetMaxOpenConns(100)
  114. writeDb.LogMode(true)
  115. // fmt.Println(rudsn)
  116. // readRemoteDb, err = gorm.Open("mssql", rudsn)
  117. // if err != nil {
  118. // panic(err)
  119. // }
  120. // readRemoteDb.DB().SetMaxIdleConns(10)
  121. // readRemoteDb.DB().SetMaxOpenConns(100)
  122. // readRemoteDb.LogMode(true)
  123. writeUserDb, err = gorm.Open("mysql", wudsn)
  124. if err != nil {
  125. //beego.Error(err)
  126. }
  127. writeUserDb.DB().SetMaxIdleConns(10)
  128. writeUserDb.DB().SetMaxOpenConns(100)
  129. writeUserDb.LogMode(true)
  130. readMiddleDb, err = gorm.Open("mysql", rmdsn)
  131. if err != nil {
  132. //beego.Error(err)
  133. }
  134. readMiddleDb.DB().SetMaxIdleConns(10)
  135. readMiddleDb.DB().SetMaxOpenConns(100)
  136. readMiddleDb.LogMode(true)
  137. writeMiddleDb, err = gorm.Open("mysql", wmdsn)
  138. if err != nil {
  139. //beego.Error(err)
  140. }
  141. writeMiddleDb.DB().SetMaxIdleConns(10)
  142. writeMiddleDb.DB().SetMaxOpenConns(100)
  143. writeMiddleDb.LogMode(true)
  144. readUserDb, err = gorm.Open("mysql", rudsn)
  145. if err != nil {
  146. //beego.Error(err)
  147. }
  148. readUserDb.DB().SetMaxIdleConns(10)
  149. readUserDb.DB().SetMaxOpenConns(100)
  150. readUserDb.LogMode(true)
  151. blDb, err = gorm.Open("mysql", bldsn)
  152. if err != nil {
  153. fmt.Println("12344")
  154. fmt.Println(bldsn)
  155. fmt.Println(err)
  156. }
  157. blDb.DB().SetMaxIdleConns(10)
  158. blDb.DB().SetMaxOpenConns(100)
  159. blDb.LogMode(true)
  160. }
  161. //func DisconnectDB() {
  162. // if err := readDb.Close(); nil != err {
  163. // beego.Error("Disconnect from database failed: " + err.Error())
  164. // }
  165. //}
  166. func XTReadDB() *gorm.DB {
  167. return readDb
  168. }
  169. func XTWriteDB() *gorm.DB {
  170. return writeDb
  171. }
  172. // func RemoteReadDB() *gorm.DB {
  173. // return readRemoteDb
  174. // }
  175. func UserWriteDB() *gorm.DB {
  176. return writeUserDb
  177. }
  178. func MiddleReadDB() *gorm.DB {
  179. return readMiddleDb
  180. }
  181. func MiddleWriteDB() *gorm.DB {
  182. return writeMiddleDb
  183. }