Skip to content

Commit 0473555

Browse files
committed
Added a way to store root password in a file
If the MYSQL_ROOT_PASSWORD variable is a valid file path, the entrypoint script will cat the file and use the output as the root password.
1 parent 1cbaef6 commit 0473555

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

5.5/docker-entrypoint.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ if [ "$1" = 'mysqld' ]; then
1212

1313
if [ ! -d "$DATADIR/mysql" ]; then
1414
if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
15-
echo >&2 'error: database is uninitialized and password option is not specified '
16-
echo >&2 ' You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD'
17-
exit 1
18-
fi
15+
echo >&2 'error: database is uninitialized and password option is not specified '
16+
echo >&2 ' You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD'
17+
exit 1
18+
fi
19+
# If the password variable is a filename we use the contents of the file
20+
if [ -f "$MYSQL_ROOT_PASSWORD" ]; then
21+
MYSQL_ROOT_PASSWORD="$(cat $MYSQL_ROOT_PASSWORD)"
22+
fi
1923
mkdir -p "$DATADIR"
2024
chown -R mysql:mysql "$DATADIR"
2125

5.6/docker-entrypoint.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ if [ "$1" = 'mysqld' ]; then
1212

1313
if [ ! -d "$DATADIR/mysql" ]; then
1414
if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
15-
echo >&2 'error: database is uninitialized and password option is not specified '
16-
echo >&2 ' You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD'
17-
exit 1
18-
fi
15+
echo >&2 'error: database is uninitialized and password option is not specified '
16+
echo >&2 ' You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD'
17+
exit 1
18+
fi
19+
# If the password variable is a filename we use the contents of the file
20+
if [ -f "$MYSQL_ROOT_PASSWORD" ]; then
21+
MYSQL_ROOT_PASSWORD="$(cat $MYSQL_ROOT_PASSWORD)"
22+
fi
1923
mkdir -p "$DATADIR"
2024
chown -R mysql:mysql "$DATADIR"
2125

5.7/docker-entrypoint.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ if [ "$1" = 'mysqld' ]; then
1616
echo >&2 ' You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD'
1717
exit 1
1818
fi
19+
# If the password variable is a filename we use the contents of the file
20+
if [ -f "$MYSQL_ROOT_PASSWORD" ]; then
21+
MYSQL_ROOT_PASSWORD="$(cat $MYSQL_ROOT_PASSWORD)"
22+
fi
1923
mkdir -p "$DATADIR"
2024
chown -R mysql:mysql "$DATADIR"
2125

0 commit comments

Comments
 (0)