这篇文章主要介绍了在CentOS系统下编写shell脚本来监控主从复制的教程,文中举了两个发现故障后再次执行复制命令的例子,需要的朋友可以参考下

目的:定时监控MySQL主从数据库是否同步,如果不同步,记录故障时间,并执行命令使主从恢复同步状态

1、创建脚本文件

vi /home/crontab/check_mysql_slave.sh  #编辑,添加下面代码
#!/bin/sh

# check_mysql_slave status

ip=eth0 #网卡名称

mysql_binfile=/usr/local/mysql/bin/mysql

mysql_user=root #MySQL数据库账号

mysql_pass=123456 #密码

mysql_sockfile=/tmp/mysql.sock

datetime=`date +"%Y-%m-%d/%H:%M:%S"`  #获取当前时间

mysql_slave_logfile=/home/logs/check_mysql_slave.log  #日志文件路径,必须提前创建好

slave_ip=`ifconfig $ip|grep "inet addr" | awk -F[:" "]+ '{print $4}'`

status=$($mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "show slave status\G" | grep -i "running")

Slave_IO_Running=`echo $status | grep Slave_IO_Running | awk ' {print $2}'`

Slave_SQL_Running=`echo $status | grep Slave_SQL_Running | awk '{print $2}'`

if [ "$Slave_IO_Running" = "Yes" -a "$Slave_SQL_Running" = "Yes" ]

then echo "Slave is Running!"

else

echo " $datetime $slave_ip Slave is not running!" >> $mysql_slave_logfile

$mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "SLAVE STOP;"

$mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1;"

$mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "SLAVE START;"

$mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "EXIT"

fi
:wq! #保存退出

chmod +x /home/crontab/check_mysql_slave.sh #添加脚本执行权限

2、添加任务计划,修改/etc/crontab

vi /etc/crontab #在最后一行添加

*/10 * * * * root /home/crontab/check_mysql_slave.sh #表示每10分钟执行一次

:wq! #保存退出

3、重新启动crond使设置生效

/etc/rc.d/init.d/crond restart  #yum install -y vixie-cron安装计划任务,某些系统上可能没有预装

chkconfig crond on #设为开机启动

service crond start #启动

可以根据日志文件/home/logs/check_mysql_slave.log查看MySQL主从同步状态

PS:接下来这个脚本增加了“当发现同步出现无法同步的时候”会自动提取主库的file号,以及pos,进行同步主库,脚本内容如下:

#!/bin/sh
#set -x
#file is slave_repl.sh
#Author by Kevin
#date is 2011-11-13 mstool="/usr/local/mysql-3307/bin/mysql -h 192.168.1.106 -uroot -pw!zl7POg27 -P 3307"
sltool="/usr/local/mysql-3307/bin/mysql -h 192.168.1.107 -uroot -pw!zl7POg27 -P 3307"
declare -a slave_stat
slave_stat=($($sltool -e "show slave status\G"|grep Running |awk '{print $2}'))
if [ "${slave_stat[0]}" = "Yes" -a "${slave_stat[1]}" = "Yes" ]
then
echo "OK slave is running"
exit 0
else
echo "Critical slave is error"
echo
echo "*********************************************************"
echo "Now Starting replication with Master Mysql!"
file=`$mstool -e "show master status\G"|grep "File"|awk '{print $2}'`
pos=`$mstool -e "show master status\G"|grep "Pos"|awk '{print $2}'`
$sltool -e "slave stop;change master to master_host='192.168.1.106',master_port=3307,master_user='repl',master_password='w!zl7POg27',master_log_file='$file',master_log_pos=$pos;slave start;"
sleep 3
$sltool -e "show slave status\G;"|grep Running
echo
echo "Now Replication is Finished!"
echo
echo "**********************************************************"
exit 2
fi
 
运行后效果,如下:
 
# ./slave_stop3307.sh
*******************************
Now stop Slave Replication!
Slave_IO_Running: No
Slave_SQL_Running: No
*******************************
# ./slave_repl3307.sh
Critical slave is error
*********************************************************
Now Starting replication with Master Mysql!
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Now Replication is Finished!
**********************************************************

