···433433 done
434434 ''}
435435436436+ ${lib.optionalString isMariaDB ''
437437+ # If MariaDB is used in an Galera cluster, we have to check if the sync is done,
438438+ # or it will fail to init the database while joining, so we get in an broken non recoverable state
439439+ # so we wait until we have an synced state
440440+ 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
441441+ echo "Galera cluster detected, waiting for node to be synced..."
442442+ while true; do
443443+ STATE=$(${cfg.package}/bin/mysql -u ${superUser} -N -e "SHOW STATUS LIKE 'wsrep_local_state_comment'" | ${lib.getExe' pkgs.gawk "awk"} '{print $2}')
444444+ if [ "$STATE" = "Synced" ]; then
445445+ echo "Node is synced"
446446+ break
447447+ else
448448+ echo "Current state: $STATE - Waiting for 1 second..."
449449+ sleep 1
450450+ fi
451451+ done
452452+ fi
453453+ ''}
454454+436455 if [ -f ${cfg.dataDir}/mysql_init ]
437456 then
438457 # While MariaDB comes with a 'mysql' super user account since 10.4.x, MySQL does not
···447466 # Create initial databases
448467 if ! test -e "${cfg.dataDir}/${database.name}"; then
449468 echo "Creating initial database: ${database.name}"
450450- ( echo 'create database `${database.name}`;'
469469+ ( echo 'CREATE DATABASE IF NOT EXISTS `${database.name}`;'
451470452471 ${lib.optionalString (database.schema != null) ''
453453- echo 'use `${database.name}`;'
472472+ echo 'USE `${database.name}`;'
454473455474 # TODO: this silently falls through if database.schema does not exist,
456475 # we should catch this somehow and exit, but can't do it here because we're in a subshell.
···469488 ${lib.optionalString (cfg.replication.role == "master") ''
470489 # Set up the replication master
471490472472- ( echo "use mysql;"
491491+ ( echo "USE mysql;"
473492 echo "CREATE USER '${cfg.replication.masterUser}'@'${cfg.replication.slaveHost}' IDENTIFIED WITH mysql_native_password;"
474493 echo "SET PASSWORD FOR '${cfg.replication.masterUser}'@'${cfg.replication.slaveHost}' = PASSWORD('${cfg.replication.masterPassword}');"
475494 echo "GRANT REPLICATION SLAVE ON *.* TO '${cfg.replication.masterUser}'@'${cfg.replication.slaveHost}';"
···479498 ${lib.optionalString (cfg.replication.role == "slave") ''
480499 # Set up the replication slave
481500482482- ( echo "stop slave;"
483483- echo "change master to master_host='${cfg.replication.masterHost}', master_user='${cfg.replication.masterUser}', master_password='${cfg.replication.masterPassword}';"
484484- echo "start slave;"
501501+ ( echo "STOP SLAVE;"
502502+ echo "CHANGE MASTER TO MASTER_HOST='${cfg.replication.masterHost}', MASTER_USER='${cfg.replication.masterUser}', MASTER_PASSWORD='${cfg.replication.masterPassword}';"
503503+ echo "START SLAVE;"
485504 ) | ${cfg.package}/bin/mysql -u ${superUser} -N
486505 ''}
487506