Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

尝试为MySQL添加SSL支持 #44

Merged
merged 2 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ table: users
# user: root
# password: pwd
# database: e5sub
# ssl_mode is only required when the database requires a SSL connection (e.g. TiDB Cloud)
# ssl_mode: PREFERRED
sqlite:
db: data.db
```
Expand Down
2 changes: 2 additions & 0 deletions README_zhCN.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ table: users
# user: root
# password: pwd
# database: e5sub
# ssl_mode仅在数据库需要SSL链接时才需要配置(如连接TiDB Cloud)
# ssl_mode: PREFERRED
sqlite:
db: data.db
```
Expand Down
12 changes: 7 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ func Init() {
switch DB {
case "mysql":
Mysql = mysqlConfig{
Host: viper.GetString("mysql.host"),
Port: viper.GetInt("mysql.port"),
User: viper.GetString("mysql.user"),
Password: viper.GetString("mysql.password"),
DB: viper.GetString("mysql.database"),
Host: viper.GetString("mysql.host"),
Port: viper.GetInt("mysql.port"),
User: viper.GetString("mysql.user"),
Password: viper.GetString("mysql.password"),
DB: viper.GetString("mysql.database"),
SSLMode: viper.GetString("mysql.ssl_mode"),
EnabledTLSProtocols: viper.GetString("mysql.enabled_tls_protocols"),
}
case "sqlite":
Sqlite = sqliteConfig{
Expand Down
12 changes: 7 additions & 5 deletions config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ type sqliteConfig struct {
DB string `json:"db,omitempty"`
}
type mysqlConfig struct {
Host string `json:"host,omitempty"`
Port int `json:"port,omitempty"`
User string `json:"user,omitempty"`
Password string `json:"password,omitempty"`
DB string `json:"db,omitempty"`
Host string `json:"host,omitempty"`
Port int `json:"port,omitempty"`
User string `json:"user,omitempty"`
Password string `json:"password,omitempty"`
DB string `json:"db,omitempty"`
SSLMode string `json:"ssl_mode,omitempty"`
EnabledTLSProtocols string `json:"enabled_tls_protocols,omitempty"`
}
10 changes: 8 additions & 2 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ func Init() {

switch config.DB {
case "mysql":
dial = mysql.Open(fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
var dsn = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
config.Mysql.User,
config.Mysql.Password,
config.Mysql.Host,
config.Mysql.Port,
config.Mysql.DB,
))
)

if config.Mysql.SSLMode != "" {
dsn += "&tls=" + config.Mysql.SSLMode
}

dial = mysql.Open(dsn)
case "sqlite":
dial = sqlite.Open(config.Sqlite.DB)
}
Expand Down