Add mysql TLS Support

Change: Use DSN custom configuration instead of using specific configuration options.
pull/235/head
labulac 2 years ago committed by 1379
parent 32102f7a8c
commit 4971a06799

@ -19,12 +19,7 @@ sqlite3:
mysql:
host: 127.0.0.1
port: 3306
db: sonicdb
username: "root"
password: "12345678"
dsn: username:password@tcp(host:port)/db_name?charset=utf8mb4&parseTime=True&loc=Local&interpolateParams=true
sonic:
mode: "production"

@ -16,13 +16,11 @@ type PostgreSQL struct {
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
}
type MySQL struct {
Host string `mapstructure:"host"`
Port string `mapstructure:"port"`
DB string `mapstructure:"db"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
Dsn string `mapstructure:"dsn"`
}
type SQLite3 struct {
Enable bool `mapstructure:"enable"`
File string

@ -2,7 +2,6 @@ package dal
import (
"context"
"fmt"
"time"
"go.uber.org/zap"
@ -18,9 +17,6 @@ import (
"github.com/go-sonic/sonic/util/xerr"
)
// mysqlDsn example user:password@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local
const mysqlDsn = "%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local&timeout=3s&readTimeout=1s&writeTimeout=1s&interpolateParams=true"
var (
DB *gorm.DB
DBType consts.DBType
@ -66,8 +62,9 @@ func initMySQL(conf *config.Config, gormLogger logger.Interface) (*gorm.DB, erro
if mysqlConfig == nil {
return nil, xerr.WithMsg(nil, "nil MySQL config")
}
dsn := fmt.Sprintf(mysqlDsn, mysqlConfig.Username, mysqlConfig.Password, mysqlConfig.Host, mysqlConfig.Port, mysqlConfig.DB)
sonicLog.Info("try connect to MySQL", zap.String("dsn", fmt.Sprintf(mysqlDsn, mysqlConfig.Username, "***", mysqlConfig.Host, mysqlConfig.Port, mysqlConfig.DB)))
dsn := mysqlConfig.Dsn
sonicLog.Info("try connect to MySQL", zap.String("dsn", `Use dsn in config`))
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
Logger: gormLogger,
PrepareStmt: true,

Loading…
Cancel
Save