ab-mariadb-alpine/entrypoint.sh

109 lines
3.8 KiB
Bash
Raw Normal View History

2020-11-27 01:38:34 -07:00
#!/bin/sh
#
### entrypoint script for mariadb container
#
# instantiate variables
sqlCmd='/tmp/cmd.sql'
# generate root password if not specified
if [ -z "$MYSQL_ROOT_PASSWORD" ]; then
MYSQL_ROOT_PASSWORD="$( head /dev/urandom | tr -dc A-Za-z0-9 | head -c32 )"
export MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD
fi
# change mysql UID & GID
sed -i "s/mysql:x:100:101/mysql:x:${MYSQL_UID}:${MYSQL_GID}/" /etc/passwd
sed -i "s/mysql:x:101/mysql:x:${MYSQL_GID}/" /etc/group
chown -R mysql:mysql /var/lib/mysql
# execute pre-init scripts: /docker-entrypoint-preinit.d/*.sh
for f in /docker-entrypoint-preinit.d/*.sh; do
if [ -s "$f" ]; then
printf "PRE-INIT: Executing %s\n" "$f"
/bin/sh "$f"
fi
done
# create socket file
mkdir -p /run/mysqld > /dev/null 2>&1
touch /run/mysqld/mysqld.dock
chown -R mysql:mysql /run/mysqld
# create database if one does not already exist
if [ -z "$(ls -A /var/lib/mysql/ 2> /dev/null)" ]; then
# create SQL cmd file
touch "$sqlCmd"
# create system tables
printf "DB-CREATE: Setting up mySQL system tables\n"
mysql_install_db --user=mysql --ldata=/var/lib/mysql > /dev/null
# statement to create new SQL database
printf "DB-CREATE: Generating SQL database create statement for '%s'\n" "$MYSQL_DATABASE"
printf "CREATE DATABASE IF NOT EXISTS '%s' CHARACTER SET %s COLLATE %s;" "$MYSQL_DATABASE" "$MYSQL_CHARSET" "$MYSQL_COLLATION" >> "$sqlCmd"
2020-11-27 01:38:34 -07:00
# statements to:
# cleanup permissions:
# leave root@localhost as root-account integrated,
# add root@% with password authentication
# create SQL user if requested
# remove 'test' table
printf 'USE mysql;' >> "$sqlCmd"
printf 'FLUSH PRIVILEGES;' >> "$sqlCmd"
2020-11-27 01:38:34 -07:00
printf "DB-CREATE: Generating SQL permissions statement for 'root@%%'\n"
printf "GRANT ALL ON *.* TO 'root'@'%%' IDENTIFIED BY '%s' WITH GRANT OPTION;" "$MYSQL_ROOT_PASSWORD" >> "$sqlCmd"
2020-11-27 01:38:34 -07:00
if [ -n "$MYSQL_USER" ] && [ -n "$MYSQL_PASSWORD" ]; then
printf "DB-CREATE: Generating SQL permissions statement for '%s'\n" "$MYSQL_USER"
printf "GRANT ALL ON '%s'.* TO '%s'@'%%' IDENTIFIED BY '%s';" "$MYSQL_DATABASE" "$MYSQL_USER" "$MYSQL_PASSWORD" >> "$sqlCmd"
2020-11-27 01:38:34 -07:00
fi
2020-11-27 01:57:25 -07:00
printf "DB-CREATE: Generating statement to drop 'test' table\n"
printf 'DROP DATABASE IF EXISTS test;' >> "$sqlCmd"
printf 'FLUSH PRIVILEGES;' >> "$sqlCmd"
2020-11-27 01:38:34 -07:00
# execute statements against mariadb and cleanup
printf "DB-CREATE: Bootstrapping mySQL database\n"
mysqld --user=mysql --bootstrap --verbose=0 --skip-name-resolve --skip-networking=0 < "$sqlCmd"
rm -f "$sqlCmd"
else
# files exist, ignore the request to create a database
printf "DB-CREATE: NOT creating %s\n" "$MYSQL_DATABASE"
printf "DB-CREATE: Using existing database\n"
fi
# process supplied SQL files in /docker-entrypoint-initdb.d/*.(sql|sql.gz)
for f in /docker-entrypoint-initdb.d/*.sh; do
case "$f" in
*.sql)
if [ -s "$f" ]; then
printf "IMPORT-SQL: Importing %s\n" "$f"
mysqld --user=mysql --bootstrap --verbose=0 --skip-name-resolve --skip-networking=0 < "$f"
printf "\n"
fi
;;
*.sql.gz)
if [ -s "$f" ]; then
printf "IMPORT-SQL: Importing %s\n" "$f"
gunzip -c "$f" | mysqld --user=mysql --bootstrap --verbose=0 --skip-name-resolve --skip-networking=0
2020-11-27 01:38:34 -07:00
printf "\n"
fi
;;
*)
printf "IMPORT-SQL: Cannot import %s\n" "$f"
;;
esac
done
# execute post-init scripts: /docker-entrypoint-postinit.d/*.sh
for f in /docker-entrypoint-postinit.d/*.sh; do
if [ -s "$f" ]; then
printf "POST-INIT: Executing %s\n" "$f"
/bin/sh "$f"
fi
done
# execute commands passed to this container
printf "\nInitialization complete... Container ready...\n"
exec "$@"
#EOF