Skip to content

Commit

Permalink
feat: support connect to mysql with unix socket (#1571)
Browse files Browse the repository at this point in the history
  • Loading branch information
5aaee9 authored Dec 19, 2022
1 parent 42a31f2 commit 8dafb4f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
15 changes: 12 additions & 3 deletions models/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,20 @@ func Init() {
conf.DatabaseConfig.Name,
conf.DatabaseConfig.Port))
case "mysql", "mssql":
db, err = gorm.Open(conf.DatabaseConfig.Type, fmt.Sprintf("%s:%s@(%s:%d)/%s?charset=%s&parseTime=True&loc=Local",
var host string
if conf.DatabaseConfig.UnixSocket {
host = fmt.Sprintf("unix(%s)",
conf.DatabaseConfig.Host)
} else {
host = fmt.Sprintf("(%s:%d)",
conf.DatabaseConfig.Host,
conf.DatabaseConfig.Port)
}

db, err = gorm.Open(conf.DatabaseConfig.Type, fmt.Sprintf("%s:%s@%s/%s?charset=%s&parseTime=True&loc=Local",
conf.DatabaseConfig.User,
conf.DatabaseConfig.Password,
conf.DatabaseConfig.Host,
conf.DatabaseConfig.Port,
host,
conf.DatabaseConfig.Name,
conf.DatabaseConfig.Charset))
default:
Expand Down
1 change: 1 addition & 0 deletions pkg/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type database struct {
DBFile string
Port int
Charset string
UnixSocket bool
}

// system 系统通用配置
Expand Down
9 changes: 5 additions & 4 deletions pkg/conf/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ var RedisConfig = &redis{

// DatabaseConfig 数据库配置
var DatabaseConfig = &database{
Type: "UNSET",
Charset: "utf8",
DBFile: "cloudreve.db",
Port: 3306,
Type: "UNSET",
Charset: "utf8",
DBFile: "cloudreve.db",
Port: 3306,
UnixSocket: false,
}

// SystemConfig 系统公用配置
Expand Down

0 comments on commit 8dafb4f

Please sign in to comment.