2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2009-10-12 18:27:57 +01:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2009-03-06 12:27:00 +00:00
|
|
|
|
2008-01-28 14:34:29 +00:00
|
|
|
let
|
|
|
|
|
2008-06-25 22:58:51 +01:00
|
|
|
cfg = config.services.mysql;
|
|
|
|
|
2020-08-12 02:09:41 +01:00
|
|
|
isMariaDB = lib.getName cfg.package == lib.getName pkgs.mariadb;
|
2019-11-24 17:22:28 +00:00
|
|
|
|
2008-06-25 22:58:51 +01:00
|
|
|
mysqldOptions =
|
2020-08-12 02:09:41 +01:00
|
|
|
"--user=${cfg.user} --datadir=${cfg.dataDir} --basedir=${cfg.package}";
|
2008-01-28 14:34:29 +00:00
|
|
|
|
2020-03-07 00:03:32 +00:00
|
|
|
settingsFile = pkgs.writeText "my.cnf" (
|
|
|
|
generators.toINI { listsAsDuplicateKeys = true; } cfg.settings +
|
|
|
|
optionalString (cfg.extraOptions != null) "[mysqld]\n${cfg.extraOptions}"
|
|
|
|
);
|
|
|
|
|
2008-01-28 14:34:29 +00:00
|
|
|
in
|
2008-06-25 22:58:51 +01:00
|
|
|
|
2009-07-15 12:19:11 +01:00
|
|
|
{
|
2019-12-10 01:51:19 +00:00
|
|
|
imports = [
|
2020-08-12 02:17:02 +01:00
|
|
|
(mkRemovedOptionModule [ "services" "mysql" "pidDir" ] "Don't wait for pidfiles, describe dependencies through systemd.")
|
2019-12-10 01:51:19 +00:00
|
|
|
(mkRemovedOptionModule [ "services" "mysql" "rootPassword" ] "Use socket authentication or set the password outside of the nix store.")
|
|
|
|
];
|
2009-07-15 12:19:11 +01:00
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-07-15 12:19:11 +01:00
|
|
|
services.mysql = {
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2020-05-14 12:08:41 +01:00
|
|
|
enable = mkEnableOption "MySQL server";
|
2009-07-15 12:19:11 +01:00
|
|
|
|
2009-12-16 15:24:15 +00:00
|
|
|
package = mkOption {
|
2014-02-26 12:54:04 +00:00
|
|
|
type = types.package;
|
2016-12-17 21:30:05 +00:00
|
|
|
example = literalExample "pkgs.mysql";
|
2009-12-16 14:54:36 +00:00
|
|
|
description = "
|
2017-11-14 01:08:15 +00:00
|
|
|
Which MySQL derivation to use. MariaDB packages are supported too.
|
2009-12-16 14:54:36 +00:00
|
|
|
";
|
|
|
|
};
|
|
|
|
|
2017-06-15 14:10:14 +01:00
|
|
|
bind = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = literalExample "0.0.0.0";
|
2020-08-12 02:17:02 +01:00
|
|
|
description = "Address to bind to. The default is to bind to all addresses.";
|
2017-06-15 14:10:14 +01:00
|
|
|
};
|
|
|
|
|
2009-07-15 12:19:11 +01:00
|
|
|
port = mkOption {
|
2016-10-02 21:34:29 +01:00
|
|
|
type = types.int;
|
|
|
|
default = 3306;
|
2020-08-12 02:17:02 +01:00
|
|
|
description = "Port of MySQL.";
|
2009-07-15 12:19:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
2016-10-02 21:34:29 +01:00
|
|
|
type = types.str;
|
2009-07-15 12:19:11 +01:00
|
|
|
default = "mysql";
|
2020-08-12 02:17:02 +01:00
|
|
|
description = "User account under which MySQL runs.";
|
2009-07-15 12:19:11 +01:00
|
|
|
};
|
|
|
|
|
2020-08-12 02:12:59 +01:00
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "mysql";
|
|
|
|
description = "Group under which MySQL runs.";
|
|
|
|
};
|
|
|
|
|
2009-07-15 12:19:11 +01:00
|
|
|
dataDir = mkOption {
|
2016-10-02 21:34:29 +01:00
|
|
|
type = types.path;
|
2017-05-19 23:30:51 +01:00
|
|
|
example = "/var/lib/mysql";
|
2020-08-12 02:17:02 +01:00
|
|
|
description = "Location where MySQL stores its table files.";
|
2009-07-15 12:19:11 +01:00
|
|
|
};
|
|
|
|
|
2020-03-07 00:03:32 +00:00
|
|
|
configFile = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = settingsFile;
|
|
|
|
defaultText = "settingsFile";
|
|
|
|
description = ''
|
|
|
|
Override the configuration file used by MySQL. By default,
|
|
|
|
NixOS generates one automatically from <option>services.mysql.settings</option>.
|
|
|
|
'';
|
|
|
|
example = literalExample ''
|
|
|
|
pkgs.writeText "my.cnf" '''
|
|
|
|
[mysqld]
|
|
|
|
datadir = /var/lib/mysql
|
|
|
|
bind-address = 127.0.0.1
|
|
|
|
port = 3336
|
|
|
|
|
|
|
|
!includedir /etc/mysql/conf.d/
|
|
|
|
''';
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
settings = mkOption {
|
|
|
|
type = with types; attrsOf (attrsOf (oneOf [ bool int str (listOf str) ]));
|
|
|
|
default = {};
|
|
|
|
description = ''
|
|
|
|
MySQL configuration. Refer to
|
|
|
|
<link xlink:href="https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html"/>,
|
|
|
|
<link xlink:href="https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html"/>,
|
|
|
|
and <link xlink:href="https://mariadb.com/kb/en/server-system-variables/"/>
|
|
|
|
for details on supported values.
|
|
|
|
|
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
MySQL configuration options such as <literal>--quick</literal> should be treated as
|
|
|
|
boolean options and provided values such as <literal>true</literal>, <literal>false</literal>,
|
|
|
|
<literal>1</literal>, or <literal>0</literal>. See the provided example below.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
'';
|
|
|
|
example = literalExample ''
|
|
|
|
{
|
|
|
|
mysqld = {
|
|
|
|
key_buffer_size = "6G";
|
|
|
|
table_cache = 1600;
|
|
|
|
log-error = "/var/log/mysql_err.log";
|
|
|
|
plugin-load-add = [ "server_audit" "ed25519=auth_ed25519" ];
|
|
|
|
};
|
|
|
|
mysqldump = {
|
|
|
|
quick = true;
|
|
|
|
max_allowed_packet = "16M";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2013-07-14 00:47:40 +01:00
|
|
|
extraOptions = mkOption {
|
2020-03-07 00:03:32 +00:00
|
|
|
type = with types; nullOr lines;
|
|
|
|
default = null;
|
2013-07-14 00:47:40 +01:00
|
|
|
example = ''
|
|
|
|
key_buffer_size = 6G
|
|
|
|
table_cache = 1600
|
|
|
|
log-error = /var/log/mysql_err.log
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
Provide extra options to the MySQL configuration file.
|
|
|
|
|
|
|
|
Please note, that these options are added to the
|
|
|
|
<literal>[mysqld]</literal> section so you don't need to explicitly
|
|
|
|
state it again.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-11-18 16:19:04 +00:00
|
|
|
initialDatabases = mkOption {
|
2019-03-12 17:08:23 +00:00
|
|
|
type = types.listOf (types.submodule {
|
|
|
|
options = {
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
The name of the database to create.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
schema = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
The initial schema of the database; if null (the default),
|
|
|
|
an empty database is created.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
2009-11-18 16:19:04 +00:00
|
|
|
default = [];
|
2017-05-20 22:57:07 +01:00
|
|
|
description = ''
|
|
|
|
List of database names and their initial schemas that should be used to create databases on the first startup
|
|
|
|
of MySQL. The schema attribute is optional: If not specified, an empty database is created.
|
|
|
|
'';
|
2012-01-18 15:01:44 +00:00
|
|
|
example = [
|
2013-10-23 19:06:39 +01:00
|
|
|
{ name = "foodatabase"; schema = literalExample "./foodatabase.sql"; }
|
2017-05-20 22:57:07 +01:00
|
|
|
{ name = "bardatabase"; }
|
2012-01-18 15:01:44 +00:00
|
|
|
];
|
2009-11-18 16:19:04 +00:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2010-04-16 19:06:23 +01:00
|
|
|
initialScript = mkOption {
|
2019-04-01 20:08:47 +01:00
|
|
|
type = types.nullOr types.path;
|
2010-04-16 19:06:23 +01:00
|
|
|
default = null;
|
2020-08-12 02:17:02 +01:00
|
|
|
description = "A file containing SQL statements to be executed on the first startup. Can be used for granting certain permissions on the database.";
|
2010-04-16 19:06:23 +01:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2017-09-10 16:58:52 +01:00
|
|
|
ensureDatabases = mkOption {
|
2019-03-12 17:08:23 +00:00
|
|
|
type = types.listOf types.str;
|
2017-09-10 16:58:52 +01:00
|
|
|
default = [];
|
|
|
|
description = ''
|
|
|
|
Ensures that the specified databases exist.
|
|
|
|
This option will never delete existing databases, especially not when the value of this
|
|
|
|
option is changed. This means that databases created once through this option or
|
|
|
|
otherwise have to be removed manually.
|
|
|
|
'';
|
|
|
|
example = [
|
|
|
|
"nextcloud"
|
2018-01-16 17:44:27 +00:00
|
|
|
"matomo"
|
2017-09-10 16:58:52 +01:00
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
ensureUsers = mkOption {
|
2019-03-12 17:08:23 +00:00
|
|
|
type = types.listOf (types.submodule {
|
|
|
|
options = {
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
Name of the user to ensure.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
ensurePermissions = mkOption {
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
default = {};
|
|
|
|
description = ''
|
|
|
|
Permissions to ensure for the user, specified as attribute set.
|
|
|
|
The attribute names specify the database and tables to grant the permissions for,
|
|
|
|
separated by a dot. You may use wildcards here.
|
|
|
|
The attribute values specfiy the permissions to grant.
|
|
|
|
You may specify one or multiple comma-separated SQL privileges here.
|
|
|
|
|
|
|
|
For more information on how to specify the target
|
|
|
|
and on which privileges exist, see the
|
|
|
|
<link xlink:href="https://mariadb.com/kb/en/library/grant/">GRANT syntax</link>.
|
|
|
|
The attributes are used as <code>GRANT ''${attrName} ON ''${attrValue}</code>.
|
|
|
|
'';
|
|
|
|
example = literalExample ''
|
|
|
|
{
|
|
|
|
"database.*" = "ALL PRIVILEGES";
|
|
|
|
"*.*" = "SELECT, LOCK TABLES";
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
2017-09-10 16:58:52 +01:00
|
|
|
default = [];
|
|
|
|
description = ''
|
|
|
|
Ensures that the specified users exist and have at least the ensured permissions.
|
|
|
|
The MySQL users will be identified using Unix socket authentication. This authenticates the Unix user with the
|
|
|
|
same name only, and that without the need for a password.
|
|
|
|
This option will never delete existing users or remove permissions, especially not when the value of this
|
|
|
|
option is changed. This means that users created and permissions assigned once through this option or
|
|
|
|
otherwise have to be removed manually.
|
|
|
|
'';
|
2019-03-12 17:08:23 +00:00
|
|
|
example = literalExample ''
|
|
|
|
[
|
|
|
|
{
|
|
|
|
name = "nextcloud";
|
|
|
|
ensurePermissions = {
|
|
|
|
"nextcloud.*" = "ALL PRIVILEGES";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
name = "backup";
|
|
|
|
ensurePermissions = {
|
|
|
|
"*.*" = "SELECT, LOCK TABLES";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
]
|
|
|
|
'';
|
2017-09-10 16:58:52 +01:00
|
|
|
};
|
|
|
|
|
2011-07-13 21:58:48 +01:00
|
|
|
replication = {
|
|
|
|
role = mkOption {
|
2016-10-02 21:34:29 +01:00
|
|
|
type = types.enum [ "master" "slave" "none" ];
|
2012-01-18 15:01:44 +00:00
|
|
|
default = "none";
|
2016-10-02 21:34:29 +01:00
|
|
|
description = "Role of the MySQL server instance.";
|
2012-01-18 15:01:44 +00:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2011-07-13 21:58:48 +01:00
|
|
|
serverId = mkOption {
|
2016-10-02 21:34:29 +01:00
|
|
|
type = types.int;
|
2012-01-18 15:01:44 +00:00
|
|
|
default = 1;
|
2020-08-12 02:17:02 +01:00
|
|
|
description = "Id of the MySQL server instance. This number must be unique for each instance.";
|
2012-01-18 15:01:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
masterHost = mkOption {
|
2016-10-02 21:34:29 +01:00
|
|
|
type = types.str;
|
2020-08-12 02:17:02 +01:00
|
|
|
description = "Hostname of the MySQL master server.";
|
2012-01-18 15:01:44 +00:00
|
|
|
};
|
|
|
|
|
2016-07-21 01:15:55 +01:00
|
|
|
slaveHost = mkOption {
|
2016-10-02 21:34:29 +01:00
|
|
|
type = types.str;
|
2020-08-12 02:17:02 +01:00
|
|
|
description = "Hostname of the MySQL slave server.";
|
2016-07-21 01:15:55 +01:00
|
|
|
};
|
|
|
|
|
2012-01-18 15:01:44 +00:00
|
|
|
masterUser = mkOption {
|
2016-10-02 21:34:29 +01:00
|
|
|
type = types.str;
|
2020-08-12 02:17:02 +01:00
|
|
|
description = "Username of the MySQL replication user.";
|
2012-01-18 15:01:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
masterPassword = mkOption {
|
2016-10-02 21:34:29 +01:00
|
|
|
type = types.str;
|
2020-08-12 02:17:02 +01:00
|
|
|
description = "Password of the MySQL replication user.";
|
2012-01-18 15:01:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
masterPort = mkOption {
|
2016-10-02 21:34:29 +01:00
|
|
|
type = types.int;
|
2012-01-18 15:01:44 +00:00
|
|
|
default = 3306;
|
2020-08-12 02:17:02 +01:00
|
|
|
description = "Port number on which the MySQL master server runs.";
|
2012-01-18 15:01:44 +00:00
|
|
|
};
|
2011-07-13 21:58:48 +01:00
|
|
|
};
|
2009-07-15 12:19:11 +01:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-07-15 12:19:11 +01:00
|
|
|
};
|
|
|
|
|
2009-03-06 12:27:00 +00:00
|
|
|
|
2009-07-15 12:19:11 +01:00
|
|
|
###### implementation
|
2008-06-25 22:58:51 +01:00
|
|
|
|
2009-07-15 12:19:11 +01:00
|
|
|
config = mkIf config.services.mysql.enable {
|
|
|
|
|
2020-03-07 00:03:32 +00:00
|
|
|
warnings = optional (cfg.extraOptions != null) "services.mysql.`extraOptions` is deprecated, please use services.mysql.`settings`.";
|
|
|
|
|
2017-05-19 23:30:51 +01:00
|
|
|
services.mysql.dataDir =
|
2018-07-25 21:22:54 +01:00
|
|
|
mkDefault (if versionAtLeast config.system.stateVersion "17.09" then "/var/lib/mysql"
|
2017-05-19 23:30:51 +01:00
|
|
|
else "/var/mysql");
|
|
|
|
|
2020-03-07 00:03:32 +00:00
|
|
|
services.mysql.settings.mysqld = mkMerge [
|
|
|
|
{
|
|
|
|
datadir = cfg.dataDir;
|
|
|
|
bind-address = mkIf (cfg.bind != null) cfg.bind;
|
|
|
|
port = cfg.port;
|
|
|
|
}
|
|
|
|
(mkIf (cfg.replication.role == "master" || cfg.replication.role == "slave") {
|
|
|
|
log-bin = "mysql-bin-${toString cfg.replication.serverId}";
|
|
|
|
log-bin-index = "mysql-bin-${toString cfg.replication.serverId}.index";
|
|
|
|
relay-log = "mysql-relay-bin";
|
|
|
|
server-id = cfg.replication.serverId;
|
2020-05-07 20:47:56 +01:00
|
|
|
binlog-ignore-db = [ "information_schema" "performance_schema" "mysql" ];
|
2020-03-07 00:03:32 +00:00
|
|
|
})
|
2020-05-05 08:22:52 +01:00
|
|
|
(mkIf (!isMariaDB) {
|
2020-08-12 02:54:51 +01:00
|
|
|
plugin-load-add = "auth_socket.so";
|
2020-05-05 08:22:52 +01:00
|
|
|
})
|
2020-03-07 00:03:32 +00:00
|
|
|
];
|
|
|
|
|
2020-08-12 02:12:59 +01:00
|
|
|
users.users = optionalAttrs (cfg.user == "mysql") {
|
|
|
|
mysql = {
|
|
|
|
description = "MySQL server user";
|
|
|
|
group = cfg.group;
|
|
|
|
uid = config.ids.uids.mysql;
|
|
|
|
};
|
2013-08-26 14:20:25 +01:00
|
|
|
};
|
|
|
|
|
2020-08-12 02:12:59 +01:00
|
|
|
users.groups = optionalAttrs (cfg.group == "mysql") {
|
|
|
|
mysql.gid = config.ids.gids.mysql;
|
|
|
|
};
|
2009-03-06 12:27:00 +00:00
|
|
|
|
2020-08-12 02:09:41 +01:00
|
|
|
environment.systemPackages = [ cfg.package ];
|
2009-03-06 12:27:00 +00:00
|
|
|
|
2020-03-07 00:03:32 +00:00
|
|
|
environment.etc."my.cnf".source = cfg.configFile;
|
2018-12-01 12:10:27 +00:00
|
|
|
|
2019-05-16 23:10:34 +01:00
|
|
|
systemd.tmpfiles.rules = [
|
2020-08-12 02:12:59 +01:00
|
|
|
"d '${cfg.dataDir}' 0700 '${cfg.user}' '${cfg.group}' - -"
|
|
|
|
"z '${cfg.dataDir}' 0700 '${cfg.user}' '${cfg.group}' - -"
|
2019-05-16 23:10:34 +01:00
|
|
|
];
|
|
|
|
|
2018-05-24 15:42:16 +01:00
|
|
|
systemd.services.mysql = let
|
2020-08-12 02:15:02 +01:00
|
|
|
hasNotify = isMariaDB;
|
2018-05-24 15:42:16 +01:00
|
|
|
in {
|
|
|
|
description = "MySQL Server";
|
2008-06-25 22:58:51 +01:00
|
|
|
|
2016-10-02 21:17:20 +01:00
|
|
|
after = [ "network.target" ];
|
2012-12-18 12:40:04 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2020-03-07 00:03:32 +00:00
|
|
|
restartTriggers = [ cfg.configFile ];
|
2012-12-18 12:40:04 +00:00
|
|
|
|
|
|
|
unitConfig.RequiresMountsFor = "${cfg.dataDir}";
|
2008-06-25 22:58:51 +01:00
|
|
|
|
2015-09-07 16:05:56 +01:00
|
|
|
path = [
|
|
|
|
# Needed for the mysql_install_db command in the preStart script
|
|
|
|
# which calls the hostname command.
|
|
|
|
pkgs.nettools
|
|
|
|
];
|
|
|
|
|
2020-03-20 20:19:43 +00:00
|
|
|
preStart = if isMariaDB then ''
|
|
|
|
if ! test -e ${cfg.dataDir}/mysql; then
|
2020-08-12 02:09:41 +01:00
|
|
|
${cfg.package}/bin/mysql_install_db --defaults-file=/etc/my.cnf ${mysqldOptions}
|
2020-05-14 18:34:14 +01:00
|
|
|
touch ${cfg.dataDir}/mysql_init
|
2020-03-20 20:19:43 +00:00
|
|
|
fi
|
|
|
|
'' else ''
|
2019-05-16 23:12:20 +01:00
|
|
|
if ! test -e ${cfg.dataDir}/mysql; then
|
2020-08-12 02:09:41 +01:00
|
|
|
${cfg.package}/bin/mysqld --defaults-file=/etc/my.cnf ${mysqldOptions} --initialize-insecure
|
2020-05-14 18:34:14 +01:00
|
|
|
touch ${cfg.dataDir}/mysql_init
|
2019-05-16 23:12:20 +01:00
|
|
|
fi
|
|
|
|
'';
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2020-08-12 03:04:16 +01:00
|
|
|
postStart = let
|
|
|
|
# The super user account to use on *first* run of MySQL server
|
|
|
|
superUser = if isMariaDB then cfg.user else "root";
|
|
|
|
in ''
|
|
|
|
${optionalString (!hasNotify) ''
|
|
|
|
# Wait until the MySQL server is available for use
|
|
|
|
count=0
|
|
|
|
while [ ! -e /run/mysqld/mysqld.sock ]
|
|
|
|
do
|
|
|
|
if [ $count -eq 30 ]
|
2019-07-01 02:58:27 +01:00
|
|
|
then
|
2020-08-12 03:04:16 +01:00
|
|
|
echo "Tried 30 times, giving up..."
|
|
|
|
exit 1
|
|
|
|
fi
|
2020-08-12 02:54:51 +01:00
|
|
|
|
2020-08-12 03:04:16 +01:00
|
|
|
echo "MySQL daemon not yet started. Waiting for 1 second..."
|
|
|
|
count=$((count++))
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
''}
|
|
|
|
|
|
|
|
if [ -f ${cfg.dataDir}/mysql_init ]
|
|
|
|
then
|
|
|
|
# While MariaDB comes with a 'mysql' super user account since 10.4.x, MySQL does not
|
|
|
|
# Since we don't want to run this service as 'root' we need to ensure the account exists on first run
|
|
|
|
( echo "CREATE USER IF NOT EXISTS '${cfg.user}'@'localhost' IDENTIFIED WITH ${if isMariaDB then "unix_socket" else "auth_socket"};"
|
|
|
|
echo "GRANT ALL PRIVILEGES ON *.* TO '${cfg.user}'@'localhost' WITH GRANT OPTION;"
|
|
|
|
) | ${cfg.package}/bin/mysql -u ${superUser} -N
|
|
|
|
|
|
|
|
${concatMapStrings (database: ''
|
|
|
|
# Create initial databases
|
|
|
|
if ! test -e "${cfg.dataDir}/${database.name}"; then
|
|
|
|
echo "Creating initial database: ${database.name}"
|
|
|
|
( echo 'create database `${database.name}`;'
|
|
|
|
|
|
|
|
${optionalString (database.schema != null) ''
|
|
|
|
echo 'use `${database.name}`;'
|
|
|
|
|
|
|
|
# TODO: this silently falls through if database.schema does not exist,
|
|
|
|
# we should catch this somehow and exit, but can't do it here because we're in a subshell.
|
|
|
|
if [ -f "${database.schema}" ]
|
|
|
|
then
|
|
|
|
cat ${database.schema}
|
|
|
|
elif [ -d "${database.schema}" ]
|
|
|
|
then
|
|
|
|
cat ${database.schema}/mysql-databases/*.sql
|
2019-07-01 02:58:27 +01:00
|
|
|
fi
|
|
|
|
''}
|
2020-08-12 03:04:16 +01:00
|
|
|
) | ${cfg.package}/bin/mysql -u ${superUser} -N
|
|
|
|
fi
|
|
|
|
'') cfg.initialDatabases}
|
2019-07-01 02:58:27 +01:00
|
|
|
|
2020-08-12 03:04:16 +01:00
|
|
|
${optionalString (cfg.replication.role == "master")
|
|
|
|
''
|
|
|
|
# Set up the replication master
|
2019-07-01 02:58:27 +01:00
|
|
|
|
2020-08-12 03:04:16 +01:00
|
|
|
( echo "use mysql;"
|
|
|
|
echo "CREATE USER '${cfg.replication.masterUser}'@'${cfg.replication.slaveHost}' IDENTIFIED WITH mysql_native_password;"
|
|
|
|
echo "SET PASSWORD FOR '${cfg.replication.masterUser}'@'${cfg.replication.slaveHost}' = PASSWORD('${cfg.replication.masterPassword}');"
|
|
|
|
echo "GRANT REPLICATION SLAVE ON *.* TO '${cfg.replication.masterUser}'@'${cfg.replication.slaveHost}';"
|
|
|
|
) | ${cfg.package}/bin/mysql -u ${superUser} -N
|
|
|
|
''}
|
2019-07-01 02:58:27 +01:00
|
|
|
|
2020-08-12 03:04:16 +01:00
|
|
|
${optionalString (cfg.replication.role == "slave")
|
|
|
|
''
|
|
|
|
# Set up the replication slave
|
2019-07-01 02:58:27 +01:00
|
|
|
|
2020-08-12 03:04:16 +01:00
|
|
|
( echo "stop slave;"
|
|
|
|
echo "change master to master_host='${cfg.replication.masterHost}', master_user='${cfg.replication.masterUser}', master_password='${cfg.replication.masterPassword}';"
|
|
|
|
echo "start slave;"
|
|
|
|
) | ${cfg.package}/bin/mysql -u ${superUser} -N
|
|
|
|
''}
|
2019-07-01 02:58:27 +01:00
|
|
|
|
2020-08-12 03:04:16 +01:00
|
|
|
${optionalString (cfg.initialScript != null)
|
|
|
|
''
|
|
|
|
# Execute initial script
|
|
|
|
# using toString to avoid copying the file to nix store if given as path instead of string,
|
|
|
|
# as it might contain credentials
|
|
|
|
cat ${toString cfg.initialScript} | ${cfg.package}/bin/mysql -u ${superUser} -N
|
2019-07-01 02:58:27 +01:00
|
|
|
''}
|
2016-07-21 01:15:55 +01:00
|
|
|
|
2020-08-12 03:04:16 +01:00
|
|
|
rm ${cfg.dataDir}/mysql_init
|
|
|
|
fi
|
|
|
|
|
|
|
|
${optionalString (cfg.ensureDatabases != []) ''
|
|
|
|
(
|
|
|
|
${concatMapStrings (database: ''
|
|
|
|
echo "CREATE DATABASE IF NOT EXISTS \`${database}\`;"
|
|
|
|
'') cfg.ensureDatabases}
|
|
|
|
) | ${cfg.package}/bin/mysql -N
|
|
|
|
''}
|
|
|
|
|
|
|
|
${concatMapStrings (user:
|
|
|
|
''
|
|
|
|
( echo "CREATE USER IF NOT EXISTS '${user.name}'@'localhost' IDENTIFIED WITH ${if isMariaDB then "unix_socket" else "auth_socket"};"
|
|
|
|
${concatStringsSep "\n" (mapAttrsToList (database: permission: ''
|
|
|
|
echo "GRANT ${permission} ON ${database} TO '${user.name}'@'localhost';"
|
|
|
|
'') user.ensurePermissions)}
|
|
|
|
) | ${cfg.package}/bin/mysql -N
|
|
|
|
'') cfg.ensureUsers}
|
|
|
|
'';
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = if hasNotify then "notify" else "simple";
|
|
|
|
Restart = "on-abort";
|
|
|
|
RestartSec = "5s";
|
|
|
|
# The last two environment variables are used for starting Galera clusters
|
|
|
|
ExecStart = "${cfg.package}/bin/mysqld --defaults-file=/etc/my.cnf ${mysqldOptions} $_WSREP_NEW_CLUSTER $_WSREP_START_POSITION";
|
2020-05-14 18:29:59 +01:00
|
|
|
# User and group
|
|
|
|
User = cfg.user;
|
2020-08-12 02:12:59 +01:00
|
|
|
Group = cfg.group;
|
2020-05-14 18:29:59 +01:00
|
|
|
# Runtime directory and mode
|
|
|
|
RuntimeDirectory = "mysqld";
|
|
|
|
RuntimeDirectoryMode = "0755";
|
|
|
|
# Access write directories
|
|
|
|
ReadWritePaths = [ cfg.dataDir ];
|
|
|
|
# Capabilities
|
|
|
|
CapabilityBoundingSet = "";
|
|
|
|
# Security
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
# Sandboxing
|
|
|
|
ProtectSystem = "strict";
|
|
|
|
ProtectHome = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" ];
|
|
|
|
LockPersonality = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
|
|
|
PrivateMounts = true;
|
|
|
|
# System Call Filtering
|
|
|
|
SystemCallArchitectures = "native";
|
2019-07-01 02:58:27 +01:00
|
|
|
};
|
2009-10-12 12:30:43 +01:00
|
|
|
};
|
2009-07-15 12:19:11 +01:00
|
|
|
|
2009-03-06 12:27:00 +00:00
|
|
|
};
|
2009-07-15 12:19:11 +01:00
|
|
|
|
2008-01-28 14:34:29 +00:00
|
|
|
}
|