php-app/mariadb/docker-entrypoint.sh

65 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
# This script will be executed every time the container spin up
# It will check if the database is properly set up:
# - main schema is present
# - anonymous login is disabled
# - remote root login is disabled
# - remote login is allowed
#
# TODO:
# - import every SQL file found into a specific volume
#
# Author: bisco <bisco@younerd.org>
set -e
if [ -z $(ls -A /var/lib/mysql) ];
then
echo "MySQL Datadir not found. Creating a new MySQL database"
mysql_install_db --user=mysql --datadir=/var/lib/mysql
echo "Starting MySQL for setup process...."
$(which mysqld_safe) --no-defaults --syslog --nowatch --pid-file=/tmp/mysqld.pid
sleep 3
echo "Disallow anonymous login...."
$(which mysql) -e "DELETE FROM mysql.global_priv WHERE User='';"
echo "Disallow remote root login...."
$(which mysql) -e "DELETE FROM mysql.global_priv WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');"
echo "Deleting test database...."
$(which mysql) -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\_%'"
echo "Creating new database...."
$(which mysql) -e "CREATE DATABASE IF NOT EXISTS \`${MYSQL_DATABASE}\` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';"
$(which mysql) -e "GRANT ALL ON \`${MYSQL_DATABASE}\`.* TO '${MYSQL_USER}'@'%' IDENTIFIED BY '${MYSQL_PASSWORD}';"
echo "Flushing all privileges...."
$(which mysql) -e "FLUSH PRIVILEGES;"
echo "Enabling remote login...."
sed -i 's/^skip-networking/#skip-networking/g' /etc/my.cnf.d/mariadb-server.cnf
sed -i 's/#bind-address/bind-address/g' /etc/my.cnf.d/mariadb-server.cnf
echo "Stopping MySQL for setup process...."
kill -9 $(cat /tmp/mysqld.pid)
echo "Starting MySQL with production environment...."
$(which mysqld_safe) --syslog
else
echo "MySQL Datadir found. Skipping creation..."
echo "Enabling remote login...."
sed -i 's/^skip-networking/#skip-networking/g' /etc/my.cnf.d/mariadb-server.cnf
sed -i 's/#bind-address/bind-address/g' /etc/my.cnf.d/mariadb-server.cnf
echo "Starting MySQL with production environment...."
$(which mysqld_safe) --syslog
fi