From 9c1325f38ec38bc4ca01df4ca8bc723841cc7cc7 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Wed, 23 Feb 2022 15:08:28 +0100 Subject: [PATCH] MariaDB: Connect via Unix domain socket if address starts with "/" --- internal/config/db.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/config/db.go b/internal/config/db.go index 09dc63240..26ccd798f 100644 --- a/internal/config/db.go +++ b/internal/config/db.go @@ -49,11 +49,21 @@ func (c *Config) DatabaseDsn() string { if c.options.DatabaseDsn == "" { switch c.DatabaseDriver() { case MySQL, MariaDB: + address := c.DatabaseServer() + + // Connect via TCP or Unix Domain Socket? + if strings.HasPrefix(address, "/") { + log.Debugf("mariadb: connecting via Unix domain socket") + address = fmt.Sprintf("unix(%s)", address) + } else { + address = fmt.Sprintf("tcp(%s)", address) + } + return fmt.Sprintf( - "%s:%s@tcp(%s)/%s?charset=utf8mb4,utf8&collation=utf8mb4_unicode_ci&parseTime=true", + "%s:%s@%s/%s?charset=utf8mb4,utf8&collation=utf8mb4_unicode_ci&parseTime=true", c.DatabaseUser(), c.DatabasePassword(), - c.DatabaseServer(), + address, c.DatabaseName(), ) case Postgres: