|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# What Was Modified: |
| 4 | +# |
| 5 | +# Choosing to use mysql official image and building off of it. Since variable |
| 6 | +# names are slightly different, this file was modified to use our choice of |
| 7 | +# variable names for backward compatibility purposes. |
| 8 | +# |
| 9 | +# MYSQL_USER is overwritten by MYSQL_USER_NAME |
| 10 | +# MYSQL_PASSWORD is overwritten by MYSQL_USER_PASSWORD |
| 11 | + |
| 12 | +if [[ -n "$MYSQL_USER_NAME" ]]; then MYSQL_USER=$MYSQL_USER_NAME; fi |
| 13 | +if [[ -n "$MYSQL_USER_PASSWORD" ]]; then MYSQL_PASSWORD=$MYSQL_USER_PASSWORD; fi |
| 14 | + |
| 15 | +set -eo pipefail |
| 16 | +shopt -s nullglob |
| 17 | + |
| 18 | +# if command starts with an option, prepend mysqld |
| 19 | +if [ "${1:0:1}" = '-' ]; then |
| 20 | + set -- mysqld "$@" |
| 21 | +fi |
| 22 | + |
| 23 | +# skip setup if they want an option that stops mysqld |
| 24 | +wantHelp= |
| 25 | +for arg; do |
| 26 | + case "$arg" in |
| 27 | + -'?'|--help|--print-defaults|-V|--version) |
| 28 | + wantHelp=1 |
| 29 | + break |
| 30 | + ;; |
| 31 | + esac |
| 32 | +done |
| 33 | + |
| 34 | +# usage: file_env VAR [DEFAULT] |
| 35 | +# ie: file_env 'XYZ_DB_PASSWORD' 'example' |
| 36 | +# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of |
| 37 | +# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature) |
| 38 | +file_env() { |
| 39 | + local var="$1" |
| 40 | + local fileVar="${var}_FILE" |
| 41 | + local def="${2:-}" |
| 42 | + if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then |
| 43 | + echo >&2 "error: both $var and $fileVar are set (but are exclusive)" |
| 44 | + exit 1 |
| 45 | + fi |
| 46 | + local val="$def" |
| 47 | + if [ "${!var:-}" ]; then |
| 48 | + val="${!var}" |
| 49 | + elif [ "${!fileVar:-}" ]; then |
| 50 | + val="$(< "${!fileVar}")" |
| 51 | + fi |
| 52 | + export "$var"="$val" |
| 53 | + unset "$fileVar" |
| 54 | +} |
| 55 | + |
| 56 | +# usage: process_init_file FILENAME MYSQLCOMMAND... |
| 57 | +# ie: process_init_file foo.sh mysql -uroot |
| 58 | +# (process a single initializer file, based on its extension. we define this |
| 59 | +# function here, so that initializer scripts (*.sh) can use the same logic, |
| 60 | +# potentially recursively, or override the logic used in subsequent calls) |
| 61 | +process_init_file() { |
| 62 | + local f="$1"; shift |
| 63 | + local mysql=( "$@" ) |
| 64 | + |
| 65 | + case "$f" in |
| 66 | + *.sh) echo "$0: running $f"; . "$f" ;; |
| 67 | + *.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;; |
| 68 | + *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;; |
| 69 | + *) echo "$0: ignoring $f" ;; |
| 70 | + esac |
| 71 | + echo |
| 72 | +} |
| 73 | + |
| 74 | +_check_config() { |
| 75 | + toRun=( "$@" --verbose --help ) |
| 76 | + if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then |
| 77 | + cat >&2 <<-EOM |
| 78 | +
|
| 79 | + ERROR: mysqld failed while attempting to check config |
| 80 | + command was: "${toRun[*]}" |
| 81 | +
|
| 82 | + $errors |
| 83 | + EOM |
| 84 | + exit 1 |
| 85 | + fi |
| 86 | +} |
| 87 | + |
| 88 | +# Fetch value from server config |
| 89 | +# We use mysqld --verbose --help instead of my_print_defaults because the |
| 90 | +# latter only show values present in config files, and not server defaults |
| 91 | +_get_config() { |
| 92 | + local conf="$1"; shift |
| 93 | + "$@" --verbose --help --log-bin-index="$(mktemp -u)" 2>/dev/null | awk '$1 == "'"$conf"'" { print $2; exit }' |
| 94 | +} |
| 95 | + |
| 96 | +# allow the container to be started with `--user` |
| 97 | +if [ "$1" = 'mysqld' -a -z "$wantHelp" -a "$(id -u)" = '0' ]; then |
| 98 | + _check_config "$@" |
| 99 | + DATADIR="$(_get_config 'datadir' "$@")" |
| 100 | + mkdir -p "$DATADIR" |
| 101 | + chown -R mysql:mysql "$DATADIR" |
| 102 | + exec gosu mysql "$BASH_SOURCE" "$@" |
| 103 | +fi |
| 104 | + |
| 105 | +if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then |
| 106 | + # still need to check config, container may have started with --user |
| 107 | + _check_config "$@" |
| 108 | + # Get config |
| 109 | + DATADIR="$(_get_config 'datadir' "$@")" |
| 110 | + |
| 111 | + if [ ! -d "$DATADIR/mysql" ]; then |
| 112 | + file_env 'MYSQL_ROOT_PASSWORD' |
| 113 | + if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then |
| 114 | + echo >&2 'error: database is uninitialized and password option is not specified ' |
| 115 | + echo >&2 ' You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD' |
| 116 | + exit 1 |
| 117 | + fi |
| 118 | + |
| 119 | + mkdir -p "$DATADIR" |
| 120 | + |
| 121 | + echo 'Initializing database' |
| 122 | + "$@" --initialize-insecure |
| 123 | + echo 'Database initialized' |
| 124 | + |
| 125 | + if command -v mysql_ssl_rsa_setup > /dev/null && [ ! -e "$DATADIR/server-key.pem" ]; then |
| 126 | + # https://github.com/mysql/mysql-server/blob/23032807537d8dd8ee4ec1c4d40f0633cd4e12f9/packaging/deb-in/extra/mysql-systemd-start#L81-L84 |
| 127 | + echo 'Initializing certificates' |
| 128 | + mysql_ssl_rsa_setup --datadir="$DATADIR" |
| 129 | + echo 'Certificates initialized' |
| 130 | + fi |
| 131 | + |
| 132 | + SOCKET="$(_get_config 'socket' "$@")" |
| 133 | + "$@" --skip-networking --socket="${SOCKET}" & |
| 134 | + pid="$!" |
| 135 | + |
| 136 | + mysql=( mysql --protocol=socket -uroot -hlocalhost --socket="${SOCKET}" ) |
| 137 | + |
| 138 | + for i in {30..0}; do |
| 139 | + if echo 'SELECT 1' | "${mysql[@]}" &> /dev/null; then |
| 140 | + break |
| 141 | + fi |
| 142 | + echo 'MySQL init process in progress...' |
| 143 | + sleep 1 |
| 144 | + done |
| 145 | + if [ "$i" = 0 ]; then |
| 146 | + echo >&2 'MySQL init process failed.' |
| 147 | + exit 1 |
| 148 | + fi |
| 149 | + |
| 150 | + if [ -z "$MYSQL_INITDB_SKIP_TZINFO" ]; then |
| 151 | + # sed is for https://bugs.mysql.com/bug.php?id=20545 |
| 152 | + mysql_tzinfo_to_sql /usr/share/zoneinfo | sed 's/Local time zone must be set--see zic manual page/FCTY/' | "${mysql[@]}" mysql |
| 153 | + fi |
| 154 | + |
| 155 | + if [ ! -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then |
| 156 | + export MYSQL_ROOT_PASSWORD="$(pwgen -1 32)" |
| 157 | + echo "GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD" |
| 158 | + fi |
| 159 | + |
| 160 | + rootCreate= |
| 161 | + # default root to listen for connections from anywhere |
| 162 | + file_env 'MYSQL_ROOT_HOST' '%' |
| 163 | + if [ ! -z "$MYSQL_ROOT_HOST" -a "$MYSQL_ROOT_HOST" != 'localhost' ]; then |
| 164 | + # no, we don't care if read finds a terminating character in this heredoc |
| 165 | + # https://unix.stackexchange.com/questions/265149/why-is-set-o-errexit-breaking-this-read-heredoc-expression/265151#265151 |
| 166 | + read -r -d '' rootCreate <<-EOSQL || true |
| 167 | + CREATE USER 'root'@'${MYSQL_ROOT_HOST}' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ; |
| 168 | + GRANT ALL ON *.* TO 'root'@'${MYSQL_ROOT_HOST}' WITH GRANT OPTION ; |
| 169 | + EOSQL |
| 170 | + fi |
| 171 | + |
| 172 | + "${mysql[@]}" <<-EOSQL |
| 173 | + -- What's done in this file shouldn't be replicated |
| 174 | + -- or products like mysql-fabric won't work |
| 175 | + SET @@SESSION.SQL_LOG_BIN=0; |
| 176 | +
|
| 177 | + SET PASSWORD FOR 'root'@'localhost'=PASSWORD('${MYSQL_ROOT_PASSWORD}') ; |
| 178 | + GRANT ALL ON *.* TO 'root'@'localhost' WITH GRANT OPTION ; |
| 179 | + ${rootCreate} |
| 180 | + DROP DATABASE IF EXISTS test ; |
| 181 | + FLUSH PRIVILEGES ; |
| 182 | + EOSQL |
| 183 | + |
| 184 | + if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then |
| 185 | + mysql+=( -p"${MYSQL_ROOT_PASSWORD}" ) |
| 186 | + fi |
| 187 | + |
| 188 | + file_env 'MYSQL_DATABASE' |
| 189 | + if [ "$MYSQL_DATABASE" ]; then |
| 190 | + echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" | "${mysql[@]}" |
| 191 | + mysql+=( "$MYSQL_DATABASE" ) |
| 192 | + fi |
| 193 | + |
| 194 | + file_env 'MYSQL_USER' |
| 195 | + file_env 'MYSQL_PASSWORD' |
| 196 | + if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then |
| 197 | + echo "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;" | "${mysql[@]}" |
| 198 | + |
| 199 | + if [ "$MYSQL_DATABASE" ]; then |
| 200 | + echo "GRANT ALL ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'%' ;" | "${mysql[@]}" |
| 201 | + fi |
| 202 | + |
| 203 | + echo 'FLUSH PRIVILEGES ;' | "${mysql[@]}" |
| 204 | + fi |
| 205 | + |
| 206 | + echo |
| 207 | + ls /docker-entrypoint-initdb.d/ > /dev/null |
| 208 | + for f in /docker-entrypoint-initdb.d/*; do |
| 209 | + process_init_file "$f" "${mysql[@]}" |
| 210 | + done |
| 211 | + |
| 212 | + if [ ! -z "$MYSQL_ONETIME_PASSWORD" ]; then |
| 213 | + "${mysql[@]}" <<-EOSQL |
| 214 | + ALTER USER 'root'@'%' PASSWORD EXPIRE; |
| 215 | + EOSQL |
| 216 | + fi |
| 217 | + if ! kill -s TERM "$pid" || ! wait "$pid"; then |
| 218 | + echo >&2 'MySQL init process failed.' |
| 219 | + exit 1 |
| 220 | + fi |
| 221 | + |
| 222 | + echo |
| 223 | + echo 'MySQL init process done. Ready for start up.' |
| 224 | + echo |
| 225 | + fi |
| 226 | +fi |
| 227 | + |
| 228 | +exec "$@" |
0 commit comments