2014-11-24 15:31:55 -08:00
#!/bin/bash
2016-01-26 09:22:48 -08:00
set -eo pipefail
2016-09-30 14:14:20 -07:00
shopt -s nullglob
2014-11-24 15:31:55 -08:00
2015-08-11 16:39:56 -07:00
# if command starts with an option, prepend mysqld
2015-01-29 16:18:59 -07:00
if [ " ${ 1 : 0 : 1 } " = '-' ] ; then
set -- mysqld " $@ "
fi
2016-02-24 15:51:13 -08:00
# skip setup if they want an option that stops mysqld
wantHelp =
for arg; do
case " $arg " in
-'?' | --help| --print-defaults| -V| --version)
wantHelp = 1
break
; ;
esac
done
2016-11-23 10:35:50 -08:00
# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env( ) {
local var = " $1 "
local fileVar = " ${ var } _FILE "
local def = " ${ 2 :- } "
if [ " ${ !var :- } " ] && [ " ${ !fileVar :- } " ] ; then
echo >& 2 " error: both $var and $fileVar are set (but are exclusive) "
exit 1
fi
local val = " $def "
if [ " ${ !var :- } " ] ; then
val = " ${ !var } "
elif [ " ${ !fileVar :- } " ] ; then
val = " $( < " ${ !fileVar } " ) "
fi
export " $var " = " $val "
unset " $fileVar "
}
2016-09-30 14:14:20 -07:00
_check_config( ) {
toRun = ( " $@ " --verbose --help --log-bin-index= " $( mktemp -u) " )
if ! errors = " $( " ${ toRun [@] } " 2>& 1 >/dev/null) " ; then
cat >& 2 <<-EOM
ERROR: mysqld failed while attempting to check config
command was: " ${ toRun [*] } "
$errors
EOM
exit 1
fi
}
2017-04-05 10:53:51 -07:00
# Fetch value from server config
# We use mysqld --verbose --help instead of my_print_defaults because the
# latter only show values present in config files, and not server defaults
_get_config( ) {
local conf = " $1 " ; shift
2018-11-12 14:37:58 -08:00
" $@ " --verbose --help --log-bin-index= " $( mktemp -u) " 2>/dev/null \
| awk '$1 == "' " $conf " '" && /^[^ \t]/ { sub(/^[^ \t]+[ \t]+/, ""); print; exit }'
# match "datadir /some/path with/spaces in/it here" but not "--xyz=abc\n datadir (xyz)"
2016-05-17 12:48:56 -07:00
}
# allow the container to be started with `--user`
if [ " $1 " = 'mysqld' -a -z " $wantHelp " -a " $( id -u) " = '0' ] ; then
2016-09-30 14:14:20 -07:00
_check_config " $@ "
2017-04-05 10:53:51 -07:00
DATADIR = " $( _get_config 'datadir' " $@ " ) "
2016-05-17 12:48:56 -07:00
mkdir -p " $DATADIR "
2018-09-24 16:29:24 -07:00
find " $DATADIR " \! -user mysql -exec chown mysql '{}' +
2016-05-17 12:48:56 -07:00
exec gosu mysql " $BASH_SOURCE " " $@ "
fi
2016-02-24 15:51:13 -08:00
if [ " $1 " = 'mysqld' -a -z " $wantHelp " ] ; then
2016-09-30 14:14:20 -07:00
# still need to check config, container may have started with --user
_check_config " $@ "
2015-08-11 16:39:56 -07:00
# Get config
2017-04-05 10:53:51 -07:00
DATADIR = " $( _get_config 'datadir' " $@ " ) "
2015-08-11 16:39:56 -07:00
2015-01-29 16:18:59 -07:00
if [ ! -d " $DATADIR /mysql " ] ; then
2016-11-23 10:35:50 -08:00
file_env 'MYSQL_ROOT_PASSWORD'
2016-01-26 09:22:48 -08:00
if [ -z " $MYSQL_ROOT_PASSWORD " -a -z " $MYSQL_ALLOW_EMPTY_PASSWORD " -a -z " $MYSQL_RANDOM_ROOT_PASSWORD " ] ; then
echo >& 2 'error: database is uninitialized and password option is not specified '
echo >& 2 ' You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD'
2015-01-29 16:18:59 -07:00
exit 1
fi
2015-08-11 16:39:56 -07:00
mkdir -p " $DATADIR "
2015-10-21 15:53:08 -07:00
echo 'Initializing database'
2019-02-27 16:47:52 -08:00
installArgs = ( --datadir= " $DATADIR " --rpm )
if { mysql_install_db --help || :; } | grep -q -- '--auth-root-authentication-method' ; then
# beginning in 10.4.3, install_db uses "socket" which only allows system user root to connect, switch back to "normal" to allow mysql root without a password
# see https://github.com/MariaDB/server/commit/b9f3f06857ac6f9105dc65caae19782f09b47fb3
# (this flag doesn't exist in 10.0 and below)
installArgs += ( --auth-root-authentication-method= normal )
fi
2018-05-18 13:06:01 -07:00
# "Other options are passed to mysqld." (so we pass all "mysqld" arguments directly here)
2019-02-27 16:47:52 -08:00
mysql_install_db " ${ installArgs [@] } " " ${ @ : 2 } "
2015-10-21 15:53:08 -07:00
echo 'Database initialized'
2015-08-11 16:39:56 -07:00
2017-04-05 10:53:51 -07:00
SOCKET = " $( _get_config 'socket' " $@ " ) "
" $@ " --skip-networking --socket= " ${ SOCKET } " &
2015-08-11 16:39:56 -07:00
pid = " $! "
2017-04-05 10:53:51 -07:00
mysql = ( mysql --protocol= socket -uroot -hlocalhost --socket= " ${ SOCKET } " )
2015-08-11 16:39:56 -07:00
for i in { 30..0} ; do
if echo 'SELECT 1' | " ${ mysql [@] } " & > /dev/null; then
break
fi
echo 'MySQL init process in progress...'
sleep 1
done
if [ " $i " = 0 ] ; then
echo >& 2 'MySQL init process failed.'
exit 1
fi
2015-09-24 23:24:45 +02:00
if [ -z " $MYSQL_INITDB_SKIP_TZINFO " ] ; then
# sed is for https://bugs.mysql.com/bug.php?id=20545
mysql_tzinfo_to_sql /usr/share/zoneinfo | sed 's/Local time zone must be set--see zic manual page/FCTY/' | " ${ mysql [@] } " mysql
fi
2015-08-11 16:39:56 -07:00
2016-01-26 09:22:48 -08:00
if [ ! -z " $MYSQL_RANDOM_ROOT_PASSWORD " ] ; then
2016-11-23 10:35:50 -08:00
export MYSQL_ROOT_PASSWORD = " $( pwgen -1 32) "
2016-01-26 09:22:48 -08:00
echo " GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD "
fi
2017-04-05 10:53:51 -07:00
rootCreate =
# default root to listen for connections from anywhere
file_env 'MYSQL_ROOT_HOST' '%'
if [ ! -z " $MYSQL_ROOT_HOST " -a " $MYSQL_ROOT_HOST " != 'localhost' ] ; then
# no, we don't care if read finds a terminating character in this heredoc
# https://unix.stackexchange.com/questions/265149/why-is-set-o-errexit-breaking-this-read-heredoc-expression/265151#265151
read -r -d '' rootCreate <<-EOSQL || true
CREATE USER 'root' @'${MYSQL_ROOT_HOST}' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
GRANT ALL ON *.* TO 'root' @'${MYSQL_ROOT_HOST}' WITH GRANT OPTION ;
EOSQL
fi
2015-08-11 16:39:56 -07:00
" ${ mysql [@] } " <<-EOSQL
-- What's done in this file shouldn' t be replicated
-- or products like mysql-fabric won' t work
SET @@SESSION.SQL_LOG_BIN= 0;
2015-08-26 11:13:23 -07:00
2017-04-05 10:53:51 -07:00
DELETE FROM mysql.user WHERE user NOT IN ( 'mysql.sys' , 'mysqlxsys' , 'root' ) OR host NOT IN ( 'localhost' ) ;
SET PASSWORD FOR 'root' @'localhost' = PASSWORD( '${MYSQL_ROOT_PASSWORD}' ) ;
GRANT ALL ON *.* TO 'root' @'localhost' WITH GRANT OPTION ;
${ rootCreate }
2015-01-29 16:18:59 -07:00
DROP DATABASE IF EXISTS test ;
2015-08-11 16:39:56 -07:00
FLUSH PRIVILEGES ;
2015-01-29 16:18:59 -07:00
EOSQL
2015-08-26 11:13:23 -07:00
if [ ! -z " $MYSQL_ROOT_PASSWORD " ] ; then
mysql += ( -p" ${ MYSQL_ROOT_PASSWORD } " )
fi
2015-08-11 16:39:56 -07:00
2016-11-23 10:35:50 -08:00
file_env 'MYSQL_DATABASE'
2014-11-24 15:31:55 -08:00
if [ " $MYSQL_DATABASE " ] ; then
2015-08-11 16:39:56 -07:00
echo " CREATE DATABASE IF NOT EXISTS \` $MYSQL_DATABASE \` ; " | " ${ mysql [@] } "
mysql += ( " $MYSQL_DATABASE " )
2015-01-29 16:18:59 -07:00
fi
2015-08-11 16:39:56 -07:00
2016-11-23 10:35:50 -08:00
file_env 'MYSQL_USER'
file_env 'MYSQL_PASSWORD'
2015-01-29 16:18:59 -07:00
if [ " $MYSQL_USER " -a " $MYSQL_PASSWORD " ] ; then
2015-10-21 15:53:08 -07:00
echo " CREATE USER ' $MYSQL_USER '@'%' IDENTIFIED BY ' $MYSQL_PASSWORD ' ; " | " ${ mysql [@] } "
2015-08-11 16:39:56 -07:00
2015-01-29 16:18:59 -07:00
if [ " $MYSQL_DATABASE " ] ; then
2015-10-21 15:53:08 -07:00
echo " GRANT ALL ON \` $MYSQL_DATABASE \`.* TO ' $MYSQL_USER '@'%' ; " | " ${ mysql [@] } "
2015-01-29 16:18:59 -07:00
fi
2014-11-24 15:31:55 -08:00
fi
2015-08-11 16:39:56 -07:00
echo
for f in /docker-entrypoint-initdb.d/*; do
case " $f " in
2015-12-05 22:42:15 +03:00
*.sh) echo " $0 : running $f " ; . " $f " ; ;
2016-01-26 09:22:48 -08:00
*.sql) echo " $0 : running $f " ; " ${ mysql [@] } " < " $f " ; echo ; ;
*.sql.gz) echo " $0 : running $f " ; gunzip -c " $f " | " ${ mysql [@] } " ; echo ; ;
2015-12-05 22:42:15 +03:00
*) echo " $0 : ignoring $f " ; ;
2015-08-11 16:39:56 -07:00
esac
echo
done
if ! kill -s TERM " $pid " || ! wait " $pid " ; then
echo >& 2 'MySQL init process failed.'
exit 1
fi
echo
echo 'MySQL init process done. Ready for start up.'
echo
2014-11-24 15:31:55 -08:00
fi
fi
exec " $@ "