mysql.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package parser
  4. import (
  5. "dust-monitor/config"
  6. "github.com/jaryhe/gopkgs/database"
  7. )
  8. var mysqlConfig config.MysqlConfig
  9. func MysqlHandler(conf *config.Configure) {
  10. // mysqlConfig.Addr 不为空表示已经初始化
  11. if mysqlConfig.Addr != "" {
  12. // 地址,用户名,密码相同比较其他配置是否一致
  13. if mysqlConfig.User == conf.Mysql.User &&
  14. mysqlConfig.Password == conf.Mysql.Password &&
  15. mysqlConfig.Addr == conf.Mysql.Addr {
  16. if mysqlConfig.MaxIdle != conf.Mysql.MaxIdle {
  17. database.DB().DB().SetMaxIdleConns(conf.Mysql.MaxIdle)
  18. }
  19. if mysqlConfig.MaxConn != conf.Mysql.MaxConn {
  20. database.DB().DB().SetMaxOpenConns(conf.Mysql.MaxConn)
  21. }
  22. if mysqlConfig.LogMode != conf.Mysql.LogMode {
  23. database.DB().LogMode(conf.Mysql.LogMode)
  24. }
  25. return // 这里直接返回
  26. }
  27. // 地址用户名密码不一致,关闭连接重新连接
  28. database.DB().Close()
  29. }
  30. // 私有db为空使用公共db
  31. dbname := conf.DustMonitor.MysqlDb
  32. if dbname == "" {
  33. dbname = conf.Mysql.DB
  34. }
  35. // 连接database
  36. database.Setup(conf.Mysql.User,
  37. conf.Mysql.Password,
  38. conf.Mysql.Addr,
  39. dbname,
  40. conf.Mysql.Charset,
  41. conf.Mysql.MaxIdle,
  42. conf.Mysql.MaxConn,
  43. conf.Mysql.LogMode)
  44. mysqlConfig = conf.Mysql
  45. }