Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport release-24.11] nixos/services.mysql: fix wait for galera cluster sync to be done #388828

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions nixos/modules/services/databases/mysql.nix
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,25 @@ in
done
''}

${lib.optionalString isMariaDB ''
# If MariaDB is used in an Galera cluster, we have to check if the sync is done,
# or it will fail to init the database while joining, so we get in an broken non recoverable state
# so we wait until we have an synced state
if ${cfg.package}/bin/mysql -u ${superUser} -N -e "SHOW VARIABLES LIKE 'wsrep_on'" 2>/dev/null | ${lib.getExe' pkgs.gnugrep "grep"} -q 'ON'; then
echo "Galera cluster detected, waiting for node to be synced..."
while true; do
STATE=$(${cfg.package}/bin/mysql -u ${superUser} -N -e "SHOW STATUS LIKE 'wsrep_local_state_comment'" | ${lib.getExe' pkgs.gawk "awk"} '{print $2}')
if [ "$STATE" = "Synced" ]; then
echo "Node is synced"
break
else
echo "Current state: $STATE - Waiting for 1 second..."
sleep 1
fi
done
fi
''}

if [ -f ${cfg.dataDir}/mysql_init ]
then
# While MariaDB comes with a 'mysql' super user account since 10.4.x, MySQL does not
Expand All @@ -404,10 +423,10 @@ in
# Create initial databases
if ! test -e "${cfg.dataDir}/${database.name}"; then
echo "Creating initial database: ${database.name}"
( echo 'create database `${database.name}`;'
( echo 'CREATE DATABASE IF NOT EXISTS `${database.name}`;'

${lib.optionalString (database.schema != null) ''
echo 'use `${database.name}`;'
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.
Expand Down Expand Up @@ -438,9 +457,9 @@ in
''
# Set up the replication slave

( 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;"
( 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
''}

Expand Down