Skip to content

Commit

Permalink
#20 - Add MYSQL_VOLUME env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
johanjanssens committed Oct 12, 2021
1 parent daa995c commit 5c5228e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
8 changes: 7 additions & 1 deletion .env.default
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ APP_APIKEY=
## MySQL
##############

MYSQL_USER=root (default)
# The mysql user (default)
MYSQL_USER=root

# Location of the persistent data directory (default)
MYSQL_VOLUME=/var/lib/mysql

## S6
##############

S6_BEHAVIOUR_IF_STAGE2_FAILS=2
S6_SERVICES_GRACETIME=10000
S6_KILL_FINISH_MAXTIME=10000
Expand All @@ -53,6 +58,7 @@ COMPOSER_HOME=/tmp
##############

# https://xdebug.org/docs/all_settings

XDEBUG_ENABLE=0
XDEBUG_MODE=develop
XDEBUG_TRIGGER=1
Expand Down
6 changes: 5 additions & 1 deletion config/s6/cont-init.d/101-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

# Set environment defaults
MYSQL_USER=${MYSQL_USER:=root}
printf "%s" $MYSQL_USER > /var/run/s6/container_environment/MYSQL_USER
printf "%s" $MYSQL_USER > /var/run/s6/container_environment/MYSQL_USER

# Set environment defaults
MYSQL_VOLUME=${MYSQL_VOLUME:=/var/lib/mysql}
printf "%s" $MYSQL_VOLUME > /var/run/s6/container_environment/MYSQL_VOLUME
29 changes: 19 additions & 10 deletions config/s6/cont-init.d/110-mysql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,38 @@

file="${0##*/}"

MYSQL_DATA_DIR=$APP_VOLUME/mysql
MYSQL_PID_FILE=/var/run/mysqld/mysqld.pid
MYSQL_RUN_DIR=/var/run/mysqld

# Make sure run dir exists
if [[ ! -d $MYSQL_RUN_DIR ]]; then
mkdir -p $MYSQL_RUN_DIR
fi


# Remove stale MySQL PID file left behind when docker stops container
if [[ -f $MYSQL_PID_FILE ]]; then
rm -f $MYSQL_PID_FILE
fi

# Ensure that /var/run/mysqld (used for socket and lock files) is writable
chown -R mysql:mysql $MYSQL_RUN_DIR
chmod 1777 $MYSQL_RUN_DIR


# Initialize MySQL data directory (if needed)
# See https://dev.mysql.com/doc/refman/8.0/en/data-directory-initialization.html
if [[ ! -d $MYSQL_DATA_DIR ]]; then
#if [[ ! -d $MYSQL_VOLUME ]]; then

echo "[cont-init.d] ${file}: An empty or uninitialized MySQL volume is detected in ${MYSQL_DATA_DIR}"
echo "[cont-init.d] ${file}: Installing MySQL in ${MYSQL_DATA_DIR} ..."
mkdir $MYSQL_DATA_DIR
chown --reference=/var/lib/mysql $MYSQL_DATA_DIR
chmod --reference=/var/lib/mysql $MYSQL_DATA_DIR
cp -Rpf /var/lib/mysql/* $MYSQL_DATA_DIR
echo "[cont-init.d] ${file}: Installing MySQL in ${MYSQL_VOLUME} ..."
mkdir -p $MYSQL_VOLUME

fi
/usr/bin/mysqld_safe --initialize-insecure --datadir=${MYSQL_VOLUME}

#fi

# Grant or revoke passwordless remote access
/usr/bin/mysqld_safe --datadir=$MYSQL_DATA_DIR -D
/usr/bin/mysqld_safe --datadir=$MYSQL_VOLUME -D

echo "[cont-init.d] ${file}: Granting local access of MySQL database from localhost for ${MYSQL_USER}"
/usr/bin/mysql -e "
Expand Down
4 changes: 2 additions & 2 deletions config/s6/services.d/mysql/run
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ trap -x
term { mysqladmin shutdown }
}

importas -D /mnt/www APP_VOLUME APP_VOLUME
exec /usr/bin/mysqld_safe --datadir=${APP_VOLUME}/mysql
importas -D /var/lib/mysql MYSQL_VOLUME MYSQL_VOLUME
exec /usr/bin/mysqld_safe --datadir=${MYSQL_VOLUME}

0 comments on commit 5c5228e

Please sign in to comment.