5.6MySql主从自动切换脚本
2015.07.17
-
#!/bin/bash
-
cat << README
-
#####################################################################################################
-
#Auther :zhanglin #
-
#Date : 2015.07.17 #
-
#Step 1: point the slave IP #
-
#Step 2: check the master and slave information whether good for change #
-
#Step 3: stop old slave,then get new master binlog name and postation,then execute change master to #
-
#Step 4: start slave,and show whether change successed. #
-
#####################################################################################################
-
README
-
User=root
-
PW=123456
-
read -p "-- Please input the slave IP:" Slave
-
Master=$(MySQL -u${User} -h${Slave} -p${PW} -e "show slave status \G;"|awk '/Master_Host/{print $2}')
-
if [ -n "${Master}" ]
-
then
-
echo -e "--Master IP:${Master},Slave IP:${Slave}"
-
M=$(mysql -u${User} -h${Master} -p${PW} -e "show master status;"|awk 'NR==2{print $2}')
-
S=$(mysql -u${User} -h${Slave} -p${PW} -e "show slave status \G;"|awk '/Read_Master_Log_Pos/{print $2}')
-
echo -e "-- master pos:${M};slave pos is:${S}"
-
else
-
echo "-- Slave IP input wrong,please input again ! "
-
exit 1
-
fi
-
if [ "${M}" -eq "${S}" ]
-
then
-
read -p "-- Master-Slave is accordance,input Yes to start changing:" var
-
case "$var" in
-
[Yy]es)
-
mysql -u${User} -h${Slave} -p${PW} -e "stop slave;reset slave;change master to master_host='';"
-
Pos=$(mysql -u${User} -h${Slave} -p${PW} -e "show master status;"|awk 'NR==2{print $2}')
-
File=$(mysql -u${User} -h${Slave} -p${PW} -e "show master status;"|awk 'NR==2{print $1}')
-
mysql -u${User} -h${Master} -p${PW} -e "stop slave;
-
change master to master_user='ideal',master_host='${Slave}',master_password='123456',master_log_file='${File}',master_log_pos=${Pos};
-
start slave;"
-
;;
-
*)
-
echo "-- error input .... exit!"
-
;;
-
esac
-
echo "-- changing, please wait 3s...."
-
sleep 3
-
echo "-- change successfull,the new master is:${Slave},new slave is ${Master}"
-
SlaveIOStatus=$(mysql -u${User} -h${Master} -p${PW} -e "show slave status \G;"|awk '/Slave_IO_Running/{print $2}')
-
SlaveSQLStatus=$(mysql -u${User} -h${Master} -p${PW} -e "show slave status \G;"|awk '/Slave_SQL_Running/{print $2}')
-
printf "The new master is: \n Slave_IO_Running=${SlaveIOStatus}\n"
-
printf "Slave_SQL_Running=${SlaveSQLStatus}\n"
-
else
-
echo "node change failed please change again ! "
-
exit 1
-
fi
文章题目:5.6MySql主从自动切换脚本
网页网址:
http://cdxtjz.cn/article/picgog.html