From ea13d8e28fd08bd818f7b2c03446f1912c8e8f7c Mon Sep 17 00:00:00 2001 From: Pavol Mederly Date: Fri, 12 Oct 2018 23:45:41 +0200 Subject: [PATCH] Add $AFTER_FIRST_TIME_SQL parameter It allows a user to add custom SQL commands to the file that is executed during database initialization. Useful e.g. to provide some initial content. Also fixed hardwired directory in my.cnf. And a minor start.sh code cleanup. --- conf/my.cnf | 2 +- container_files/bin/fix-all-permissions.sh | 8 ++++++ container_files/bin/start.sh | 32 +++++++++------------- 3 files changed, 22 insertions(+), 20 deletions(-) create mode 100755 container_files/bin/fix-all-permissions.sh diff --git a/conf/my.cnf b/conf/my.cnf index ab171a5..839843c 100644 --- a/conf/my.cnf +++ b/conf/my.cnf @@ -11,7 +11,7 @@ # this is only for the mysqld standalone daemon [mysqld] skip-character-set-client-handshake -datadir=/var/lib/mysqlmounted +datadir=${MYSQL_DATADIR} socket=/var/lib/mysql/mysql.sock character-set-server=utf8 #default-character-set=utf8 diff --git a/container_files/bin/fix-all-permissions.sh b/container_files/bin/fix-all-permissions.sh new file mode 100755 index 0000000..9dee849 --- /dev/null +++ b/container_files/bin/fix-all-permissions.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +echo "Fixing Permissions" +chown -R mysql:mysql $MYSQL_DATADIR +/opt/bin/fix-permissions.sh $MYSQL_DATADIR +/opt/bin/fix-permissions.sh /var/log/mariadb/ +/opt/bin/fix-permissions.sh /var/run/ +echo "Done Fixing Permissions" diff --git a/container_files/bin/start.sh b/container_files/bin/start.sh index 5c4530e..7ce5723 100755 --- a/container_files/bin/start.sh +++ b/container_files/bin/start.sh @@ -16,7 +16,8 @@ if [ -e "/tmp/firsttimerunning" ]; then fi echo "Setting DataDir: $MYSQL_DATADIR" >> $log - + sed -e "s=\${MYSQL_DATADIR}=$MYSQL_DATADIR=" < /etc/my.cnf > /etc/my.cnf + if [[ $CREATE_NEW_DATABASE == 1 || ($CREATE_NEW_DATABASE == if_needed && ( ! -d $MYSQL_DATADIR || -z $(ls -A $MYSQL_DATADIR) ) ) ]]; then echo "Installing MariaDB" >> $log @@ -26,15 +27,15 @@ if [ -e "/tmp/firsttimerunning" ]; then echo >&2 ' Did you forget to add -e MYSQL_ROOT_PASSWORD=... ?' >> $log exit 1 fi - + echo 'Running mysql_install_db ...' >> $log mysql_install_db --defaults-file=/etc/my.cnf --datadir="$MYSQL_DATADIR" >> $log echo 'Finished mysql_install_db' >> $log - + # These statements _must_ be on individual lines, and _must_ end with # semicolons (no line breaks or comments are permitted). # TODO proper SQL escaping on ALL the things D: - + tempSqlFile='/tmp/mysql-first-time.sql' echo "DELETE FROM mysql.user ;" > $tempSqlFile echo "CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;" >> $tempSqlFile @@ -43,7 +44,6 @@ if [ -e "/tmp/firsttimerunning" ]; then echo "GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;" >> $tempSqlFile echo "DROP DATABASE IF EXISTS test ;" >> $tempSqlFile - if [ "$MYSQL_DATABASE" != "" ]; then echo "flush privileges;" >> "$tempSqlFile" echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" >> "$tempSqlFile" @@ -54,29 +54,23 @@ if [ -e "/tmp/firsttimerunning" ]; then echo "GRANT ALL ON $MYSQL_DATABASE.* TO '$MYSQL_USER'@'$MYSQL_DATABASE.%_i2network' IDENTIFIED BY '$MYSQL_PASSWORD' ;" >> "$tempSqlFile" echo "GRANT ALL ON $MYSQL_DATABASE.* TO '$MYSQL_USER'@'$MYSQL_DATABASE.%_i2network' IDENTIFIED BY '$MYSQL_PASSWORD' ;" >> "$tempSqlFile" fi - + echo 'FLUSH PRIVILEGES ;' >> "$tempSqlFile" # [jvf] mysqld_safe unknown option '--character-set-server=utf8' [FIXME] #echo "character-set-server = utf8" >> /etc/my.cnf #echo "collation-server = utf8_unicode_ci" >> /etc/my.cnf #echo "" >> /etc/my.cnf - - echo "Fixing Permissions" >> $log - chown -R mysql:mysql $MYSQL_DATADIR - /opt/bin/fix-permissions.sh $MYSQL_DATADIR >> $log - /opt/bin/fix-permissions.sh /var/log/mariadb/ >> $log - /opt/bin/fix-permissions.sh /var/run/ >> $log - echo "Done Fixing Permissions" >> $log + + if [ -e "$AFTER_FIRST_TIME_SQL" ]; then + cat $AFTER_FIRST_TIME_SQL >> $tempSqlFile + fi + + /opt/bin/fix-all-permissions.sh >> $log rm -f /tmp/firsttimerunning /usr/bin/mysqld_safe --init-file="$tempSqlFile" --datadir="$MYSQL_DATADIR" else echo "Not Creating a MariaDB - Using Existing from DataDir: $MYSQL_DATADIR" >> $log - echo "Fixing Permissions" >> $log - chown -R mysql:mysql $MYSQL_DATADIR - /opt/bin/fix-permissions.sh $MYSQL_DATADIR >> $log - /opt/bin/fix-permissions.sh /var/log/mariadb/ >> $log - /opt/bin/fix-permissions.sh /var/run/ >> $log - echo "Done Fixing Permissions" >> $log + /opt/bin/fix-all-permissions.sh >> $log rm -f /tmp/firsttimerunning /usr/bin/mysqld_safe --datadir="$MYSQL_DATADIR" fi