-
Notifications
You must be signed in to change notification settings - Fork 1
/
run-db.sh
executable file
·122 lines (101 loc) · 3.91 KB
/
run-db.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/sh
# Returns true once mysql can connect.
mysql_ready() {
mysqladmin ping --host=localhost --user=root --password=${MYSQL_ROOT_PASSWORD:-""} > /dev/null 2>&1
}
echo "[i] Access data: $MYSQL_ROOT_PASSWORD MYSQL_DATABASE=${MYSQL_DATABASE:-""} MYSQL_USER=${MYSQL_USER:-""} MYSQL_PASSWORD=${MYSQL_PASSWORD:-""}"
# execute any pre-init scripts
for i in /scripts/pre-init.d/*sh
do
if [ -e "${i}" ]; then
echo "[i] pre-init.d - processing $i"
. "${i}"
fi
done
if [ -d "/run/mysqld" ]; then
echo "[i] mysqld already present, skipping creation"
chown -R mysql:mysql /run/mysqld
else
echo "[i] mysqld not found, creating...."
mkdir -p /run/mysqld
chown -R mysql:mysql /run/mysqld
fi
if [ -d /var/lib/mysql/mysql ]; then
echo "[i] MySQL directory already present, skipping creation"
chown -R mysql:mysql /var/lib/mysql
else
echo "[i] MySQL data directory not found, creating initial DBs"
chown -R mysql:mysql /var/lib/mysql
# supress output
mysql_install_db --user=mysql --ldata=/var/lib/mysql/ > /dev/null
if [ "$MYSQL_ROOT_PASSWORD" = "" ]; then
MYSQL_ROOT_PASSWORD=`pwgen 16 1`
else
MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-""}
fi
echo "[i] Access data: $MYSQL_ROOT_PASSWORD MYSQL_DATABASE=${MYSQL_DATABASE:-""} MYSQL_USER=${MYSQL_USER:-""} MYSQL_PASSWORD=${MYSQL_PASSWORD:-""}"
tfile=`mktemp`
if [ ! -f "$tfile" ]; then
return 1
fi
cat << EOF > $tfile
USE mysql;
FLUSH PRIVILEGES;
SET PASSWORD FOR root@'localhost' = PASSWORD('$MYSQL_ROOT_PASSWORD');
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' identified by '$MYSQL_ROOT_PASSWORD' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' identified by '$MYSQL_ROOT_PASSWORD' WITH GRANT OPTION;
DROP DATABASE test;
EOF
#UPDATE user SET password=PASSWORD("") WHERE user='root' AND host='localhost';
if [ "$MYSQL_DATABASE" != "" ]; then
echo "[i] Creating database: $MYSQL_DATABASE"
echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` CHARACTER SET utf8 COLLATE utf8_general_ci;" >> $tfile
if [ "$MYSQL_USER" != "" ]; then
echo "[i] Creating user: $MYSQL_USER with password $MYSQL_PASSWORD"
echo "CREATE USER '$MYSQL_USER' IDENTIFIED BY '$MYSQL_PASSWORD';" >> $tfile
echo "SET PASSWORD FOR '$MYSQL_USER'@'%' = PASSWORD('$MYSQL_PASSWORD');" >> $tfile
echo "GRANT ALL PRIVILEGES ON \`$MYSQL_DATABASE\`.* to '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD';" >> $tfile
echo "GRANT ALL PRIVILEGES ON \`$MYSQL_DATABASE\`.* to '$MYSQL_USER'@'localhost' IDENTIFIED BY '$MYSQL_PASSWORD';" >> $tfile
fi
fi
echo "[i] Trying to do all of the above"
/usr/bin/mysqld --user=mysql --bootstrap --skip-name-resolve < $tfile
echo "[i] Removing mktemp file"
rm -f $tfile
echo
echo 'MySQL init process done. Ready for start up.'
echo
nohup /usr/bin/mysqld --user=mysql --console --skip-name-resolve "$@" &
while !(mysql_ready)
do
sleep 3
echo "[i] Waiting for mysql to come up..."
done
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
*.sql) echo "$0: running $f"; mysql -u root -p$MYSQL_ROOT_PASSWORD $MYSQL_DATABASE < "$f"; echo ;;
# *.sql) echo "$0: running $f"; mysql -u $MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE < "$f"; echo ;;
# *.sql) echo "$0: running $f"; /usr/bin/mysqld --verbose=4 --user=mysql --bootstrap --skip-name-resolve < "$f"; echo ;;
# *.sql) echo "$0: running $f"; mysqld --user=mysql --bootstrap --skip-name-resolve < "$f"; echo ;;
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | mysql -u $MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE < "$f"; echo ;;
*) echo "$0: ignoring or entrypoint initdb empty $f" ;;
esac
echo
done
# exec killall mysqld
fi
# execute any pre-exec scripts
for i in /scripts/pre-exec.d/*sh
do
if [ -e "${i}" ]; then
echo "[i] pre-exec.d - processing $i"
. ${i}
fi
done
if !(mysql_ready); then
exec /usr/bin/mysqld --user=mysql --console --skip-name-resolve $@
fi
while (mysql_ready)
do
sleep 5
done