您的位置:首页 > 数据库 > MySQL

MySQL主从同步

2016-01-17 15:06 399 查看

脚本

[root@test scripts]# cat ss.sh
#!/bin/bash
. /etc/init.d/functions
MYUSER=root
MYPASS=c565f972
SOCKET="/data/3307/mysql.sock"
MYCMD="/application/mysql/bin/mysql -u$MYUSER -p$MYPASS -S $SOCKET"
STATUS=($($MYCMD -e "show slave status\G;"|egrep "Seconds_Behind|_Running|Last_SQL_Errno"|awk '{print $NF}'))
ERRNU=(1158 1159 1008 1007 1062)
###############Effect#####################
Wait(){
action "MySql Slave is fail !" /bin/false
echo -n "Trying to skip the error ,wait 3s"
for ((i=0;i<4;i++))
do
echo -n "."
sleep 1
done
echo
}
Print(){
action "Skip error is ok" /bin/true
action "MySql Slave is ok" /bin/true
read -p "Show slave status{IO|SQL|Seconds_Behind_Master|Last_SQL_Errno} ? please enter yes
/no.   :" enter
case $enter in
yes)
echo "=================================================="
$MYCMD -e "show slave status\G;"|egrep "Seconds_Behind|_Running|Last_SQL_Errno"
echo "=================================================="
exit 0
;;
no)
exit 1
;;
esac
}

###############Check######################
Check_status(){
if [ "${STATUS[0]}" = "Yes" -a "${STATUS[1]}" = "Yes" -a "${STATUS[2]}" = "0" ]
then
action "MySql Slave is ok !" /bin/true
exit 0
else
sta=1
return $sta
fi
}
Check_err(){
Check_status
if [ $? -ne 0 ]
then
Wait            #调用前面的函数
for ((a=0;a<${#ERRNU[*]};a++))
do
A=`echo ${ERRNU[a]}`
if [ $A -eq "${STATUS[3]}" ]
then
$MYCMD -e "stop slave;"
$MYCMD -e "set global sql_slave_skip_counter = 1;"
$MYCMD -e "start slave;"
fi
done
fi
}
Check_again(){
STATUS=($($MYCMD -e "show slave status\G;"|egrep "Seconds_Behind|_Running|Last_SQL_Errno"|
awk '{print $NF}'))
if [ "${STATUS[0]}" = "Yes" -a "${STATUS[1]}" = "Yes" -a "${STATUS[2]}" = "0" ]
then
Print        #调用前面的函数,输入yes/no确认有没有跳过错误,看看状态
else
action "MySql Slave is fail" /bin/false
echo "MySql Slave is fail `date +%F\ %T`" >>/tmp/mysql_slave.log
mail -s "MySql Slave is fail `date +%F\ %T`" topaz1618@163.com </tmp/mysql_slave.log
fi
}

###############main######################
main(){
Check_err
Check_again
}
main


执行

没有问题的检查结果

[root@test scripts]# sh ss.sh
MySql Slave is ok !                                        [  OK  ]


有问题的检查结果

[root@test scripts]# sh ss.sh
MySql Slave is fail !                                      [FAILED]
Trying to skip the error ,wait 3s....                       #提示正在跳过错误
Skip error is ok                                           [  OK  ]   #修复成功
MySql Slave is ok                                          [  OK  ]   #MySQL主从服务好了
Show slave status{IO|SQL|Seconds_Behind_Master|Last_SQL_Errno} ? please enter yes/no.

                                          #敲yes,看状态确认,no会直接退出
:yes
==================================================
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0
Last_SQL_Errno: 0
==================================================
You have new mail in /var/spool/mail/root
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: