From 9738c3f8c8bb3d9798ee5b5a98a77a21a229112a Mon Sep 17 00:00:00 2001 From: Zankaria Date: Thu, 19 Sep 2024 15:59:46 +0200 Subject: [PATCH] Add support for unix socket connections to the mysql database --- config.template.yaml | 2 +- src/database.js | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/config.template.yaml b/config.template.yaml index 3652ddd4..b24de8d8 100644 --- a/config.template.yaml +++ b/config.template.yaml @@ -1,5 +1,5 @@ # MySQL server details -# server: domain or IP of MySQL server +# server: domain, IP or unix socket path of MySQL server. If a unix socket, it be like so `unix:/path/to/sock.sock` # database: a MySQL database that the user specified has read/write access to # user: username to authenticate as # password: password for user diff --git a/src/database.js b/src/database.js index 31ed2017..4433d915 100644 --- a/src/database.js +++ b/src/database.js @@ -29,9 +29,19 @@ let globalBanDB = null; class Database { constructor(knexConfig = null) { if (knexConfig === null) { - knexConfig = { - client: 'mysql', - connection: { + let mysqlServer = Config.get('mysql.server'); + let connection; + if (mysqlServer.startsWith('unix:')) { + connection = { + socketPath: mysqlServer.slice(5), + user: Config.get('mysql.user'), + password: Config.get('mysql.password'), + database: Config.get('mysql.database'), + multipleStatements: true, // Legacy thing + charset: 'utf8mb4' + } + } else { + connection = { host: Config.get('mysql.server'), port: Config.get('mysql.port'), user: Config.get('mysql.user'), @@ -39,7 +49,12 @@ class Database { database: Config.get('mysql.database'), multipleStatements: true, // Legacy thing charset: 'utf8mb4' - }, + } + } + + knexConfig = { + client: 'mysql', + connection, pool: { min: Config.get('mysql.pool-size'), max: Config.get('mysql.pool-size')