feat(mysql): allow configuring socketDir on mysql. if not provided, uses dataDir as default. solves #171

This commit is contained in:
Attila Ersek 2024-04-25 13:00:04 +02:00 committed by Shivaraj B H
parent 4c9fd4fb3b
commit dbb93b39ce
2 changed files with 46 additions and 17 deletions

View File

@ -21,6 +21,12 @@ in
description = "The mysql data directory";
};
socketDir = lib.mkOption {
type = types.nullOr types.str;
default = config.dataDir;
description = "The mysql socket directory. If null, defaults to dataDir.";
};
settings = lib.mkOption {
type = format.type;
default = { };
@ -170,8 +176,8 @@ in
mysqldOptions = "${mysqlOptions} --datadir=${config.dataDir} --basedir=${config.package}";
envs = ''
MYSQL_HOME=$(${pkgs.coreutils}/bin/realpath ${config.dataDir})
MYSQL_UNIX_PORT=$(${pkgs.coreutils}/bin/realpath ${config.dataDir + "/mysql.sock"})
MYSQLX_UNIX_PORT=$(${pkgs.coreutils}/bin/realpath ${config.dataDir + "/mysqlx.sock"})
MYSQL_UNIX_PORT=$(${pkgs.coreutils}/bin/realpath ${config.socketDir + "/mysql.sock"})
MYSQLX_UNIX_PORT=$(${pkgs.coreutils}/bin/realpath ${config.socketDir + "/mysqlx.sock"})
export MYSQL_HOME
export MYSQL_UNIX_PORT
@ -193,7 +199,7 @@ in
configureTimezones = ''
# Start a temp database with the default-time-zone to import tz data
# and hide the temp database from the configureScript by setting a custom socket
CONFIG_SOCKET="$(${pkgs.coreutils}/bin/realpath ${config.dataDir + "/config.sock"})"
CONFIG_SOCKET="$(${pkgs.coreutils}/bin/realpath ${config.socketDir + "/config.sock"})"
nohup mysqld ${mysqldOptions} --socket="$CONFIG_SOCKET" --skip-networking --default-time-zone=SYSTEM &
while ! MYSQL_PWD="" mysqladmin --socket="$CONFIG_SOCKET" ping -u root --silent; do
@ -210,6 +216,9 @@ in
text = ''
set -euo pipefail
if [[ ! -d ${config.socketDir} ]]; then
mkdir -p ${config.socketDir}
fi
if [[ ! -d ${config.dataDir} || ! -f ${config.dataDir}/ibdata1 ]]; then
mkdir -p ${config.dataDir}
${initDatabaseCmd}
@ -282,7 +291,7 @@ in
readiness_probe = {
# Turns out using `--defaults-file` alone doesn't make the readiness_probe work unless `MYSQL_UNIX_PORT` is set.
# Hence the use of `--socket`.
exec.command = "${config.package}/bin/mysqladmin --socket=${config.dataDir}/mysql.sock ping -h localhost";
exec.command = "${config.package}/bin/mysqladmin --socket=${config.socketDir}/mysql.sock ping -h localhost";
initial_delay_seconds = 2;
period_seconds = 10;
timeout_seconds = 4;

View File

@ -6,25 +6,28 @@
CREATE USER foo IDENTIFIED BY 'password@123';
CREATE USER bar;
'';
ensureUsers = [
{
name = "test_database";
password = "test_database";
ensurePermissions = { "test_database.*" = "ALL PRIVILEGES"; };
}
];
};
services.mysql.m2 =
{ name, ... }:
{
enable = true;
importTimeZones = true;
socketDir = "/tmp/${name}";
settings.mysqld.port = 3308;
};
services.mysql.m3 = {
enable = true;
importTimeZones = true;
package = pkgs.mysql80;
settings.mysqld.port = 3309;
};
services.mysql.m1.ensureUsers = [
{
name = "test_database";
password = "test_database";
ensurePermissions = { "test_database.*" = "ALL PRIVILEGES"; };
}
];
services.mysql.m2 = {
enable = true;
importTimeZones = true;
settings.mysqld.port = 3308;
};
settings.processes.test =
{
command = pkgs.writeShellApplication {
@ -60,7 +63,24 @@
else
echo "time_zone_name table is correctly populated with $tz_names rows on mariadb"
fi
echo "Checking socketDir:"
socket=$(echo 'SELECT @@GLOBAL.socket' | MYSQL_PWD="" mysql -h 127.0.0.1 -u root | tail -n1)
m1socket="$(${pkgs.coreutils}/bin/realpath ${config.services.mysql.m1.dataDir + "/mysql.sock"})"
if [[ "$socket" != "$m1socket" ]]; then
echo "socket is not in $m1socket"
exit 1
else
echo "socket is in $m1socket"
fi
m2socket="$(${pkgs.coreutils}/bin/realpath ${config.services.mysql.m2.socketDir + "/mysql.sock"})"
socket=$(echo 'SELECT @@GLOBAL.socket' | MYSQL_PWD="" mysql -h 127.0.0.1 -P 3308 -u root | tail -n1)
if [[ "$socket" != "$m2socket" ]]; then
echo "socket is not in $m2socket"
exit 1
else
echo "socket is in $m2socket"
fi
echo "Checking if both foo.sql and bar.sql are executed, ignoring baz.md"
echo "SELECT * FROM information_schema.tables WHERE table_schema = 'test_database' AND table_name = 'foo' LIMIT 1;" | MYSQL_PWD="" mysql -h 127.0.0.1 -u root | grep foo
echo "SELECT * FROM information_schema.tables WHERE table_schema = 'test_database' AND table_name = 'bar' LIMIT 1;" | MYSQL_PWD="" mysql -h 127.0.0.1 -u root | grep bar