-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql_create_slave.sh
284 lines (187 loc) · 8.45 KB
/
mysql_create_slave.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# -----------------------------------------------------------------------------
# Name : mysql_create_slave.sh
# Description : create a slave
# Author : qiaolei
# Date : 2014/10/13
# -----------------------------------------------------------------------------
#!/bin/sh
usage()
{
echo "Usage: $0 <master_host> <master_mysql_user> <master_mysql_password> <slave_host> <slave_host_user> <slave_host_password> <slave_mysql_user> <slave_mysql_password> <slave_server_id>"
echo "<slave_server_id> is an unsigned integer, and is unique to each slave and should not use 1 (1 is used by master)"
}
if [ $# -ne 9 ]
then
usage
exit 1
fi
LOG_FILE=/tmp/mysql_scripts.log
if [ ! -f $LOG_FILE ];then
touch $LOG_FILE
fi
TIMESTAMP=`date +%Y%m%d%H%M%S`
echo "@$TIMESTAMP: {$0 $*}" | tee -a $LOG_FILE
MASTER_HOST=$1
MASTER_MYSQL_USER=$2
if [ $MASTER_MYSQL_USER = "NULL" ];then
MASTER_MYSQL_USER ="root"
fi
MASTER_MYSQL_PASSWORD=$3
SLAVE_HOST=$4
SLAVE_HOST_USER=$5
if [ $SLAVE_HOST_USER = "NULL" ];then
SLAVE_HOST_USER="root"
fi
SLAVE_HOST_PASSWORD=$6
SLAVE_MYSQL_USER=$7
if [ $SLAVE_MYSQL_USER = "NULL" ];then
SLAVE_MYSQL_USER ="root"
fi
SLAVE_MYSQL_PASSWORD=$8
SLAVE_SERVER_ID=$9
if [ $SLAVE_SERVER_ID = "NULL" ];then
SLAVE_SERVER_ID="2"
fi
REPL_USER="repl"
REPL_PASS="slavepass"
MYSQL_SCRIPTS_PATH=/usr/sbin/mysql_scripts
echo "creating slave{$SLAVE_HOST} for mysql master{$MASTER_HOST} with server id {$SLAVE_SERVER_ID}..." | tee -a $LOG_FILE
# STEP 1
echo -n -e "{\033[31m STEP 1/19 \033[0m: @{$MASTER_HOST}}: checking expect and try to install it if does not exist..."
rpm -q expect
if [ $? -ne 0 ];then
yum install expect -y >/dev/null
if [ $? -ne 0 ];then
echo "expect is mandatory and it doest not installed successfully. please install it manually."
exit 1
fi
echo "expect is installed successfully."
fi
echo "expect is already installed."
echo -e "[\033[32m DONE \033[0m]"
echo
# STEP 2
echo -n -e "{\033[31m STEP 2/19 \033[0m: @{$MASTER_HOST}}: generating a ssh key pair if does not exist..."
$MYSQL_SCRIPTS_PATH/mysql_ssh_keygen.sh
echo -e "[\033[32m DONE \033[0m]"
echo
# STEP 3
echo -n -e "{\033[31m STEP 3/19 \033[0m: @{$MASTER_HOST}}: copying ssh public key id to remote host{$SLAVE_HOST}..." | tee -a $LOG_FILE
$MYSQL_SCRIPTS_PATH/mysql_ssh_copyid.sh $SLAVE_HOST $SLAVE_HOST_USER $SLAVE_HOST_PASSWORD
echo -e "[\033[32m DONE \033[0m]"
echo
# STEP 4
echo -n -e "{\033[31m STEP 4/19 \033[0m: @{$MASTER_HOST}}: checking iptables status and try to stop it..." | tee -a $LOG_FILE
service iptables status
if [ $? -eq 0 ];then
service iptables stop
chkconfig iptables off
fi
echo -e "[\033[32m DONE \033[0m]"
echo
# STEP 5
echo -n -e "{\033[31m STEP 5/19 \033[0m: @{$MASTER_HOST}}: checking mysqld status and try to start it if necessary..." | tee -a $LOG_FILE
$MYSQL_SCRIPTS_PATH/mysql_service_action.sh status
if [ $? -ne 0 ];then
$MYSQL_SCRIPTS_PATH/mysql_service_action.sh start
fi
echo -e "[\033[32m DONE \033[0m]"
echo
# STEP 6
echo -n -e "{\033[31m STEP 6/19 \033[0m: @{$MASTER_HOST}}: creating replication user and grant privileges..." | tee -a $LOG_FILE
$MYSQL_SCRIPTS_PATH/mysql_create_replication_user.sh $MASTER_MYSQL_USER $MASTER_MYSQL_PASSWORD $REPL_USER $REPL_PASS %
echo -e "[\033[32m DONE \033[0m]"
echo
# STEP 7
echo -n -e "{\033[31m STEP 7/19 \033[0m: @{$MASTER_HOST}}: dumping mysql databases..." | tee -a $LOG_FILE
DUMPED_FILES=`$MYSQL_SCRIPTS_PATH/mysql_dump_databases.sh $MASTER_MYSQL_USER $MASTER_MYSQL_PASSWORD NULL`
echo -e "[\033[32m DONE \033[0m]"
echo
# STEP 8
echo -n -e "{\033[31m STEP 8/19 \033[0m: @{$MASTER_HOST}}: copying database files to remote host{$SLAVE_HOST}..." | tee -a $LOG_FILE
DUMPED_FILES=`$MYSQL_SCRIPTS_PATH/mysql_get_value_by_key.sh dumped_files "$DUMPED_FILES"`
DUMPED_FILES=${DUMPED_FILES//,/ }
for f in $DUMPED_FILES;do
$MYSQL_SCRIPTS_PATH/mysql_send_file.sh $SLAVE_HOST $SLAVE_HOST_USER $SLAVE_HOST_PASSWORD $f /tmp
done
echo -e "[\033[32m DONE \033[0m]"
echo
# STEP 9
echo -n -e "{\033[31m STEP 9/19 \033[0m: @{$MASTER_HOST}}: adding master configuration to /etc/my.cnf...." | tee -a $LOG_FILE
$MYSQL_SCRIPTS_PATH/mysql_master_cnf.sh 1
echo -e "[\033[32m DONE \033[0m]"
echo
# STEP 10
echo -n -e "{\033[31m STEP 10/19 \033[0m: @{$MASTER_HOST}}: restarting mysqld service..." | tee -a $LOG_FILE
$MYSQL_SCRIPTS_PATH/mysql_service_action.sh restart
echo -e "[\033[32m DONE \033[0m]"
echo
# STEP 11
echo -n -e "{\033[31m STEP 11/19 \033[0m: @{$MASTER_HOST}}: getting master status..." | tee -a $LOG_FILE
MASTER_STATUS=`$MYSQL_SCRIPTS_PATH/mysql_master_status.sh $MASTER_MYSQL_USER $MASTER_MYSQL_PASSWORD`
MASTER_LOG_FILE=`$MYSQL_SCRIPTS_PATH/mysql_get_value_by_key.sh File "$MASTER_STATUS"`
MASTER_LOG_POS=`$MYSQL_SCRIPTS_PATH/mysql_get_value_by_key.sh Position "$MASTER_STATUS"`
echo -e "[\033[32m DONE \033[0m]"
echo
# STEP 12
echo -n -e "{\033[31m STEP 12/19 \033[0m: @{$MASTER_HOST}}: getting all databases..." | tee -a $LOG_FILE
JSON_DATABASES=`$MYSQL_SCRIPTS_PATH/mysql_get_databases.sh $MASTER_MYSQL_USER $MASTER_MYSQL_PASSWORD`
DATABASES=`$MYSQL_SCRIPTS_PATH/mysql_get_value_by_key.sh databases "$JSON_DATABASES"`
echo -e "[\033[32m DONE \033[0m]"
echo
# STEP 13
echo -n -e "{\033[31m STEP 13/19 \033[0m: @{$SLAVE_HOST}}: checking iptables status and try to stop it..." | tee -a $LOG_FILE
$MYSQL_SCRIPTS_PATH/mysql_execute_command.sh ${SLAVE_HOST} ${SLAVE_HOST_USER} ${SLAVE_HOST_PASSWORD} "service iptables status;if [ "$?" = "0" ];then service iptables stop;chkconfig iptables off;fi"
echo -e "[\033[32m DONE \033[0m]"
echo
# STEP 14
echo -n -e "{\033[31m STEP 14/19 \033[0m: @{$SLAVE_HOST}}: checking mysqld status and try to start it if necessary..." | tee -a $LOG_FILE
$MYSQL_SCRIPTS_PATH/mysql_execute_command.sh ${SLAVE_HOST} ${SLAVE_HOST_USER} ${SLAVE_HOST_PASSWORD} "$MYSQL_SCRIPTS_PATH/mysql_service_action.sh restart"
echo -e "[\033[32m DONE \033[0m]"
echo
# STEP 15
echo -n -e "{\033[31m STEP 15/19 \033[0m: @{$SLAVE_HOST}}: creating databases if not exists..." | tee -a $LOG_FILE
$MYSQL_SCRIPTS_PATH/mysql_execute_command.sh ${SLAVE_HOST} ${SLAVE_HOST_USER} ${SLAVE_HOST_PASSWORD} "$MYSQL_SCRIPTS_PATH/mysql_create_databases.sh $SLAVE_MYSQL_USER $SLAVE_MYSQL_PASSWORD $DATABASES"
echo -e "[\033[32m DONE \033[0m]"
echo
# STEP 16
echo -n -e "{\033[31m STEP 16/19 \033[0m: @{$SLAVE_HOST}}: importing database files..." | tee -a $LOG_FILE
DB_NAME=""
for f in $DUMPED_FILES;do
DB_NAME=$f
DB_NAME=${DB_NAME//\//|}
DB_NAME=`echo $DB_NAME | awk -F'|' '{print $3}' | awk -F'-' '{print $1}'`
$MYSQL_SCRIPTS_PATH/mysql_execute_command.sh ${SLAVE_HOST} ${SLAVE_HOST_USER} ${SLAVE_HOST_PASSWORD} "$MYSQL_SCRIPTS_PATH/mysql_import_database.sh $SLAVE_MYSQL_USER $SLAVE_MYSQL_PASSWORD $DB_NAME $f"
done
echo -e "[\033[32m DONE \033[0m]"
echo
# STEP 17
echo -n -e "{\033[31m STEP 17/19 \033[0m: @{$SLAVE_HOST}}: adding slave configuration to /etc/my.cnf..." | tee -a $LOG_FILE
$MYSQL_SCRIPTS_PATH/mysql_execute_command.sh ${SLAVE_HOST} ${SLAVE_HOST_USER} ${SLAVE_HOST_PASSWORD} "$MYSQL_SCRIPTS_PATH/mysql_slave_cnf.sh $SLAVE_SERVER_ID"
echo -e "[\033[32m DONE \033[0m]"
echo
# STEP 18
echo -n -e "{\033[31m STEP 18/19 \033[0m: @{$SLAVE_HOST}}: restart mysqld service..." | tee -a $LOG_FILE
$MYSQL_SCRIPTS_PATH/mysql_execute_command.sh ${SLAVE_HOST} ${SLAVE_HOST_USER} ${SLAVE_HOST_PASSWORD} "$MYSQL_SCRIPTS_PATH/mysql_service_action.sh restart"
echo -e "[\033[32m DONE \033[0m]"
echo
# STEP 19
echo -n -e "{\033[31m STEP 19/19 \033[0m: @{$SLAVE_HOST}}: changing master and restart slave..." | tee -a $LOG_FILE
$MYSQL_SCRIPTS_PATH/mysql_execute_command.sh ${SLAVE_HOST} ${SLAVE_HOST_USER} ${SLAVE_HOST_PASSWORD} "$MYSQL_SCRIPTS_PATH/mysql_stop_slave.sh $SLAVE_MYSQL_USER $SLAVE_MYSQL_PASSWORD"
$MYSQL_SCRIPTS_PATH/mysql_execute_command.sh ${SLAVE_HOST} ${SLAVE_HOST_USER} ${SLAVE_HOST_PASSWORD} "$MYSQL_SCRIPTS_PATH/mysql_reset_slave.sh $SLAVE_MYSQL_USER $SLAVE_MYSQL_PASSWORD"
$MYSQL_SCRIPTS_PATH/mysql_execute_command.sh ${SLAVE_HOST} ${SLAVE_HOST_USER} ${SLAVE_HOST_PASSWORD} "$MYSQL_SCRIPTS_PATH/mysql_change_master.sh $SLAVE_MYSQL_USER $SLAVE_MYSQL_PASSWORD $MASTER_HOST $REPL_USER $REPL_PASS $MASTER_LOG_FILE $MASTER_LOG_POS"
$MYSQL_SCRIPTS_PATH/mysql_execute_command.sh ${SLAVE_HOST} ${SLAVE_HOST_USER} ${SLAVE_HOST_PASSWORD} "$MYSQL_SCRIPTS_PATH/mysql_start_slave.sh $SLAVE_MYSQL_USER $SLAVE_MYSQL_PASSWORD"
echo -e "[\033[32m DONE \033[0m]"
echo
RC=$?
CODE=-1
MSG=""
if [ $RC -eq 0 ];then
CODE=$RC
MSG="OK"
else
CODE=$RC
MSG="KO"
fi
RESULT=`printf "%s%s%s%s%s%s%s" "{" "\"result\":" "\"$CODE\"" "," "\"msg\":" "\"$MSG\"" "}"`
echo $RESULT