/sk_admin/setup/mysql.go

https://github.com/yxhsea/SecKill · Go · 42 lines · 33 code · 5 blank · 4 comment · 2 complexity · 2dfbfa63f095ee9b661959995e471a31 MD5 · raw file

  1. package setup
  2. import (
  3. "SecKill/sk_admin/config"
  4. "fmt"
  5. _ "github.com/go-sql-driver/mysql"
  6. "github.com/gohouse/gorose"
  7. )
  8. func InitMysql(hostMysql, portMysql, userMysql, pwdMysql, dbMysql string) {
  9. DbConfig := map[string]interface{}{
  10. // Default database configuration
  11. "Default": "mysql_dev",
  12. // (Connection pool) Max open connections, default value 0 means unlimit.
  13. "SetMaxOpenConns": 300,
  14. // (Connection pool) Max idle connections, default value is 1.
  15. "SetMaxIdleConns": 10,
  16. // Define the database configuration character "mysql_dev".
  17. "Connections": map[string]map[string]string{
  18. "mysql_dev": map[string]string{
  19. "host": hostMysql,
  20. "username": userMysql,
  21. "password": pwdMysql,
  22. "port": portMysql,
  23. "database": dbMysql,
  24. "charset": "utf8",
  25. "protocol": "tcp",
  26. "prefix": "", // Table prefix
  27. "driver": "mysql", // Database driver(mysql,sqlite,postgres,oracle,mssql)
  28. },
  29. },
  30. }
  31. connection, err := gorose.Open(DbConfig)
  32. if err != nil {
  33. fmt.Println(err)
  34. return
  35. }
  36. config.SecAdminConfCtx.DbConf = &config.DbConf{DbConn: connection}
  37. }