-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmysql_cluster-entrypoint.sh
executable file
·191 lines (150 loc) · 6.54 KB
/
mysql_cluster-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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/bash
set -e
if [ -z "$NODE_TYPE" ]; then
echo >&2 'error: Cluster node type is required'
echo >&2 ' You need to specify NODE_TYPE with a possible value of sql, management, or data'
exit 1
fi
# if command starts with an option, save them as CMD arguments
if [ "${1:0:1}" = '-' ]; then
ARGS="$@"
fi
# If we're setting up a mysqld/SQL API node
if [ "$NODE_TYPE" = 'sql' ]; then
echo 'Setting up node as a new MySQL API node...'
CMD="mysqld"
# we need to ensure that they have specified and endpoint for an existing management server
if [ -z "$MANAGEMENT_SERVER" ]; then
echo >&2 'error: Cluster management server is required'
echo >&2 ' You need to specify MANAGEMENT_SERVER=<hostname> in order to setup this new SQL node'
exit 1
fi
# now we need to ensure that we can communicate with the management server
# would like to use `ndb_mgm -t 0 -c "$MANAGEMENT_SERVER" -e "49 status"` but you can't disable the retry...
if ! $(nc -z "$MANAGEMENT_SERVER" 1186 >& /dev/null); then
echo >&2 "error: Could not reach the specified Cluster management server at $MANAGEMENT_SERVER"
echo >&2 ' You need to specify a valid MANAGEMENT_SERVER=<hostname> option in order to setup this new sql node'
exit 1
fi
# Get config
DATADIR="$("$CMD" --verbose --help --log-bin-index=/tmp/tmp.index 2>/dev/null | awk '$1 == "datadir" { print $2; exit }')"
if [ ! -d "$DATADIR/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
# If the password variable is a filename we use the contents of the file
if [ -f "$MYSQL_ROOT_PASSWORD" ]; then
MYSQL_ROOT_PASSWORD="$(cat $MYSQL_ROOT_PASSWORD)"
fi
mkdir -p "$DATADIR"
chown -R mysql:mysql "$DATADIR"
echo -n 'Initializing database... '
"$CMD" --initialize-insecure=on
echo 'done'
"$CMD" --skip-networking &
pid="$!"
mysql=( mysql --protocol=socket -uroot )
for i in {30..0}; do
if echo 'SELECT 1' | "${mysql[@]}" &> /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
mysql_tzinfo_to_sql /usr/share/zoneinfo | "${mysql[@]}" mysql
if [ ! -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
MYSQL_ROOT_PASSWORD="$(pwmake 128)"
echo "GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
fi
"${mysql[@]}" <<-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 WHERE user NOT IN ('mysql.sys', 'mysqlxsys');
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+=( -p"${MYSQL_ROOT_PASSWORD}" )
fi
if [ "$MYSQL_DATABASE" ]; then
echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" | "${mysql[@]}"
mysql+=( "$MYSQL_DATABASE" )
fi
if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
echo "CREATE USER '"$MYSQL_USER"'@'%' IDENTIFIED BY '"$MYSQL_PASSWORD"' ;" | "${mysql[@]}"
if [ "$MYSQL_DATABASE" ]; then
echo "GRANT ALL ON \`"$MYSQL_DATABASE"\`.* TO '"$MYSQL_USER"'@'%' ;" | "${mysql[@]}"
fi
echo 'FLUSH PRIVILEGES ;' | "${mysql[@]}"
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[@]}" < "$f" && echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
done
if [ ! -z "$MYSQL_ONETIME_PASSWORD" ]; then
"${mysql[@]}" <<-EOSQL
ALTER USER 'root'@'%' PASSWORD EXPIRE;
EOSQL
fi
if ! kill -s TERM "$pid" || ! wait "$pid"; then
echo >&2 'MySQL init process failed!'
exit 1
fi
echo 'MySQL API node init process complete. Ready for node start up ...'
fi
chown -R mysql:mysql "$DATADIR"
mkdir /var/lib/mysql-files
chown -R mysql:mysql /var/lib/mysql-files
CMD="mysqld --ndb_connectstring=$MANAGEMENT_SERVER:1186 $ARGS"
# If we're setting up a management node
elif [ "$NODE_TYPE" = 'management' ]; then
# if they're bootstrapping a new cluster, then we just need to start with a fresh Cluster
if [ ! -z "$BOOTSTRAP" ]; then
echo 'Bootstrapping new Cluster with a fresh management node ...'
# otherwise we need to ensure that they have specified endpoint info for an existing ndb_mgmd node
elif [ ! -z "$MANAGEMENT_SERVER" ]; then
echo "Adding new management node and registering it with the existing server: $MANAGEMENT_SERVER ..."
else
echo >&2 'error: Cluster management node is required'
echo >&2 ' You need to specify MANAGEMENT_SERVER=<hostname> in order to add a new managmeent node, or you must specify BOOTSTRAP in order to create a new Cluster'
exit 1
fi
mkdir /var/lib/ndb/management
CMD="ndb_mgmd --config-file=/etc/mysql/cluster-config.ini --config-dir=/etc/mysql --nodaemon=TRUE $ARGS"
# If we're setting up a data node
elif [ "$NODE_TYPE" = 'data' ]; then
echo 'Setting up node as a new MySQL Cluster data node ...'
# we need to ensure that they have specified endpoint info for an existing ndb_mgmd node
if [ -z "$MANAGEMENT_SERVER" ]; then
echo >&2 'error: Cluster management server is required'
echo >&2 ' You need to specify MANAGEMENT_SERVER=<hostname> in order to setup this new data node'
exit 1
fi
# now we need to ensure that we can communicate with the management server
# would like to use `ndb_mgm -t 0 -c "$MANAGEMENT_SERVER" -e "49 status"` but you can't disable the retry...
if ! $(nc -z "$MANAGEMENT_SERVER" 1186 >& /dev/null); then
echo >&2 "error: Could not reach the specified Cluster management server at $MANAGEMENT_SERVER"
echo >&2 ' You need to specify a valid MANAGEMENT_SERVER=<hostname> option in order to setup this new data node'
exit 1
fi
mkdir /var/lib/ndb/data
# then we'll start an ndbmtd process in this container
CMD="ndbmtd --ndb_connectstring=$MANAGEMENT_SERVER:1186 --nodaemon=TRUE $ARGS"
else
echo 'Invalid node type set. Valid node types are sql, management, and data.'
fi
exec $CMD