docker-entrypoint.sh
2.15 KB
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
#!/bin/bash
MYSQL_DATA='/var/lib/mysql'
MYSQL_FILE_BEGIN="$MYSQL_DATA/MYSQL_BEGIN"
function VerifyCredintials {
local USER=$1
local PASSWORD=$2
local DATABASE=$3
if [ -z "$USER" ] || [ -z "$PASSWORD" ]; then
echo "ERROR: MYSQL_USER and MYSQL_PASSWORD cannot be empty." > /dev/stderr
exit -1
fi
if [ "$USER" = "root" ]; then
echo "ERROR: MYSQL_USER cannot be the root account." > /dev/stderr
exit -1
fi
if [ -z "$DATABASE" ]; then
MYSQL_DATABASE="$USER"
fi
}
function CreateInitialDatabase {
local DATADIR=$1
# mysql_install_db --datadir="$DATADIR" --user='mysql' --insecure --verbose
mysqld --initialize-insecure --datadir="$DATADIR" --user="mysql"
if [ $? -ne 0 ]; then
echo "ERROR: Could not create an initial database." > /dev/stderr
exit -1
fi
}
function CreateSuperuser {
local USER=$1
local PASSWORD=$2
mysql -u root --protocol=socket --wait -e "CREATE USER '$USER' IDENTIFIED BY '$PASSWORD';"
mysql -u root --protocol=socket --wait -e "GRANT ALL PRIVILEGES ON $MYSQL_DATABASE.* TO '$USER';"
mysql -u root --protocol=socket --wait -e "FLUSH PRIVILEGES;"
}
function CreateDatabase {
local DATABASE=$1
mysql -u root --protocol=socket --wait -e "CREATE DATABASE IF NOT EXISTS $DATABASE;"
}
function CreateFileBegin {
local FILE=$1
date +%c > $FILE
}
function StartDatabaseServer {
mysqld $@
}
function StartDatabaseServerBackground {
mysqld &
sleep 5
}
function StopDatabaseServer {
mysqladmin -u root shutdown
sleep 5
}
function RestoreDatabase {
mysqladmin -u root refresh
for file in `ls /docker-entrypoint-initdb.d/*`; do
case $file in
*.sh )
echo "Running '$file'..."
. $file ;;
esac
done
}
function Main {
if [ ! -f $MYSQL_FILE_BEGIN ]; then
VerifyCredintials $MYSQL_USER $MYSQL_PASSWORD $MYSQL_DATABASE
CreateInitialDatabase $MYSQL_DATA
StartDatabaseServerBackground
CreateDatabase $MYSQL_DATABASE
CreateSuperuser $MYSQL_USER $MYSQL_PASSWORD
RestoreDatabase
StopDatabaseServer
CreateFileBegin $MYSQL_FILE_BEGIN
cat /var/log/mysqld.log
fi
StartDatabaseServerBackground
tail -f /var/log/mysqld.log
}
Main $@