mysql: fix replication tests (#17174)

Eliminate race condition in replication test
Remove replication configuration from standalone test
Improve mysql command syntax consistency

Changed files
+9 -10
nixos
modules
services
databases
tests
+2 -1
nixos/modules/services/databases/mysql.nix
···
# Set up the replication master
( 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}';"
-
echo "update user set Password=password('${cfg.replication.masterPassword}') where User='${cfg.replication.masterUser}';"
) | ${mysql}/bin/mysql -u root -N
''}
+7 -8
nixos/tests/mysql-replication.nix
···
services.mysql.enable = true;
services.mysql.package = pkgs.mysql;
services.mysql.replication.role = "master";
+
services.mysql.replication.slaveHost = "%";
+
services.mysql.replication.masterUser = replicateUser;
+
services.mysql.replication.masterPassword = replicatePassword;
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}'@'%';
-
'';
networking.firewall.allowedTCPPorts = [ 3306 ];
};
···
};
testScript = ''
-
startAll;
-
+
$master->start;
$master->waitForUnit("mysql");
-
$master->waitForUnit("mysql");
+
$slave1->start;
+
$slave2->start;
+
$slave1->waitForUnit("mysql");
$slave2->waitForUnit("mysql");
$slave2->sleep(100); # Hopefully this is long enough!!
$slave2->succeed("echo 'use testdb; select * from tests' | mysql -u root -N | grep 4");
-1
nixos/tests/mysql.nix
···
{
services.mysql.enable = true;
-
services.mysql.replication.role = "master";
services.mysql.initialDatabases = [ { name = "testdb"; schema = ./testdb.sql; } ];
services.mysql.package = pkgs.mysql;
};