1 |
#!/bin/bash |
2 |
|
3 |
set -m |
4 |
set -e |
5 |
|
6 |
VOLUME_HOME="/var/lib/mysql" |
7 |
CONF_FILE="/etc/mysql/conf.d/my.cnf" |
8 |
LOG="/var/log/mysql/error.log" |
9 |
|
10 |
# Set permission of config file |
11 |
chmod 644 ${CONF_FILE} |
12 |
chmod 644 /etc/mysql/conf.d/mysqld_charset.cnf |
13 |
|
14 |
StartMySQL () |
15 |
{ |
16 |
/usr/bin/mysqld_safe ${EXTRA_OPTS} > /dev/null 2>&1 & |
17 |
# Time out in 1 minute |
18 |
LOOP_LIMIT=60 |
19 |
for (( i=0 ; ; i++ )); do |
20 |
if [ ${i} -eq ${LOOP_LIMIT} ]; then |
21 |
echo "Time out. Error log is shown as below:" |
22 |
tail -n 100 ${LOG} |
23 |
exit 1 |
24 |
fi |
25 |
echo "=> Waiting for confirmation of MySQL service startup, trying ${i}/${LOOP_LIMIT} ..." |
26 |
sleep 1 |
27 |
mysql -uroot -e "status" > /dev/null 2>&1 && break |
28 |
done |
29 |
} |
30 |
|
31 |
CreateMySQLUser() |
32 |
{ |
33 |
if [ "$MYSQL_PASS" = "**Random**" ]; then |
34 |
unset MYSQL_PASS |
35 |
fi |
36 |
|
37 |
PASS=${MYSQL_PASS:-$(pwgen -s 12 1)} |
38 |
_word=$( [ ${MYSQL_PASS} ] && echo "preset" || echo "random" ) |
39 |
echo "=> Creating MySQL user ${MYSQL_USER} with ${_word} password" |
40 |
|
41 |
mysql -uroot -e "CREATE USER '${MYSQL_USER}'@'%' IDENTIFIED BY '$PASS'" |
42 |
mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '${MYSQL_USER}'@'%' WITH GRANT OPTION" |
43 |
echo "=> Done!" |
44 |
echo "========================================================================" |
45 |
echo "You can now connect to this MySQL Server using:" |
46 |
echo "" |
47 |
echo " mysql -u$MYSQL_USER -p$PASS -h<host> -P<port>" |
48 |
echo "" |
49 |
echo "Please remember to change the above password as soon as possible!" |
50 |
echo "MySQL user 'root' has no password but only allows local connections" |
51 |
echo "========================================================================" |
52 |
} |
53 |
|
54 |
OnCreateDB() |
55 |
{ |
56 |
if [ "$ON_CREATE_DB" = "**False**" ]; then |
57 |
unset ON_CREATE_DB |
58 |
else |
59 |
echo "Creating MySQL database ${ON_CREATE_DB}" |
60 |
mysql -uroot -e "CREATE DATABASE IF NOT EXISTS ${ON_CREATE_DB};" |
61 |
echo "Database created!" |
62 |
fi |
63 |
} |
64 |
|
65 |
ImportSql() |
66 |
{ |
67 |
for FILE in ${STARTUP_SQL}; do |
68 |
echo "=> Importing SQL file ${FILE}" |
69 |
if [ "$ON_CREATE_DB" ]; then |
70 |
mysql -uroot "$ON_CREATE_DB" < "${FILE}" |
71 |
else |
72 |
mysql -uroot < "${FILE}" |
73 |
fi |
74 |
done |
75 |
} |
76 |
|
77 |
# Main |
78 |
if [ ${REPLICATION_MASTER} == "**False**" ]; then |
79 |
unset REPLICATION_MASTER |
80 |
fi |
81 |
|
82 |
if [ ${REPLICATION_SLAVE} == "**False**" ]; then |
83 |
unset REPLICATION_SLAVE |
84 |
fi |
85 |
|
86 |
# Initialize empty data volume and create MySQL user |
87 |
if [[ ! -d $VOLUME_HOME/mysql ]]; then |
88 |
echo "=> An empty or uninitialized MySQL volume is detected in $VOLUME_HOME" |
89 |
echo "=> Installing MySQL ..." |
90 |
if [ ! -f /usr/share/mysql/my-default.cnf ] ; then |
91 |
cp /etc/mysql/my.cnf /usr/share/mysql/my-default.cnf |
92 |
fi |
93 |
mysql_install_db || exit 1 |
94 |
touch /var/lib/mysql/.EMPTY_DB |
95 |
echo "=> Done!" |
96 |
else |
97 |
echo "=> Using an existing volume of MySQL" |
98 |
fi |
99 |
|
100 |
# Set MySQL REPLICATION - MASTER |
101 |
if [ -n "${REPLICATION_MASTER}" ]; then |
102 |
echo "=> Configuring MySQL replication as master (1/2) ..." |
103 |
if [ ! -f /replication_set.1 ]; then |
104 |
RAND="$(date +%s | rev | cut -c 1-2)$(echo ${RANDOM})" |
105 |
echo "=> Writting configuration file '${CONF_FILE}' with server-id=${RAND}" |
106 |
sed -i "s/^#server-id.*/server-id = ${RAND}/" ${CONF_FILE} |
107 |
sed -i "s/^#log-bin.*/log-bin = mysql-bin/" ${CONF_FILE} |
108 |
touch /replication_set.1 |
109 |
else |
110 |
echo "=> MySQL replication master already configured, skip" |
111 |
fi |
112 |
fi |
113 |
|
114 |
# Set MySQL REPLICATION - SLAVE |
115 |
if [ -n "${REPLICATION_SLAVE}" ]; then |
116 |
echo "=> Configuring MySQL replication as slave (1/2) ..." |
117 |
if [ -n "${MYSQL_PORT_3306_TCP_ADDR}" ] && [ -n "${MYSQL_PORT_3306_TCP_PORT}" ]; then |
118 |
if [ ! -f /replication_set.1 ]; then |
119 |
RAND="$(date +%s | rev | cut -c 1-2)$(echo ${RANDOM})" |
120 |
echo "=> Writting configuration file '${CONF_FILE}' with server-id=${RAND}" |
121 |
sed -i "s/^#server-id.*/server-id = ${RAND}/" ${CONF_FILE} |
122 |
sed -i "s/^#log-bin.*/log-bin = mysql-bin/" ${CONF_FILE} |
123 |
touch /replication_set.1 |
124 |
else |
125 |
echo "=> MySQL replication slave already configured, skip" |
126 |
fi |
127 |
else |
128 |
echo "=> Cannot configure slave, please link it to another MySQL container with alias as 'mysql'" |
129 |
exit 1 |
130 |
fi |
131 |
fi |
132 |
|
133 |
|
134 |
echo "=> Starting MySQL ..." |
135 |
StartMySQL |
136 |
tail -F $LOG & |
137 |
|
138 |
# Create admin user and pre create database |
139 |
if [ -f /var/lib/mysql/.EMPTY_DB ]; then |
140 |
echo "=> Creating admin user ..." |
141 |
CreateMySQLUser |
142 |
OnCreateDB |
143 |
rm /var/lib/mysql/.EMPTY_DB |
144 |
fi |
145 |
|
146 |
|
147 |
# Import Startup SQL |
148 |
if [ -n "${STARTUP_SQL}" ]; then |
149 |
if [ ! -f /sql_imported ]; then |
150 |
echo "=> Initializing DB with ${STARTUP_SQL}" |
151 |
ImportSql |
152 |
touch /sql_imported |
153 |
fi |
154 |
fi |
155 |
|
156 |
# Set MySQL REPLICATION - MASTER |
157 |
if [ -n "${REPLICATION_MASTER}" ]; then |
158 |
echo "=> Configuring MySQL replication as master (2/2) ..." |
159 |
if [ ! -f /replication_set.2 ]; then |
160 |
echo "=> Creating a log user ${REPLICATION_USER}:${REPLICATION_PASS}" |
161 |
mysql -uroot -e "CREATE USER '${REPLICATION_USER}'@'%' IDENTIFIED BY '${REPLICATION_PASS}'" |
162 |
mysql -uroot -e "GRANT REPLICATION SLAVE ON *.* TO '${REPLICATION_USER}'@'%'" |
163 |
mysql -uroot -e "reset master" |
164 |
echo "=> Done!" |
165 |
touch /replication_set.2 |
166 |
else |
167 |
echo "=> MySQL replication master already configured, skip" |
168 |
fi |
169 |
fi |
170 |
|
171 |
# Set MySQL REPLICATION - SLAVE |
172 |
if [ -n "${REPLICATION_SLAVE}" ]; then |
173 |
echo "=> Configuring MySQL replication as slave (2/2) ..." |
174 |
if [ -n "${MYSQL_PORT_3306_TCP_ADDR}" ] && [ -n "${MYSQL_PORT_3306_TCP_PORT}" ]; then |
175 |
if [ ! -f /replication_set.2 ]; then |
176 |
echo "=> Setting master connection info on slave" |
177 |
mysql -uroot -e "CHANGE MASTER TO MASTER_HOST='${MYSQL_PORT_3306_TCP_ADDR}',MASTER_USER='${MYSQL_ENV_REPLICATION_USER}',MASTER_PASSWORD='${MYSQL_ENV_REPLICATION_PASS}',MASTER_PORT=${MYSQL_PORT_3306_TCP_PORT}, MASTER_CONNECT_RETRY=30" |
178 |
mysql -uroot -e "start slave" |
179 |
echo "=> Done!" |
180 |
touch /replication_set.2 |
181 |
else |
182 |
echo "=> MySQL replication slave already configured, skip" |
183 |
fi |
184 |
else |
185 |
echo "=> Cannot configure slave, please link it to another MySQL container with alias as 'mysql'" |
186 |
exit 1 |
187 |
fi |
188 |
fi |
189 |
|
190 |
fg |