Added MySQL replication support + 2 MySQL testcases (including replication)

svn path=/nixos/trunk/; revision=27771
This commit is contained in:
Sander van der Burg 2011-07-13 20:58:48 +00:00
parent d7c4900420
commit c630e52873
5 changed files with 135 additions and 1 deletions

View File

@ -14,6 +14,20 @@ let
"--user=${cfg.user} --datadir=${cfg.dataDir} " +
"--log-error=${cfg.logError} --pid-file=${pidFile}";
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}
''}
'';
in
{
@ -81,6 +95,35 @@ in
default = null;
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.";
};
replication = {
role = mkOption {
default = "none";
description = "Role of the MySQL server instance. Can be either: master, slave or none";
};
serverId = mkOption {
default = 1;
description = "Id of the MySQL server instance. This number must be unique for each instance";
};
masterHost = mkOption {
description = "Hostname of the MySQL master server";
};
masterUser = mkOption {
description = "Username of the MySQL replication user";
};
masterPassword = mkOption {
description = "Password of the MySQL replication user";
};
masterPort = mkOption {
default = 3306;
description = "Port number on which the MySQL master server runs";
};
};
};
};
@ -115,7 +158,7 @@ in
chown -R ${cfg.user} ${cfg.pidDir}
'';
exec = "${mysql}/libexec/mysqld ${mysqldOptions}";
exec = "${mysql}/libexec/mysqld --defaults-extra-file=${myCnf} ${mysqldOptions}";
postStart =
''

View File

@ -14,6 +14,8 @@ with import ../lib/testing.nix { inherit nixpkgs system; };
kde4 = makeTest (import ./kde4.nix);
login = makeTest (import ./login.nix);
mpich = makeTest (import ./mpich.nix);
mysql = makeTest (import ./mysql.nix);
mysql_replication = makeTest (import ./mysql-replication.nix);
nat = makeTest (import ./nat.nix);
nfs = makeTest (import ./nfs.nix);
openssh = makeTest (import ./openssh.nix);

View File

@ -0,0 +1,57 @@
{ pkgs, ... }:
let
replicateUser = "replicate";
replicatePassword = "secret";
in
{
nodes = {
master =
{ pkgs, config, ... }:
{
services.mysql.enable = true;
services.mysql.replication.role = "master";
services.mysql.initialDatabases = [ { name = "testdb"; schema = ./testdb.sql; } ];
services.mysql.initialScript = pkgs.writeText "initmysql"
''
create user '${replicateUser}'@'%' identified by '${replicatePassword}';
grant replication slave on *.* to '${replicateUser}'@'%';
'';
};
slave1 =
{ pkgs, config, nodes, ... }:
{
services.mysql.enable = true;
services.mysql.replication.role = "slave";
services.mysql.replication.serverId = 2;
services.mysql.replication.masterHost = "${nodes.master.config.networking.hostName}";
services.mysql.replication.masterUser = replicateUser;
services.mysql.replication.masterPassword = replicatePassword;
};
slave2 =
{ pkgs, config, nodes, ... }:
{
services.mysql.enable = true;
services.mysql.replication.role = "slave";
services.mysql.replication.serverId = 3;
services.mysql.replication.masterHost = "${nodes.master.config.networking.hostName}";
services.mysql.replication.masterUser = replicateUser;
services.mysql.replication.masterPassword = replicatePassword;
};
};
testScript = ''
startAll;
$master->waitForJob("mysql");
$master->waitForJob("mysql");
$slave2->waitForJob("mysql");
$slave2->sleep(100); # Hopefully this is long enough!!
$slave2->mustSucceed("echo 'use testdb; select * from tests' | mysql -u root -N | grep 4");
'';
}

22
tests/mysql.nix Normal file
View File

@ -0,0 +1,22 @@
{ pkgs, ... }:
{
nodes = {
master =
{ pkgs, config, ... }:
{
services.mysql.enable = true;
services.mysql.replication.role = "master";
services.mysql.initialDatabases = [ { name = "testdb"; schema = ./testdb.sql; } ];
};
};
testScript = ''
startAll;
$master->waitForJob("mysql");
$master->sleep(10); # Hopefully this is long enough!!
$master->mustSucceed("echo 'use testdb; select * from tests' | mysql -u root -N | grep 4");
'';
}

10
tests/testdb.sql Normal file
View File

@ -0,0 +1,10 @@
create table tests
( Id INTEGER NOT NULL,
Name VARCHAR(255) NOT NULL,
primary key(Id)
);
insert into tests values (1, 'a');
insert into tests values (2, 'b');
insert into tests values (3, 'c');
insert into tests values (4, 'd');