-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-entrypoint.sh
96 lines (80 loc) · 2.38 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
#!/bin/bash
#
# Multi architecture MySQL docker image
# Copyright 2022 Thu Dinh
#
DATADIR='/var/lib/mysql';
initialize() {
MYSQL_ROOT_PASSWORD="${MYSQL_ROOT_PASSWORD:-dev}"
MYSQL_PASSWORD="${MYSQL_PASSWORD:-dev}"
MYSQL_USER="${MYSQL_USER:-dev}"
MYSQL_ROOT_HOST="${MYSQL_ROOT_HOST:-%}"
$DATADIR="${DATADIR:-data}"
echo "> Initializing database"
mkdir -p /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql
mysqld --initialize-insecure --user=mysql
# Start temporary server
echo "> Starting temporary server";
if ! mysqld --daemonize --skip-networking --user=mysql; then
echo "Error starting mysqld"
exit 1
fi
echo "> Setting root password";
echo
echo "Password: $MYSQL_ROOT_PASSWORD"
if [ "$MYSQL_ROOT_HOST" != 'localhost' ]; then
mysql <<EOF
CREATE USER 'root'@'${MYSQL_ROOT_HOST}' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}';
GRANT ALL ON *.* TO 'root'@'${MYSQL_ROOT_HOST}' WITH GRANT OPTION;"
EOF
fi
mysql <<EOF
ALTER USER 'root'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}';
GRANT ALL ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EOF
# work similar to official mysql docker image
if [ -n "$MYSQL_DATABASE" ]; then
echo "> Creating database $MYSQL_DATABASE";
mysql -p"$MYSQL_ROOT_PASSWORD" <<< "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\`;";
fi
if [ -n "$MYSQL_USER" ] && [ -n "$MYSQL_PASSWORD" ]; then
echo "> Creating user";
echo
echo "User: $MYSQL_USER"
echo "Password: $MYSQL_PASSWORD"
echo
mysql -p"$MYSQL_ROOT_PASSWORD" <<< "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD';"
echo "> Granting permissions";
if [ -n "$MYSQL_DATABASE" ]; then
mysql -p"$MYSQL_ROOT_PASSWORD" <<< "GRANT ALL ON \`${MYSQL_DATABASE}\`.* TO '$MYSQL_USER'@'%';";
fi
fi
# Shutdown temporary server
echo "> Shutting down temporary server";
if ! mysqladmin shutdown -uroot -p"$MYSQL_ROOT_PASSWORD" ; then
echo "Error shutting down mysqld"
exit 1
fi
echo "> Complete";
}
# Initialize if needed
if [ "$1" = 'mysqld' ]; then
if [ ! -d "$DATADIR/mysql" ]; then
initialize;
fi
fi
cat <<EOF
__ ___ _____ ____ __
/ |/ /_ __/ ___// __ \ / /
/ /|_/ / / / /\__ \/ / / / / /
/ / / / /_/ /___/ / /_/ / / /___
/_/ /_/\__, //____/\___\_\/_____/
/____/
EOF
if [ "$1" = 'mysqld' ]; then
exec "$@" "--user=mysql"
else
exec "$@"
fi