This commit is contained in:
Louis Lam 2024-11-02 18:04:08 +08:00
parent 9eb2ab7587
commit a17bd03c04
2 changed files with 22 additions and 8 deletions

View file

@ -296,7 +296,7 @@ class Database {
client: "mysql2", client: "mysql2",
connection: { connection: {
socketPath: embeddedMariaDB.socketPath, socketPath: embeddedMariaDB.socketPath,
user: "node", user: embeddedMariaDB.username,
database: "kuma", database: "kuma",
timezone: "Z", timezone: "Z",
typeCast: function (field, next) { typeCast: function (field, next) {

View file

@ -14,9 +14,15 @@ class EmbeddedMariaDB {
mariadbDataDir = "/app/data/mariadb"; 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} * @type {ChildProcessWithoutNullStreams}
@ -49,6 +55,12 @@ class EmbeddedMariaDB {
* @returns {Promise<void>|void} A promise that resolves when the MariaDB is started or void if it is already started * @returns {Promise<void>|void} A promise that resolves when the MariaDB is started or void if it is already started
*/ */
start() { 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(); this.startChildProcess();
return new Promise((resolve) => { return new Promise((resolve) => {
@ -73,8 +85,6 @@ class EmbeddedMariaDB {
return; return;
} }
this.initDB();
// Create the mariadb directory if not exists and chown it to the node user // Create the mariadb directory if not exists and chown it to the node user
if (!fs.existsSync(this.mariadbDataDir)) { if (!fs.existsSync(this.mariadbDataDir)) {
fs.mkdirSync(this.mariadbDataDir, { fs.mkdirSync(this.mariadbDataDir, {
@ -107,6 +117,8 @@ class EmbeddedMariaDB {
fs.chmodSync(this.runDir, 0o755); fs.chmodSync(this.runDir, 0o755);
} }
this.initDB();
this.running = true; this.running = true;
log.info("mariadb", "Starting Embedded MariaDB"); log.info("mariadb", "Starting Embedded MariaDB");
this.childProcess = childProcess.spawn(this.exec, [ this.childProcess = childProcess.spawn(this.exec, [
@ -171,9 +183,11 @@ class EmbeddedMariaDB {
recursive: true, recursive: true,
}); });
let result = childProcess.spawnSync("mysql_install_db", [ let result = childProcess.spawnSync("mariadb-install-db", [
"--user=node", "--user=node",
"--ldata=" + this.mariadbDataDir, "--auth-root-socket-user=node",
"--datadir=" + this.mariadbDataDir,
"--auth-root-authentication-method=socket",
]); ]);
if (result.status !== 0) { if (result.status !== 0) {
@ -201,7 +215,7 @@ class EmbeddedMariaDB {
async initDBAfterStarted() { async initDBAfterStarted() {
const connection = mysql.createConnection({ const connection = mysql.createConnection({
socketPath: this.socketPath, socketPath: this.socketPath,
user: "node", user: this.username,
}); });
let result = await connection.execute("CREATE DATABASE IF NOT EXISTS `kuma`"); let result = await connection.execute("CREATE DATABASE IF NOT EXISTS `kuma`");