You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.5 KiB

7 years ago
4 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package mysql
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "github.com/go-sql-driver/mysql"
  6. "strings"
  7. "time"
  8. "github.com/seaweedfs/seaweedfs/weed/filer"
  9. _ "github.com/go-sql-driver/mysql"
  10. "github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
  11. "github.com/seaweedfs/seaweedfs/weed/util"
  12. )
  13. const (
  14. CONNECTION_URL_PATTERN = "%s:%s@tcp(%s:%d)/%s?collation=utf8mb4_bin"
  15. )
  16. func init() {
  17. filer.Stores = append(filer.Stores, &MysqlStore{})
  18. }
  19. type MysqlStore struct {
  20. abstract_sql.AbstractSqlStore
  21. }
  22. func (store *MysqlStore) GetName() string {
  23. return "mysql"
  24. }
  25. func (store *MysqlStore) Initialize(configuration util.Configuration, prefix string) (err error) {
  26. return store.initialize(
  27. configuration.GetString(prefix+"dsn"),
  28. configuration.GetString(prefix+"upsertQuery"),
  29. configuration.GetBool(prefix+"enableUpsert"),
  30. configuration.GetString(prefix+"username"),
  31. configuration.GetString(prefix+"password"),
  32. configuration.GetString(prefix+"hostname"),
  33. configuration.GetInt(prefix+"port"),
  34. configuration.GetString(prefix+"database"),
  35. configuration.GetInt(prefix+"connection_max_idle"),
  36. configuration.GetInt(prefix+"connection_max_open"),
  37. configuration.GetInt(prefix+"connection_max_lifetime_seconds"),
  38. configuration.GetBool(prefix+"interpolateParams"),
  39. )
  40. }
  41. func (store *MysqlStore) initialize(dsn string, upsertQuery string, enableUpsert bool, user, password, hostname string, port int, database string, maxIdle, maxOpen,
  42. maxLifetimeSeconds int, interpolateParams bool) (err error) {
  43. store.SupportBucketTable = false
  44. if !enableUpsert {
  45. upsertQuery = ""
  46. }
  47. store.SqlGenerator = &SqlGenMysql{
  48. CreateTableSqlTemplate: "",
  49. DropTableSqlTemplate: "DROP TABLE `%s`",
  50. UpsertQueryTemplate: upsertQuery,
  51. }
  52. if dsn == "" {
  53. dsn = fmt.Sprintf(CONNECTION_URL_PATTERN, user, password, hostname, port, database)
  54. if interpolateParams {
  55. dsn += "&interpolateParams=true"
  56. }
  57. }
  58. cfg, err := mysql.ParseDSN(dsn)
  59. if err != nil {
  60. return fmt.Errorf("can not parse DSN error:%v", err)
  61. }
  62. var dbErr error
  63. store.DB, dbErr = sql.Open("mysql", dsn)
  64. if dbErr != nil {
  65. store.DB.Close()
  66. store.DB = nil
  67. return fmt.Errorf("can not connect to %s error:%v", strings.ReplaceAll(dsn, cfg.Passwd, "<ADAPTED>"), err)
  68. }
  69. store.DB.SetMaxIdleConns(maxIdle)
  70. store.DB.SetMaxOpenConns(maxOpen)
  71. store.DB.SetConnMaxLifetime(time.Duration(maxLifetimeSeconds) * time.Second)
  72. if err = store.DB.Ping(); err != nil {
  73. return fmt.Errorf("connect to %s error:%v", strings.ReplaceAll(dsn, cfg.Passwd, "<ADAPTED>"), err)
  74. }
  75. return nil
  76. }