2009-10-12 19:27:57 +02:00
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
|
|
|
with pkgs.lib;
|
2009-03-06 13:27:00 +01:00
|
|
|
|
2008-01-28 15:34:29 +01:00
|
|
|
let
|
|
|
|
|
2008-06-25 23:58:51 +02:00
|
|
|
cfg = config.services.mysql;
|
|
|
|
|
2009-12-16 16:24:15 +01:00
|
|
|
mysql = cfg.package;
|
2008-06-25 23:58:51 +02:00
|
|
|
|
|
|
|
pidFile = "${cfg.pidDir}/mysqld.pid";
|
|
|
|
|
|
|
|
mysqldOptions =
|
|
|
|
"--user=${cfg.user} --datadir=${cfg.dataDir} " +
|
|
|
|
"--log-error=${cfg.logError} --pid-file=${pidFile}";
|
2008-01-28 15:34:29 +01:00
|
|
|
|
2011-07-13 22:58:48 +02:00
|
|
|
myCnf = pkgs.writeText "my.cnf"
|
|
|
|
''
|
|
|
|
[mysqld]
|
|
|
|
${optionalString (cfg.replication.role == "master" || cfg.replication.role == "slave") "log-bin=mysql-bin"}
|
|
|
|
${optionalString (cfg.replication.role == "master" || cfg.replication.role == "slave") "server-id = ${toString cfg.replication.serverId}"}
|
|
|
|
${optionalString (cfg.replication.role == "slave")
|
|
|
|
''
|
|
|
|
master-host = ${cfg.replication.masterHost}
|
|
|
|
master-user = ${cfg.replication.masterUser}
|
|
|
|
master-password = ${cfg.replication.masterPassword}
|
|
|
|
master-port = ${toString cfg.replication.masterPort}
|
|
|
|
''}
|
|
|
|
'';
|
|
|
|
|
2008-01-28 15:34:29 +01:00
|
|
|
in
|
2008-06-25 23:58:51 +02:00
|
|
|
|
2009-07-15 13:19:11 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-07-15 13:19:11 +02:00
|
|
|
services.mysql = {
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-07-15 13:19:11 +02:00
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = "
|
|
|
|
Whether to enable the MySQL server.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
2009-12-16 16:24:15 +01:00
|
|
|
package = mkOption {
|
2009-12-16 15:54:36 +01:00
|
|
|
default = pkgs.mysql;
|
|
|
|
description = "
|
|
|
|
Which MySQL derivation to use.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
2009-07-15 13:19:11 +02:00
|
|
|
port = mkOption {
|
|
|
|
default = "3306";
|
2011-09-14 20:20:50 +02:00
|
|
|
description = "Port of MySQL";
|
2009-07-15 13:19:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
default = "mysql";
|
|
|
|
description = "User account under which MySQL runs";
|
|
|
|
};
|
|
|
|
|
|
|
|
dataDir = mkOption {
|
2009-10-12 13:30:43 +02:00
|
|
|
default = "/var/mysql"; # !!! should be /var/db/mysql
|
2009-07-15 13:19:11 +02:00
|
|
|
description = "Location where MySQL stores its table files";
|
|
|
|
};
|
|
|
|
|
|
|
|
logError = mkOption {
|
|
|
|
default = "/var/log/mysql_err.log";
|
|
|
|
description = "Location of the MySQL error logfile";
|
|
|
|
};
|
|
|
|
|
|
|
|
pidDir = mkOption {
|
|
|
|
default = "/var/run/mysql";
|
|
|
|
description = "Location of the file which stores the PID of the MySQL server";
|
2011-09-14 20:20:50 +02:00
|
|
|
};
|
|
|
|
|
2009-11-18 17:19:04 +01:00
|
|
|
initialDatabases = mkOption {
|
|
|
|
default = [];
|
|
|
|
description = "List of database names and their initial schemas that should be used to create databases on the first startup of MySQL";
|
|
|
|
example = [
|
|
|
|
{ name = "foodatabase"; schema = ./foodatabase.sql; }
|
|
|
|
{ name = "bardatabase"; schema = ./bardatabase.sql; }
|
|
|
|
];
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2010-04-16 20:06:23 +02:00
|
|
|
initialScript = mkOption {
|
|
|
|
default = null;
|
|
|
|
description = "A file containing SQL statements to be executed on the first startup. Can be used for granting certain permissions on the database";
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2010-04-16 20:06:23 +02:00
|
|
|
rootPassword = mkOption {
|
|
|
|
default = null;
|
2011-09-14 20:20:50 +02:00
|
|
|
description = "Path to a file containing the root password, modified on the first startup. Not specifying a root password will leave the root password empty.";
|
2010-04-16 20:06:23 +02:00
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2011-07-13 22:58:48 +02:00
|
|
|
replication = {
|
|
|
|
role = mkOption {
|
|
|
|
default = "none";
|
|
|
|
description = "Role of the MySQL server instance. Can be either: master, slave or none";
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2011-07-13 22:58:48 +02:00
|
|
|
serverId = mkOption {
|
|
|
|
default = 1;
|
|
|
|
description = "Id of the MySQL server instance. This number must be unique for each instance";
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2011-07-13 22:58:48 +02:00
|
|
|
masterHost = mkOption {
|
|
|
|
description = "Hostname of the MySQL master server";
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2011-07-13 22:58:48 +02:00
|
|
|
masterUser = mkOption {
|
|
|
|
description = "Username of the MySQL replication user";
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2011-07-13 22:58:48 +02:00
|
|
|
masterPassword = mkOption {
|
|
|
|
description = "Password of the MySQL replication user";
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2011-07-13 22:58:48 +02:00
|
|
|
masterPort = mkOption {
|
|
|
|
default = 3306;
|
|
|
|
description = "Port number on which the MySQL master server runs";
|
|
|
|
};
|
|
|
|
};
|
2009-07-15 13:19:11 +02:00
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-07-15 13:19:11 +02:00
|
|
|
};
|
|
|
|
|
2009-03-06 13:27:00 +01:00
|
|
|
|
2009-07-15 13:19:11 +02:00
|
|
|
###### implementation
|
2008-06-25 23:58:51 +02:00
|
|
|
|
2009-07-15 13:19:11 +02:00
|
|
|
config = mkIf config.services.mysql.enable {
|
|
|
|
|
|
|
|
users.extraUsers = singleton
|
2009-03-06 13:27:00 +01:00
|
|
|
{ name = "mysql";
|
|
|
|
description = "MySQL server user";
|
2009-07-15 13:19:11 +02:00
|
|
|
};
|
2009-03-06 13:27:00 +01:00
|
|
|
|
2009-07-15 13:19:11 +02:00
|
|
|
environment.systemPackages = [mysql];
|
2009-03-06 13:27:00 +01:00
|
|
|
|
2009-10-12 20:09:34 +02:00
|
|
|
jobs.mysql =
|
2009-10-12 19:27:57 +02:00
|
|
|
{ description = "MySQL server";
|
2008-06-25 23:58:51 +02:00
|
|
|
|
2010-06-13 22:59:49 +02:00
|
|
|
startOn = "filesystem";
|
2008-06-25 23:58:51 +02:00
|
|
|
|
2009-10-12 13:30:43 +02:00
|
|
|
preStart =
|
|
|
|
''
|
|
|
|
if ! test -e ${cfg.dataDir}/mysql; then
|
2009-03-06 13:27:00 +01:00
|
|
|
mkdir -m 0700 -p ${cfg.dataDir}
|
|
|
|
chown -R ${cfg.user} ${cfg.dataDir}
|
|
|
|
${mysql}/bin/mysql_install_db ${mysqldOptions}
|
2010-04-16 20:06:23 +02:00
|
|
|
touch /tmp/mysql_init
|
2009-03-06 13:27:00 +01:00
|
|
|
fi
|
2008-06-25 23:58:51 +02:00
|
|
|
|
2009-03-06 13:27:00 +01:00
|
|
|
mkdir -m 0700 -p ${cfg.pidDir}
|
|
|
|
chown -R ${cfg.user} ${cfg.pidDir}
|
2009-10-12 13:30:43 +02:00
|
|
|
'';
|
2008-01-28 15:34:29 +01:00
|
|
|
|
2011-07-13 22:58:48 +02:00
|
|
|
exec = "${mysql}/libexec/mysqld --defaults-extra-file=${myCnf} ${mysqldOptions}";
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-11-18 17:19:04 +01:00
|
|
|
postStart =
|
|
|
|
''
|
|
|
|
# Wait until the MySQL server is available for use
|
|
|
|
count=0
|
|
|
|
while [ ! -e /tmp/mysql.sock ]
|
|
|
|
do
|
|
|
|
if [ $count -eq 30 ]
|
|
|
|
then
|
2010-04-16 20:06:23 +02:00
|
|
|
echo "Tried 30 times, giving up..."
|
|
|
|
exit 1
|
|
|
|
fi
|
2009-11-18 17:19:04 +01:00
|
|
|
|
2010-04-16 20:06:23 +02:00
|
|
|
echo "MySQL daemon not yet started. Waiting for 1 second..."
|
|
|
|
count=$((count++))
|
|
|
|
sleep 1
|
2009-11-18 17:19:04 +01:00
|
|
|
done
|
|
|
|
|
2010-04-16 20:06:23 +02:00
|
|
|
if [ -f /tmp/mysql_init ]
|
|
|
|
then
|
|
|
|
# Create initial databases
|
|
|
|
|
2011-09-14 20:20:50 +02:00
|
|
|
${concatMapStrings (database:
|
2010-04-16 20:06:23 +02:00
|
|
|
''
|
|
|
|
if ! test -e "${cfg.dataDir}/${database.name}"; then
|
|
|
|
echo "Creating initial database: ${database.name}"
|
|
|
|
( echo "create database ${database.name};"
|
|
|
|
echo "use ${database.name};"
|
|
|
|
if [ -f "${database.schema}" ]
|
|
|
|
then
|
|
|
|
cat ${database.schema}
|
|
|
|
elif [ -d "${database.schema}" ]
|
|
|
|
then
|
|
|
|
cat ${database.schema}/mysql-databases/*.sql
|
|
|
|
fi
|
|
|
|
) | ${mysql}/bin/mysql -u root -N
|
|
|
|
fi
|
2011-09-14 20:20:50 +02:00
|
|
|
'') cfg.initialDatabases}
|
|
|
|
|
2010-04-16 20:06:23 +02:00
|
|
|
# Execute initial script
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2010-04-16 20:06:23 +02:00
|
|
|
${optionalString (cfg.initialScript != null)
|
|
|
|
''
|
|
|
|
cat ${cfg.initialScript} | ${mysql}/bin/mysql -u root -N
|
|
|
|
''}
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2010-04-16 20:06:23 +02:00
|
|
|
# Change root password
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2010-04-16 20:06:23 +02:00
|
|
|
${optionalString (cfg.rootPassword != null)
|
|
|
|
''
|
|
|
|
( echo "use mysql;"
|
|
|
|
echo "update user set Password=password('$(cat ${cfg.rootPassword})') where User='root';"
|
|
|
|
echo "flush privileges;"
|
2009-11-18 22:56:47 +01:00
|
|
|
) | ${mysql}/bin/mysql -u root -N
|
2010-04-16 20:06:23 +02:00
|
|
|
''}
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2010-04-16 20:06:23 +02:00
|
|
|
rm /tmp/mysql_init
|
|
|
|
fi
|
2009-11-18 17:19:04 +01:00
|
|
|
'';
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-11-07 13:43:32 +01:00
|
|
|
# !!! Need a postStart script to wait until mysqld is ready to
|
|
|
|
# accept connections.
|
|
|
|
|
|
|
|
extraConfig = "kill timeout 60";
|
2009-10-12 13:30:43 +02:00
|
|
|
};
|
2009-07-15 13:19:11 +02:00
|
|
|
|
2009-03-06 13:27:00 +01:00
|
|
|
};
|
2009-07-15 13:19:11 +02:00
|
|
|
|
2008-01-28 15:34:29 +01:00
|
|
|
}
|