5 # if command starts with an option, prepend mysqld
6 if [ "${1:0:1}" = '-' ]; then
10 # skip setup if they want an option that stops mysqld
14 -'?'|--help|--print-defaults|-V|--version)
23 echo "$1" | sed -e "s/'/\\\\'/g; s/\"/\\\\\"/g"
26 # usage: file_env VAR [DEFAULT]
27 # ie: file_env 'XYZ_DB_PASSWORD' 'example'
28 # (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
29 # "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
32 local fileVar="${var}_FILE"
34 if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
35 echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
39 if [ "${!var:-}" ]; then
41 elif [ "${!fileVar:-}" ]; then
42 val="$(< "${!fileVar}")"
44 val=`prepare_password $val`
50 toRun=( "$@" --verbose --help --log-bin-index="$(mktemp -u)" )
51 if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
54 ERROR: mysqld failed while attempting to check config
55 command was: "${toRun[*]}"
63 # Fetch value from server config
64 # We use mysqld --verbose --help instead of my_print_defaults because the
65 # latter only show values present in config files, and not server defaults
67 local conf="$1"; shift
68 "$@" --verbose --help --log-bin-index="$(mktemp -u)" 2>/dev/null \
69 | awk '$1 == "'"$conf"'" && /^[^ \t]/ { sub(/^[^ \t]+[ \t]+/, ""); print; exit }'
70 # match "datadir /some/path with/spaces in/it here" but not "--xyz=abc\n datadir (xyz)"
73 # allow the container to be started with `--user`
74 if [ "$1" = 'mysqld' -a -z "$wantHelp" -a "$(id -u)" = '0' ]; then
76 DATADIR="$(_get_config 'datadir' "$@")"
78 find "$DATADIR" \! -user mysql -exec chown mysql '{}' +
79 exec gosu mysql "$BASH_SOURCE" "$@"
82 if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
83 # still need to check config, container may have started with --user
86 DATADIR="$(_get_config 'datadir' "$@")"
88 if [ ! -d "$DATADIR/mysql" ]; then
89 file_env 'MYSQL_ROOT_PASSWORD'
90 if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
91 echo >&2 'error: database is uninitialized and password option is not specified '
92 echo >&2 ' You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD'
98 echo 'Initializing database'
99 # "Other options are passed to mysqld." (so we pass all "mysqld" arguments directly here)
100 mysql_install_db --datadir="$DATADIR" --rpm "${@:2}"
101 echo 'Database initialized'
103 SOCKET="$(_get_config 'socket' "$@")"
104 "$@" --skip-networking --socket="${SOCKET}" &
107 mysql=( mysql --protocol=socket -uroot -hlocalhost --socket="${SOCKET}" )
110 if echo 'SELECT 1' | "${mysql[@]}" &> /dev/null; then
113 echo 'MySQL init process in progress...'
116 if [ "$i" = 0 ]; then
117 echo >&2 'MySQL init process failed.'
121 if [ -z "$MYSQL_INITDB_SKIP_TZINFO" ]; then
122 # sed is for https://bugs.mysql.com/bug.php?id=20545
123 mysql_tzinfo_to_sql /usr/share/zoneinfo | sed 's/Local time zone must be set--see zic manual page/FCTY/' | "${mysql[@]}" mysql
126 if [ ! -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
127 export MYSQL_ROOT_PASSWORD="$(pwgen -1 32)"
128 echo "GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
132 # default root to listen for connections from anywhere
133 file_env 'MYSQL_ROOT_HOST' '%'
134 if [ ! -z "$MYSQL_ROOT_HOST" -a "$MYSQL_ROOT_HOST" != 'localhost' ]; then
135 # no, we don't care if read finds a terminating character in this heredoc
136 # https://unix.stackexchange.com/questions/265149/why-is-set-o-errexit-breaking-this-read-heredoc-expression/265151#265151
137 read -r -d '' rootCreate <<-EOSQL || true
138 CREATE USER 'root'@'${MYSQL_ROOT_HOST}' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
139 GRANT ALL ON *.* TO 'root'@'${MYSQL_ROOT_HOST}' WITH GRANT OPTION ;
143 "${mysql[@]}" <<-EOSQL
144 -- What's done in this file shouldn't be replicated
145 -- or products like mysql-fabric won't work
146 SET @@SESSION.SQL_LOG_BIN=0;
148 DELETE FROM mysql.user WHERE user NOT IN ('mysql.sys', 'mysqlxsys', 'root') OR host NOT IN ('localhost') ;
149 SET PASSWORD FOR 'root'@'localhost'=PASSWORD('${MYSQL_ROOT_PASSWORD}') ;
150 GRANT ALL ON *.* TO 'root'@'localhost' WITH GRANT OPTION ;
152 DROP DATABASE IF EXISTS test ;
156 if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then
157 mysql+=( -p"${MYSQL_ROOT_PASSWORD}" )
160 file_env 'MYSQL_DATABASE'
161 if [ "$MYSQL_DATABASE" ]; then
162 echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" | "${mysql[@]}"
163 mysql+=( "$MYSQL_DATABASE" )
166 file_env 'MYSQL_USER'
167 file_env 'MYSQL_PASSWORD'
168 if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
169 echo "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;" | "${mysql[@]}"
171 if [ "$MYSQL_DATABASE" ]; then
172 echo "GRANT ALL ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'%' ;" | "${mysql[@]}"
177 for f in /docker-entrypoint-initdb.d/*; do
179 *.sh) echo "$0: running $f"; . "$f" ;;
180 *.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
181 *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
182 *) echo "$0: ignoring $f" ;;
187 if ! kill -s TERM "$pid" || ! wait "$pid"; then
188 echo >&2 'MySQL init process failed.'
193 echo 'MySQL init process done. Ready for start up.'