@@ -85,11 +85,13 @@ docker_temp_server_start() {
85
85
fi
86
86
87
87
# For 5.7+ the server is ready for use as soon as startup command unblocks
88
- if [ " ${MYSQL_MAJOR} " = " 5.5 " ] || [ " ${MYSQL_MAJOR} " = " 5. 6" ]; then
88
+ if [ " ${MYSQL_MAJOR} " = " 5.6" ]; then
89
89
mysql_note " Waiting for server startup"
90
90
local i
91
91
for i in {30..0}; do
92
- if docker_process_sql --database=mysql <<< ' SELECT 1' & > /dev/null; then
92
+ # unset MYSQL_ROOT_PASSWORD for just docker_process_sql
93
+ # so that it won't try to fill in a password file when it hasn't been set yet
94
+ if MYSQL_ROOT_PASSWORD= docker_process_sql --database=mysql <<< ' SELECT 1' & > /dev/null; then
93
95
break
94
96
fi
95
97
sleep 1
@@ -104,7 +106,7 @@ docker_temp_server_start() {
104
106
# the shutdown is complete.
105
107
docker_temp_server_stop () {
106
108
local result=0
107
- mysqladmin --defaults-extra-file=<( echo " ${PASSFILE} " ) shutdown -uroot --socket=" ${SOCKET} " || result=$?
109
+ mysqladmin --defaults-extra-file=<( _mysql_passfile ) shutdown -uroot --socket=" ${SOCKET} " || result=$?
108
110
if [ " $result " != " 0" ]; then
109
111
mysql_error " Unable to shut down server. Status code $result ."
110
112
fi
@@ -170,25 +172,29 @@ docker_process_sql() {
170
172
set -- --database=" $MYSQL_DATABASE " " $@ "
171
173
fi
172
174
173
- mysql --defaults-file=<( echo " ${PASSFILE} " ) --protocol=socket -uroot -hlocalhost --socket=" ${SOCKET} " " $@ "
175
+ mysql --defaults-file=<( _mysql_passfile ) --protocol=socket -uroot -hlocalhost --socket=" ${SOCKET} " " $@ "
174
176
}
175
177
176
178
# Initializes database with timezone info and root password, plus optional extra db/user
177
179
docker_setup_db () {
178
180
# Load timezone info into database
179
181
if [ -z " $MYSQL_INITDB_SKIP_TZINFO " ]; then
180
182
# sed is for https://bugs.mysql.com/bug.php?id=20545
181
- mysql_tzinfo_to_sql /usr/share/zoneinfo | sed ' s/Local time zone must be set--see zic manual page/FCTY/' | docker_process_sql --database=mysql
183
+ mysql_tzinfo_to_sql /usr/share/zoneinfo \
184
+ | sed ' s/Local time zone must be set--see zic manual page/FCTY/' \
185
+ | MYSQL_ROOT_PASSWORD= docker_process_sql --database=mysql
186
+ # unset MYSQL_ROOT_PASSWORD for just docker_process_sql
187
+ # so that it won't try to fill in a password file when it hasn't been set yet
182
188
fi
183
189
# Generate random root password
184
- if [ ! -z " $MYSQL_RANDOM_ROOT_PASSWORD " ]; then
190
+ if [ -n " $MYSQL_RANDOM_ROOT_PASSWORD " ]; then
185
191
export MYSQL_ROOT_PASSWORD=" $( pwgen -1 32) "
186
192
mysql_note " GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD "
187
193
fi
188
194
# Sets root password and creates root users for non-localhost hosts
189
195
rootCreate=
190
196
# default root to listen for connections from anywhere
191
- if [ ! -z " $MYSQL_ROOT_HOST " -a " $MYSQL_ROOT_HOST " != ' localhost' ]; then
197
+ if [ -n " $MYSQL_ROOT_HOST " ] && [ " $MYSQL_ROOT_HOST " != ' localhost' ]; then
192
198
# no, we don't care if read finds a terminating character in this heredoc
193
199
# https://unix.stackexchange.com/questions/265149/why-is-set-o-errexit-breaking-this-read-heredoc-expression/265151#265151
194
200
read -r -d ' ' rootCreate << -EOSQL || true
@@ -197,7 +203,9 @@ docker_setup_db() {
197
203
EOSQL
198
204
fi
199
205
200
- docker_process_sql --database=mysql << -EOSQL
206
+ # unset MYSQL_ROOT_PASSWORD for just docker_process_sql
207
+ # so that it won't try to fill in a password file when it is just now being set
208
+ MYSQL_ROOT_PASSWORD= docker_process_sql --database=mysql << -EOSQL
201
209
-- What's done in this file shouldn't be replicated
202
210
-- or products like mysql-fabric won't work
203
211
SET @@SESSION.SQL_LOG_BIN=0;
@@ -210,27 +218,17 @@ SET PASSWORD FOR 'root'@'localhost'=PASSWORD('${MYSQL_ROOT_PASSWORD}');
210
218
DROP DATABASE IF EXISTS test ;
211
219
EOSQL
212
220
213
- # Write the password to the "file" the client uses
214
- # the client command will use process substitution to create a file on the fly
215
- # ie: --defaults-file=<( echo "${PASSFILE}" )
216
- if [ ! -z " $MYSQL_ROOT_PASSWORD " ]; then
217
- read -r -d ' ' PASSFILE << -EOF || true
218
- [client]
219
- password="${MYSQL_ROOT_PASSWORD} "
220
- EOF
221
- fi
222
-
223
221
# Creates a custom database and user if specified
224
- if [ " $MYSQL_DATABASE " ]; then
222
+ if [ -n " $MYSQL_DATABASE " ]; then
225
223
mysql_note " Creating database ${MYSQL_DATABASE} "
226
224
docker_process_sql --database=mysql <<< " CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;"
227
225
fi
228
226
229
- if [ " $MYSQL_USER " -a " $MYSQL_PASSWORD " ]; then
227
+ if [ -n " $MYSQL_USER " ] && [ -n " $MYSQL_PASSWORD " ]; then
230
228
mysql_note " Creating user ${MYSQL_USER} "
231
229
docker_process_sql --database=mysql <<< " CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;"
232
230
233
- if [ " $MYSQL_DATABASE " ]; then
231
+ if [ -n " $MYSQL_DATABASE " ]; then
234
232
mysql_note " Giving user ${MYSQL_USER} access to schema ${MYSQL_DATABASE} "
235
233
docker_process_sql --database=mysql <<< " GRANT ALL ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'%' ;"
236
234
fi
@@ -239,23 +237,31 @@ SET PASSWORD FOR 'root'@'localhost'=PASSWORD('${MYSQL_ROOT_PASSWORD}');
239
237
fi
240
238
}
241
239
240
+ _mysql_passfile () {
241
+ # echo the password to the "file" the client uses
242
+ # the client command will use process substitution to create a file on the fly
243
+ # ie: --defaults-file=<( _mysql_passfile )
244
+ if [ -n " $MYSQL_ROOT_PASSWORD " ]; then
245
+ cat << -EOF
246
+ [client]
247
+ password="${MYSQL_ROOT_PASSWORD} "
248
+ EOF
249
+ fi
250
+ }
251
+
242
252
# Mark root user as expired so the password must be changed before anything
243
253
# else can be done (only supported for 5.6+)
244
254
mysql_expire_root_user () {
245
- if [ ! -z " $MYSQL_ONETIME_PASSWORD " ]; then
246
- if [ " ${MYSQL_MAJOR} " = " 5.5" ]; then
247
- mysql_warn " MySQL 5.5 does not support PASSWORD EXPIRE (required for MYSQL_ONETIME_PASSWORD)"
248
- else
249
- docker_process_sql --database=mysql << -EOSQL
250
- ALTER USER 'root'@'%' PASSWORD EXPIRE;
251
- EOSQL
252
- fi
255
+ if [ -n " $MYSQL_ONETIME_PASSWORD " ]; then
256
+ docker_process_sql --database=mysql << -EOSQL
257
+ ALTER USER 'root'@'%' PASSWORD EXPIRE;
258
+ EOSQL
253
259
fi
254
260
}
255
261
256
262
# check arguments for an option that would cause mysqld to stop
257
263
# return true if there is one
258
- _want_help () {
264
+ _mysql_want_help () {
259
265
local arg
260
266
for arg; do
261
267
case " $arg " in
@@ -274,23 +280,20 @@ _main() {
274
280
fi
275
281
276
282
# skip setup if they aren't running mysqld or want an option that stops mysqld
277
- if [ " $1 " = ' mysqld' ] && ! _want_help " $@ " ; then
283
+ if [ " $1 " = ' mysqld' ] && ! _mysql_want_help " $@ " ; then
278
284
mysql_note " Entrypoint script for MySQL Server ${MYSQL_VERSION} started."
279
285
280
286
# Load various environment variables
281
287
docker_setup_env " $@ "
282
288
mysql_check_config " $@ "
289
+ docker_create_db_directories
283
290
284
291
# If container is started as root user, restart as dedicated mysql user
285
292
if [ " $( id -u) " = " 0" ]; then
286
- docker_create_db_directories
287
293
mysql_note " Switching to dedicated user 'mysql'"
288
294
exec gosu mysql " $BASH_SOURCE " " $@ "
289
295
fi
290
296
291
- # just in case the script was not started as root
292
- docker_create_db_directories
293
-
294
297
# If this is true then there's no database, and it needs to be initialized
295
298
if [ ! -d " $DATADIR /mysql" ]; then
296
299
docker_verify_minimum_env
@@ -309,7 +312,6 @@ _main() {
309
312
docker_temp_server_stop
310
313
mysql_note " Temporary server stopped"
311
314
312
- unset PASSFILE
313
315
echo
314
316
mysql_note " MySQL init process done. Ready for start up."
315
317
echo
0 commit comments