CentOS下编写shell脚本来监控MySQL主从复制的教程的更多相关文章

  1. 在windows下编写shell脚本

    注意两点: 1.第一行:#!/bin/bash 2.将文档格式转换为unix,因为在windows下编写shell脚本回车符是\n\r,而linux下的回车符是\n,所以在linux下运行脚本的时候, ...

  2. shell监控脚本实例—监控mysql主从复制

    分享一例shell脚本,用于监测mysql数据库的主从复制,有需要的朋友不妨参考学习下. 转自:http://www.jbxue.com/article/14103.html(转载请注明出处) 本节内 ...

  3. linux环境下编写shell脚本实现启动停止tomcat服务

    第一步:以管理员的身份进入控制台,在指定目录下新建一个shell脚本,我这里命名为tomcat.sh 第二步:编写shell脚本 #!/bin/bash tomcat_home=/usr/tomcat ...

  4. 关于nagios系统下使用shell脚本自定义监控插件的编写以及没有实时监控图的问题

    关于nagios系统下shell自定义监控插件的编写.脚本规范以及没有实时监控图的问题的解决办法 在自已编写监控插件之前我们首先需要对nagios监控原理有一定的了解 Nagios的功能是监控服务和主 ...

  5. 关于nagios系统下使用shell脚本自定义监控插件的编写

    在自已编写监控插件之前我们首先需要对nagios监控原理有一定的了解 Nagios的功能是监控服务和主机,但是他自身并不包括这部分功能,所有的监控.检测功能都是通过各种插件来完成的. 启动Nagios ...

  6. shell脚本检测监控mysql的CPU占用率

    网站访问量大的时候mysql的压力就比较大,当mysql的CPU利用率超过300%的时候就不能提供服务了,近乎卡死状态,这时候最好的方法 就是重启mysql服务.由于这种事具有不可预见性,我们不知道什 ...

  7. windows下编写shell脚本执行错误

    在 windows 下,换行符是 \r\n,在linux下,换行符是 \n.如果你在IDEA里写sh脚本,可以手动设置脚本的换行符为 \n,如果你用notepad++写脚本,可以显示所有字符,以便明确 ...

  8. centos下yum搭建安装linux+apache+mysql+php环境教程

    我们利用linux系统中yum安装Apache+MySQL+PHP是非常的简单哦,只需要几步就可以完成,具体如下: 一.脚本YUM源安装: 1.yum install wget             ...

  9. Centos 6.4上面用Shell脚本一键安装mysql 5.6.15

    Centos 6.4上面用Shell脚本一键安装mysql 5.6.15  #!/bin/bash if [ `uname -m` == "x86_64" ];then machi ...

随机推荐

  1. sharepoint 2013 资源管理器copy大文件到本地失败解决方法

    Error 0x800700DF: The file size exceeds the limit allowed and cannot be saved 中文错误信息是:文件大小超出同意范围.不能被 ...

  2. 【WPF学习笔记】之如何保存画面上新建的数据到数据库中并且删除画面上的数据和数据库的数据:动画系列之(五)

    ...... 承接系列四后续: 首先,我要在用户控件2中添加“保存”,“删除”按钮. XAML代码: <UserControl x:Class="User.uc_item" ...

  3. 基于Apache POI 向xlsx写入数据

    [0]写在前面 0.1) these codes are from 基于Apache POI 的向xlsx写入数据 0.2) this idea is from http://cwind.iteye. ...

  4. Android-Android进程间通讯之messenger

    转自‘https://www.cnblogs.com/makaruila/p/4869912.html 平时一说进程间通讯,大家都会想到AIDL,其实messenger和AIDL作用一样,都可以进行进 ...

  5. python 基础 2.3 for 循环

    #/usr/bin/python #coding=utf-8 #@Time   :2017/10/16 10:05 #@Auther :liuzhenchuan #@File   :for 循环.py ...

  6. whl文件下载

    到哪找.whl文件?http://www.lfd.uci.edu/~gohlke/pythonlibs/

  7. Python爬虫--初识爬虫

    Python爬虫 一.爬虫的本质是什么? 模拟浏览器打开网页,获取网页中我们想要的那部分数据 浏览器打开网页的过程:当你在浏览器中输入地址后,经过DNS服务器找到服务器主机,向服务器发送一个请求,服务 ...

  8. AsyncTask==Handler+Thread对比使用说明

    AsyncTask能够合理且轻松使用UI线程,该类允许执行后台操作和发送结果到UI线程而不需要操作threads或handlers. AsyncTask是针对Thread和Handler代替而封装好的 ...

  9. linux-shell脚本命令之grep

    版权声明: https://blog.csdn.net/zdp072/article/details/26015611 [ grep简单介绍: ] grep是用来过滤含有特定字符的行, 能使用正則表達 ...

  10. JavaScript判断图片是否加载完成的三种方式 (转)

    一.load事件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <!DOCTYPE HTML> <html> <head>      ...