aurweb/docker/mariadb-entrypoint.sh
Kevin Morris bace345da4
Docker: support both '%' and 'localhost' in mariadb
This is needed to be able to reach the mysql service from
other hosts or through localhost. Handling both cases here
means that we can support both localhost access and host access.

Signed-off-by: Kevin Morris <kevr@0cost.org>
2021-08-08 21:46:46 -07:00

38 lines
1.2 KiB
Bash
Executable file

#!/bin/bash
set -eou pipefail
MYSQL_DATA=/var/lib/mysql
[[ -z "$DB_HOST" ]] && DB_HOST="localhost"
mariadb-install-db --user=mysql --basedir=/usr --datadir=$MYSQL_DATA
# Start it up.
mysqld_safe --datadir=$MYSQL_DATA --skip-networking &
while ! mysqladmin ping 2>/dev/null; do
sleep 1s
done
# Configure databases.
DATABASE="aurweb" # Persistent database for fastapi/php-fpm.
TEST_DB="aurweb_test" # Test database (ephemereal).
echo "Taking care of primary database '${DATABASE}'..."
mysql -u root -e "CREATE USER IF NOT EXISTS 'aur'@'localhost' IDENTIFIED BY 'aur';"
mysql -u root -e "CREATE USER IF NOT EXISTS 'aur'@'%' IDENTIFIED BY 'aur';"
mysql -u root -e "CREATE DATABASE IF NOT EXISTS $DATABASE;"
mysql -u root -e "GRANT ALL ON ${DATABASE}.* TO 'aur'@'localhost';"
mysql -u root -e "GRANT ALL ON ${DATABASE}.* TO 'aur'@'%';"
# Drop and create our test database.
echo "Dropping test database '$TEST_DB'..."
mysql -u root -e "DROP DATABASE IF EXISTS $TEST_DB;"
mysql -u root -e "CREATE DATABASE $TEST_DB;"
mysql -u root -e "GRANT ALL ON ${TEST_DB}.* TO 'aur'@'localhost';"
mysql -u root -e "GRANT ALL ON ${TEST_DB}.* TO 'aur'@'%';"
echo "Created new '$TEST_DB'!"
mysqladmin -uroot shutdown
exec "$@"