#!/bin/sh if [ ! -d /run/mysqld ]; then echo "/run/mysqld not found so creating one" mkdir -p /run/mysqld fi chown mysql:mysql /run/mysqld if [ ! -d $DATADIR ]; then echo "initializing db because $DATADIR is missing" echo "creating $DATADIR and ensuring permissions" mkdir -p $DATADIR chown mysql:mysql $DATADIR echo "installing db" su-exec mysql:mysql mysql_install_db --force --datadir=$DATADIR echo "pushing initialization data into server" su-exec mysql:mysql mysqld --bind-address=127.0.0.1 --datadir=$DATADIR & pid="$!" for i in {30..0}; do if echo 'SELECT 1' | mysql &> /dev/null; then break fi echo 'MySQL init process in progress...' sleep 5 done envsubst "`printf '${%s} ' $(sh -c "env|cut -d'=' -f1")`" < /etc/templates/dbinit.sql > /tmp/dbinit.sql cat /tmp/dbinit.sql | mysql if ! kill -s TERM "$pid" || ! wait "$pid"; then echo >&2 'MySQL init process failed.' exit 1 fi rm /tmp/dbinit.sql fi