From a17bd03c0433425bbec94fdf3a7522634ac6f46a Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Sat, 2 Nov 2024 18:04:08 +0800 Subject: [PATCH] WIP --- server/database.js | 2 +- server/embedded-mariadb.js | 28 +++++++++++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/server/database.js b/server/database.js index 3927d6db8..3b7646de8 100644 --- a/server/database.js +++ b/server/database.js @@ -296,7 +296,7 @@ class Database { client: "mysql2", connection: { socketPath: embeddedMariaDB.socketPath, - user: "node", + user: embeddedMariaDB.username, database: "kuma", timezone: "Z", typeCast: function (field, next) { diff --git a/server/embedded-mariadb.js b/server/embedded-mariadb.js index 6899ab02a..252f81e2c 100644 --- a/server/embedded-mariadb.js +++ b/server/embedded-mariadb.js @@ -14,9 +14,15 @@ class EmbeddedMariaDB { mariadbDataDir = "/app/data/mariadb"; - runDir = "/app/data/run/mariadb"; + runDir = "/app/data/run"; - socketPath = this.runDir + "/mysqld.sock"; + socketPath = this.runDir + "/mariadb.sock"; + + /** + * The username to connect to the MariaDB + * @type {string} + */ + username = null; /** * @type {ChildProcessWithoutNullStreams} @@ -49,6 +55,12 @@ class EmbeddedMariaDB { * @returns {Promise|void} A promise that resolves when the MariaDB is started or void if it is already started */ start() { + // Check if the current user is "node" or "root" + this.username = require("os").userInfo().username; + if (this.username !== "node" && this.username !== "root") { + throw new Error("Embedded Mariadb supports only 'node' or 'root' user, but the current user is: " + this.username); + } + this.startChildProcess(); return new Promise((resolve) => { @@ -73,8 +85,6 @@ class EmbeddedMariaDB { return; } - this.initDB(); - // Create the mariadb directory if not exists and chown it to the node user if (!fs.existsSync(this.mariadbDataDir)) { fs.mkdirSync(this.mariadbDataDir, { @@ -107,6 +117,8 @@ class EmbeddedMariaDB { fs.chmodSync(this.runDir, 0o755); } + this.initDB(); + this.running = true; log.info("mariadb", "Starting Embedded MariaDB"); this.childProcess = childProcess.spawn(this.exec, [ @@ -171,9 +183,11 @@ class EmbeddedMariaDB { recursive: true, }); - let result = childProcess.spawnSync("mysql_install_db", [ + let result = childProcess.spawnSync("mariadb-install-db", [ "--user=node", - "--ldata=" + this.mariadbDataDir, + "--auth-root-socket-user=node", + "--datadir=" + this.mariadbDataDir, + "--auth-root-authentication-method=socket", ]); if (result.status !== 0) { @@ -201,7 +215,7 @@ class EmbeddedMariaDB { async initDBAfterStarted() { const connection = mysql.createConnection({ socketPath: this.socketPath, - user: "node", + user: this.username, }); let result = await connection.execute("CREATE DATABASE IF NOT EXISTS `kuma`");