docker-images/pluie/alpine-mysql/pre-init.d/10-db.sh

74 lines
2.3 KiB
Bash
Raw Normal View History

2016-07-25 02:56:10 +00:00
#!/bin/bash
2016-08-06 00:29:06 +00:00
# @app pluie/alpine-mysql
# @author a-Sansara https://git.pluie.org/pluie/docker-images
2016-07-25 02:56:10 +00:00
2016-08-04 01:00:33 +00:00
. /scripts/util.sh
2016-07-29 11:51:56 +00:00
function mysql.secure(){
chown mysql:mysql $1
sleep 5
2016-08-04 01:00:33 +00:00
initTitle "SECURING" "DATABASE"
2016-07-29 11:51:56 +00:00
echo "please wait."
sleep 5
rm -f $1
echo "done"
}
2016-07-25 02:56:10 +00:00
if [ ! -d "/run/mysqld" ]; then
mkdir -p /run/mysqld
chown -R mysql:mysql /run/mysqld
fi
if [ ! -d /var/lib/mysql/mysql ]; then
2016-07-29 11:51:56 +00:00
2016-08-04 01:00:33 +00:00
initTitle "Initialize" "DATABASE"
2016-07-25 02:56:10 +00:00
chown -R mysql:mysql /var/lib/mysql
2016-07-29 11:51:56 +00:00
mysql_install_db --user=mysql --verbose=1 --basedir=/usr --datadir=/var/lib/mysql --rpm > /dev/null
2016-07-25 02:56:10 +00:00
if [ -z "$MYSQL_ROOT_PASSWORD" ]; then
2016-07-25 02:56:10 +00:00
MYSQL_ROOT_PASSWORD=`pwgen -y -s 18 1`
fi
MYSQL_DATABASE=${MYSQL_DATABASE:-""}
MYSQL_USER=${MYSQL_USER:-""}
MYSQL_PASSWORD=${MYSQL_PASSWORD:-""}
tfile=`mktemp`
if [ ! -f "$tfile" ]; then
return 1
fi
2016-07-29 11:51:56 +00:00
cat <<-EOF > $tfile
UPDATE mysql.user SET password=PASSWORD('$MYSQL_ROOT_PASSWORD') WHERE user='root';
DELETE FROM mysql.user WHERE user='root' AND host NOT IN ('localhost', '127.0.0.1', '::1', '$(hostname)');
DELETE FROM mysql.user WHERE user='';
DELETE FROM mysql.db WHERE db='test' OR db='test\_%';
2016-07-25 02:56:10 +00:00
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
2016-07-29 11:51:56 +00:00
UPDATE mysql.user SET password=PASSWORD("") WHERE user='root' AND host='localhost';
2016-07-25 02:56:10 +00:00
EOF
if [ ! -z "$MYSQL_DATABASE" ]; then
echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` CHARACTER SET utf8 COLLATE utf8_general_ci;" >> $tfile
2016-07-28 12:48:29 +00:00
fi
if [ "$MYSQL_USER" != "" ] && [ "$MYSQL_USER" != 'root' ]; then
echo "
CREATE USER '$MYSQL_USER'@'localhost' IDENTIFIED BY '$MYSQL_PASSWORD';
CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD';" >> $tfile
if [ ! -z "$MYSQL_DATABASE" ]; then
echo "
GRANT ALL ON \`$MYSQL_DATABASE\`.* to '$MYSQL_USER'@'localhost' IDENTIFIED BY '$MYSQL_PASSWORD';
GRANT ALL ON \`$MYSQL_DATABASE\`.* to '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD';" >> $tfile
2016-07-25 02:56:10 +00:00
fi
fi
2016-07-29 11:51:56 +00:00
echo "FLUSH PRIVILEGES;" >> $tfile
mysql.secure $tfile &
2016-08-04 01:00:33 +00:00
initTitle "Starting" "Mysql Daemon"
2016-07-29 11:51:56 +00:00
exec /usr/bin/mysqld --user=mysql --console --init-file="$tfile"
else
2016-08-04 01:00:33 +00:00
initTitle "Skipping" "DB init"
initTitle "Starting" "Mysql Daemon"
2016-07-25 02:56:10 +00:00
fi