db.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package initialize
  2. import (
  3. _ "github.com/go-sql-driver/mysql"
  4. )
  5. import (
  6. "fmt"
  7. "log"
  8. "time"
  9. "gorm.io/gorm/logger"
  10. "github.com/beego/beego/v2/core/logs"
  11. beego "github.com/beego/beego/v2/server/web"
  12. "gorm.io/driver/mysql"
  13. "gorm.io/gorm"
  14. )
  15. var DB *gorm.DB
  16. var Err error
  17. // 重写gorm日志的Writer
  18. type Writer struct {
  19. }
  20. func (w Writer) Printf(format string, args ...interface{}) {
  21. // log.Infof(format, args...)
  22. logs.Notice(format, args...)
  23. }
  24. // 设置psql
  25. func init() {
  26. log.Println("连接数据库...")
  27. mysqlUrlPort, err := beego.AppConfig.String("mysqlUrlPort")
  28. if err != nil {
  29. log.Fatalf("无法获取mysqlUrlPort: %v", err)
  30. }
  31. mysqlUser, err := beego.AppConfig.String("mysqlUser")
  32. if err != nil {
  33. log.Fatalf("无法获取mysqlUser: %v", err)
  34. }
  35. mysqlPass, err := beego.AppConfig.String("mysqlPass")
  36. if err != nil {
  37. log.Fatalf("无法获取mysqlPass: %v", err)
  38. }
  39. mysqlDB, err := beego.AppConfig.String("mysqlDB")
  40. if err != nil {
  41. log.Fatalf("无法获取mysqlDB: %v", err)
  42. }
  43. mysqlMaxConns, err := beego.AppConfig.Int("mysqlMaxConns")
  44. if err != nil {
  45. log.Fatalf("无法获取mysqlMaxConns: %v", err)
  46. }
  47. mysqlMaxOpen, err := beego.AppConfig.Int("mysqlMaxOpen")
  48. if err != nil {
  49. log.Fatalf("无法获取mysqlMaxOpen: %v", err)
  50. }
  51. sqlloglevel, err := beego.AppConfig.Int("sqlloglevel")
  52. if err != nil {
  53. log.Fatalf("无法获取sqlloglevel: %v", err)
  54. }
  55. dataSource := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
  56. mysqlUser,
  57. mysqlPass,
  58. mysqlUrlPort,
  59. mysqlDB,
  60. )
  61. slow_threshold, err := beego.AppConfig.Int("slow_threshold")
  62. if err != nil {
  63. log.Fatalf("无法获取slow_threshold: %v", err)
  64. }
  65. //设置gorm日志规则
  66. newLogger := logger.New(
  67. // log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer 单独设置grom日志输出
  68. Writer{}, // beego日志info输出
  69. logger.Config{
  70. SlowThreshold: time.Duration(slow_threshold) * time.Millisecond, // Slow SQL threshold
  71. LogLevel: logger.LogLevel(sqlloglevel), // Log level
  72. IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
  73. Colorful: true, // Disable color
  74. },
  75. )
  76. DB, Err = gorm.Open(mysql.Open(dataSource), &gorm.Config{
  77. DisableForeignKeyConstraintWhenMigrating: true,
  78. Logger: newLogger,
  79. DisableAutomaticPing: true,
  80. })
  81. if Err != nil {
  82. log.Fatalf("连接数据库失败: %v", Err)
  83. }
  84. SqlDB, err2 := DB.DB()
  85. if err2 != nil {
  86. log.Fatalf("连接数据库失败: %v", err2)
  87. }
  88. SqlDB.SetMaxIdleConns(mysqlMaxConns)
  89. SqlDB.SetMaxOpenConns(mysqlMaxOpen)
  90. SqlDB.SetConnMaxLifetime(time.Hour)
  91. log.Println("连接数据库完成...")
  92. }