# cat docker-entrypoint.sh
####################################################
#!/bin/sh
set -e
umask 0077
mysql=( mysql --protocol=socket -uroot )
if [ -z "$(ls -A /var/lib/mysql)" ]; then
if [ -z "${MYSQL_ROOT_PASSWORD}" ]; then
mysqld --initialize-insecure --user=mysql
else
mysqld --initialize-insecure --user=mysql
mysqld &
mysqld_pid="$!"
for i in {15..0}; do
if echo 'SELECT 1' | "${mysql[@]}" &> /dev/null; then
mysql -u root --skip-password -e "alter user root@'localhost' identified by \"${MYSQL_ROOT_PASSWORD}\";"
mysql -u root -p"${MYSQL_ROOT_PASSWORD}" -e "flush privileges;"
break
fi
echo 'MySQL init process in progress...'
sleep 1
done
if [ "$i" = 0 ]; then
echo >&2 'MySQL init process failed.'
exit 1
fi
fi
fi
if ! kill -s TERM "$mysqld_pid" || ! wait "$mysqld_pid"; then
echo >&2 'MySQL init process failed.'
exit 1
fi
exec "$@"
#####################################################