-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocker-entrypoint.sh
executable file
·106 lines (84 loc) · 3.15 KB
/
docker-entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/sh
set -e # terminate on errors
set -xo pipefail
DATA_DIR="$(mysqld --verbose --help --log-bin-index=`mktemp -u` 2>/dev/null | awk '$1 == "datadir" { print $2; exit }')"
PID_FILE=/run/mysqld/mysqld.pid
if [ ! -d "$DATA_DIR/mysql" ]; then
if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
echo >&2 'error: database is uninitialized and password option is not specified '
echo >&2 'You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD'
exit 1
fi
mkdir -p "$DATA_DIR"
chown -R mysql:mysql "$DATA_DIR"
echo 'Initializing database'
mysql_install_db --user=mysql --datadir="$DATA_DIR" --rpm
echo 'Database initialized'
mysqld_safe --pid-file=$PID_FILE --skip-networking --nowatch
mysql_options='--protocol=socket -uroot'
for i in `seq 30 -1 0`; do
if mysql $mysql_options -e 'SELECT 1' &> /dev/null; then
break
fi
echo 'MySQL init process in progress...'
sleep 1
done
if [ "$i" = 0 ]; then
echo >&2 'MySQL init process failed.'
exit 1
fi
if [ -z "$MYSQL_INITDB_SKIP_TZINFO" ]; then
apk add --update-cache tzdata
# sed is for https://bugs.mysql.com/bug.php?id=20545
mysql_tzinfo_to_sql /usr/share/zoneinfo | sed 's/Local time zone must be set--see zic manual page/FCTY/' | mysql $mysql_options mysql
fi
if [ ! -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
MYSQL_ROOT_PASSWORD="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c10)"
echo "GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
fi
mysql $mysql_options <<-EOSQL
-- What's done in this file shouldn't be replicated
-- or products like mysql-fabric won't work
SET @@SESSION.SQL_LOG_BIN=0;
DELETE FROM mysql.user ;
CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ;
DROP DATABASE IF EXISTS test ;
FLUSH PRIVILEGES ;
EOSQL
if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then
mysql_options="$mysql_options -p${MYSQL_ROOT_PASSWORD}"
fi
if [ "$MYSQL_DATABASE" ]; then
mysql $mysql_options -e "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;"
mysql_options="$mysql_options $MYSQL_DATABASE"
fi
if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
mysql $mysql_options -e "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;"
if [ "$MYSQL_DATABASE" ]; then
mysql $mysql_options -e "GRANT ALL ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'%' ;"
fi
mysql $mysql_options -e 'FLUSH PRIVILEGES ;'
fi
echo
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; mysql $mysql_options < "$f"; echo ;;
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | mysql $mysql_options; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
done
pid="`cat $PID_FILE`"
if ! kill -s TERM "$pid"; then
echo >&2 'MySQL init process failed.'
exit 1
fi
# make sure mysql completely ended
sleep 2
echo
echo 'MySQL init process done. Ready for start up.'
echo
fi
exec mysqld_safe --pid-file=$PID_FILE