blob: 94e394ac6d8fbd474b4e9817795cb3214f86c7b3 [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001#!/bin/bash
2set -eo pipefail
3shopt -s nullglob
4
5openssl req -x509 -sha256 -newkey rsa:2048 -keyout /var/lib/mysql/sql.key -out /var/lib/mysql/sql.crt -days 3650 -nodes -subj '/CN=mysql'
6
7# if command starts with an option, prepend mysqld
8if [ "${1:0:1}" = '-' ]; then
9 set -- mysqld "$@"
10fi
11
12# skip setup if they want an option that stops mysqld
13wantHelp=
14for arg; do
15 case "$arg" in
16 -'?'|--help|--print-defaults|-V|--version)
17 wantHelp=1
18 break
19 ;;
20 esac
21done
22
23# usage: file_env VAR [DEFAULT]
24# ie: file_env 'XYZ_DB_PASSWORD' 'example'
25# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
26# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
27file_env() {
28 local var="$1"
29 local fileVar="${var}_FILE"
30 local def="${2:-}"
31 if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
32 echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
33 exit 1
34 fi
35 local val="$def"
36 if [ "${!var:-}" ]; then
37 val="${!var}"
38 elif [ "${!fileVar:-}" ]; then
39 val="$(< "${!fileVar}")"
40 fi
41 export "$var"="$val"
42 unset "$fileVar"
43}
44
45_check_config() {
46 toRun=( "$@" --verbose --help --log-bin-index="$(mktemp -u)" )
47 if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
48 cat >&2 <<-EOM
49
50 ERROR: mysqld failed while attempting to check config
51 command was: "${toRun[*]}"
52
53 $errors
54 EOM
55 exit 1
56 fi
57}
58
59# Fetch value from server config
60# We use mysqld --verbose --help instead of my_print_defaults because the
61# latter only show values present in config files, and not server defaults
62_get_config() {
63 local conf="$1"; shift
64 "$@" --verbose --help --log-bin-index="$(mktemp -u)" 2>/dev/null | awk '$1 == "'"$conf"'" { print $2; exit }'
65}
66
67# allow the container to be started with `--user`
68if [ "$1" = 'mysqld' -a -z "$wantHelp" -a "$(id -u)" = '0' ]; then
69 _check_config "$@"
70 DATADIR="$(_get_config 'datadir' "$@")"
71 mkdir -p "$DATADIR"
72 chown -R mysql:mysql "$DATADIR"
73 exec gosu mysql "$BASH_SOURCE" "$@"
74fi
75
76if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
77 # still need to check config, container may have started with --user
78 _check_config "$@"
79 # Get config
80 DATADIR="$(_get_config 'datadir' "$@")"
81
82 if [ ! -d "$DATADIR/mysql" ]; then
83 file_env 'MYSQL_ROOT_PASSWORD'
84 if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
85 echo >&2 'error: database is uninitialized and password option is not specified '
86 echo >&2 ' You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD'
87 exit 1
88 fi
89
90 mkdir -p "$DATADIR"
91
92 echo 'Initializing database'
93 # "Other options are passed to mysqld." (so we pass all "mysqld" arguments directly here)
94 mysql_install_db --datadir="$DATADIR" --rpm "${@:2}"
95 echo 'Database initialized'
96
97 SOCKET="$(_get_config 'socket' "$@")"
98 "$@" --skip-networking --socket="${SOCKET}" &
99 pid="$!"
100
101 mysql=( mysql --protocol=socket -uroot -hlocalhost --socket="${SOCKET}" )
102
103 for i in {30..0}; do
104 if echo 'SELECT 1' | "${mysql[@]}" &> /dev/null; then
105 break
106 fi
107 echo 'MySQL init process in progress...'
108 sleep 1
109 done
110 if [ "$i" = 0 ]; then
111 echo >&2 'MySQL init process failed.'
112 exit 1
113 fi
114
115 if [ -z "$MYSQL_INITDB_SKIP_TZINFO" ]; then
116 # sed is for https://bugs.mysql.com/bug.php?id=20545
117 mysql_tzinfo_to_sql /usr/share/zoneinfo | sed 's/Local time zone must be set--see zic manual page/FCTY/' | "${mysql[@]}" mysql
118 fi
119
120 if [ ! -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
121 export MYSQL_ROOT_PASSWORD="$(pwgen -1 32)"
122 echo "GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
123 fi
124
125 rootCreate=
126 # default root to listen for connections from anywhere
127 file_env 'MYSQL_ROOT_HOST' '%'
128 if [ ! -z "$MYSQL_ROOT_HOST" -a "$MYSQL_ROOT_HOST" != 'localhost' ]; then
129 # no, we don't care if read finds a terminating character in this heredoc
130 # https://unix.stackexchange.com/questions/265149/why-is-set-o-errexit-breaking-this-read-heredoc-expression/265151#265151
131 read -r -d '' rootCreate <<-EOSQL || true
132 CREATE USER 'root'@'${MYSQL_ROOT_HOST}' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
133 GRANT ALL ON *.* TO 'root'@'${MYSQL_ROOT_HOST}' WITH GRANT OPTION ;
134 EOSQL
135 fi
136
137 "${mysql[@]}" <<-EOSQL
138 -- What's done in this file shouldn't be replicated
139 -- or products like mysql-fabric won't work
140 SET @@SESSION.SQL_LOG_BIN=0;
141
142 DELETE FROM mysql.user WHERE user NOT IN ('mysql.sys', 'mysqlxsys', 'root') OR host NOT IN ('localhost') ;
143 SET PASSWORD FOR 'root'@'localhost'=PASSWORD('${MYSQL_ROOT_PASSWORD}') ;
144 GRANT ALL ON *.* TO 'root'@'localhost' WITH GRANT OPTION ;
145 ${rootCreate}
146 DROP DATABASE IF EXISTS test ;
147 FLUSH PRIVILEGES ;
148 EOSQL
149
150 if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then
151 mysql+=( -p"${MYSQL_ROOT_PASSWORD}" )
152 fi
153
154 file_env 'MYSQL_DATABASE'
155 if [ "$MYSQL_DATABASE" ]; then
156 echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" | "${mysql[@]}"
157 mysql+=( "$MYSQL_DATABASE" )
158 fi
159
160 file_env 'MYSQL_USER'
161 file_env 'MYSQL_PASSWORD'
162 if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
163 echo "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;" | "${mysql[@]}"
164
165 if [ "$MYSQL_DATABASE" ]; then
166 echo "GRANT ALL ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'%' ;" | "${mysql[@]}"
167 fi
168 fi
169
170 echo
171 for f in /docker-entrypoint-initdb.d/*; do
172 case "$f" in
173 *.sh) echo "$0: running $f"; . "$f" ;;
174 *.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
175 *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
176 *) echo "$0: ignoring $f" ;;
177 esac
178 echo
179 done
180
181 if ! kill -s TERM "$pid" || ! wait "$pid"; then
182 echo >&2 'MySQL init process failed.'
183 exit 1
184 fi
185
186 echo
187 echo 'MySQL init process done. Ready for start up.'
188 echo
189 fi
190fi
191
192exec "$@"