From 27d0d2927eca16ca02acdf78d3c505278845b6d7 Mon Sep 17 00:00:00 2001 From: Sander van der Burg Date: Wed, 18 Nov 2009 16:19:04 +0000 Subject: [PATCH] Added initialDatabases option to the MySQL service. This is useful for e.g. automatically intialing databases in a test VM svn path=/nixos/branches/upstart-0.6/; revision=18437 --- modules/services/databases/mysql.nix | 40 +++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/modules/services/databases/mysql.nix b/modules/services/databases/mysql.nix index 230d36dfe91..50d00c0e5ba 100644 --- a/modules/services/databases/mysql.nix +++ b/modules/services/databases/mysql.nix @@ -55,7 +55,15 @@ in default = "/var/run/mysql"; description = "Location of the file which stores the PID of the MySQL server"; }; - + + initialDatabases = mkOption { + default = []; + description = "List of database names and their initial schemas that should be used to create databases on the first startup of MySQL"; + example = [ + { name = "foodatabase"; schema = ./foodatabase.sql; } + { name = "bardatabase"; schema = ./bardatabase.sql; } + ]; + }; }; }; @@ -90,6 +98,36 @@ in ''; exec = "${mysql}/libexec/mysqld ${mysqldOptions}"; + + postStart = + '' + # Wait until the MySQL server is available for use + count=0 + while [ ! -e /tmp/mysql.sock ] + do + if [ $count -eq 30 ] + then + echo "Tried 30 times, giving up..." + exit 1 + fi + + echo "MySQL daemon not yet started. Waiting for 1 second..." + count=$((count++)) + sleep 1 + done + + # Create initial databases + + ${concatMapStrings (database: + '' + if ! test -e "${cfg.dataDir}/${database.name}"; then + echo "Creating initial database: ${database.name}" + ( echo "create database ${database.name};" + echo "use ${database.name};" + cat ${database.schema} ) | ${mysql}/bin/mysql -u root -N + fi + '') cfg.initialDatabases} + ''; # !!! Need a postStart script to wait until mysqld is ready to # accept connections.