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
相关文章推荐
- Solr Dataimporthandler 导入MySQL 内存溢出
- mysql 数据库 如何获取刚刚插入的自增长的id号
- 【数据库】Mysql中的函数
- mysql 父子结构排序
- Can't connect to local MySQL server through socket '/tmp/mysql.sock'
- 在mac上安装mysql5.7.10
- mysql之临时表【整理】
- Mycat学习笔记 第二篇. MySql 读写分离与日志分析——主从多结点
- Mysql 数据类型--课程笔记
- mysql安装教程
- Mac下使用MySQL
- mysql安装配置
- MySQL 常用语法总结
- 【数据库】Mysql中的存储过程
- mysql创建用户
- 安装mysql
- MySQL函数大全系列(字符串操作)
- MySQL5.7.10下载及安装及配置
- MySql 学习笔记
- Mysql中LAST_INSERT_ID()的函数使用详解