zabbix自定义key监察和控制mysql主从同步超轻易,检

通过查看 slave  状态,确保 Slave_IO_Running: Yes Slave_SQL_Running: Yes

发布:thebaby   来源:net     【大 中 小】

原理:利用在slave上运行show slave status获取Slave_IO_Running和Slave_SQL_Running的值

原理:

#!/bin/bash
#Author:Darius-D
mysql -uroot -p123123 -e 'show slave statusG' >a.txt
IO=`head -12 a.txt | tail -1|awk -F ':' '{print $2}'`
SQL=`head -13 a.txt | tail -1|awk -F ':' '{print $2}'`
if [ $IO == 'Yes' ];then
echo "Slave_IO_Running:Yes"
if [ $SQL == 'Yes' ];then
echo "Slave_SQL_Running:Yes"
else
echo "Slave_SQL_Running:No"
fi
else
echo "Slave_IO_Running:No"
fi
有关于mysql主从备份相关原理及备份链接进入查看。

分享一例shell脚本,用于修改mysql的主从同步问题,有需要的朋友参考下吧。

1.在zabbix客户端配置文件中加入:

1)监控MYSQL的运行状态;

一个可以修改mysql主从同步的shell脚本。

首先要对mysql提供一个查询主从状态的账号!(当然用root也可以。)

2)Slave机器的IO和SQL状态都必须为YES,缺一不可;

例子:  

grant replication client on *.* to 'xxx'@'localhost' identified by 'xxx';

注意,请结合自身的实际环境使用。

复制代码代码示例:

 flush privileges;

ps:网上搜了下,大致都存在同一个坑,echo输出的时候没有注意默认不转义换行的问题,会导致对Slave_SQL_Running的监控失去意义,实际上是把Slave_IO_Running的值重复监控了。

#!/bin/sh
#修复mysql主从同步
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
LOGFILE=/data/repair_mysql_sync_`date %F`.log
SQLCMD1="show slave status"
#查看MySQL是否启动
retval=`ps aux | grep mysqld | grep -v grep`
if [ "${retval}X" = "X" ]; then
    echo The MySQL is not running at: `date %F" "%H-%M-%S` >> ${LOGFILE}
    exit 1
fi
#获得MySQL从端Relay binlog的路径
retval=`grep "^relay-log" /etc/my.cnf | grep -v relay-log- | grep '/'`
if [ "${retval}" = "X" ]; then
    RELAY_BINLOG_PATH=`ps aux | grep -w mysqld | grep -v grep | awk '{print $13}' | awk -F '=' '{print $2}'`
else
    RELAY_BINLOG_PATH=`dirname $(echo ${retval} | awk -F '=' '{print $2}')`
fi
#查找master.info文件,用于定位Binlog信息
MASTER_FILE=`ps aux | grep -w mysqld | grep -v grep | awk '{print $13}' | awk -F '=' '{print $2}'`/master.info
if [ ! -e ${MASTER_FILE} ]; then
   echo This Server is not MySQL Slave at: `date %F" "%H-%M-%S` >> ${LOGFILE}
   exit 1
fi
#获得当前的同步状态
IO_STATUS=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Slave_IO_Running:" {print $2}'`
SQL_STATUS=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Slave_SQL_Running:" {print $2}'`
if [[ "${IO_STATUS}" = "Yes" && "${SQL_STATUS}" = "Yes" ]]; then
   echo Now, The MySQL Replication is synchronous at: `date %F" "%H-%M-%S` >> ${LOGFILE}
   exit 0
fi
#从master.info文件中,获得MySQL主端的同步信息
REPLI_INFO=`sed '/^$/d' ${MASTER_FILE} | tail 2 | head -5`
REPLI_BINLOG_FILE=`echo ${REPLI_INFO} | awk '{print $1}'`
REPLI_IPADDR=`echo ${REPLI_INFO} | awk '{print $3}'`
REPLI_USER=`echo ${REPLI_INFO} | awk '{print $4}'`
REPLI_PWD=`echo ${REPLI_INFO} | awk '{print $5}'`
#删除无用的Relay binlog
rm -rf ${RELAY_BINLOG_PATH}/*-relay-bin.*
#直接从0位置开始同步
SQLCMD2="change master to master_host='${REPLI_IPADDR}', master_user='${REPLI_USER}', master_password='${REPLI_PWD}',"
SQLCMD2="${SQLCMD2} master_log_file='${REPLI_BINLOG_FILE}', master_log_pos=0"
mysql -uroot -e "stop slave;"
mysql -uroot -e "${SQLCMD2};"
mysql -uroot -e "start slave;"
#如果同步的过程中,出现重复记录导致同步失败,就跳过
while true
do
   sleep 2
   IO_STATUS=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Slave_IO_Running:" {print $2}'`
   SQL_STATUS=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Slave_SQL_Running:" {print $2}'`
   BEHIND_STATUS=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Seconds_Behind_Master:" {print $2}'`
   SLAVE_BINLOG1=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Master_Log_File:" {print $2}'`
   SLAVE_BINLOG2=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Relay_Master_Log_File:" {print $2}'`
   #出现错误,就将错误信息记录到日志文件,并跳过错误继续同步
   if [[ "${IO_STATUS}" != "Yes" || "${SQL_STATUS}" != "Yes" ]]; then
       ERRORINFO=`mysql -uroot -e "${SQLCMD1}G;" | awk -F ': ' '$1=="Last_Error" {print $2}'`
       echo "The MySQL synchronous error information: ${ERRORINFO}" >> ${LOGFILE}
       mysql -uroot -e "stop slave;"
       mysql -uroot -e "set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;"
       mysql -uroot -e "start slave;"
       #已完成同步,就正常退出
   elif [[ "${IO_STATUS}" = "Yes" && "${SQL_STATUS}" = "Yes" && "${SLAVE_BINLOG1}" = "${SLAVE_BINLOG2}" && ${BEHIND_STATUS} -eq 0 ]]; then
      echo The MySQL synchronous is ok at: `date %F" "%H-%M-%S` >> ${LOGFILE}
      break
 fi
done

UserParameter=mysql.replicate_error,if test `mysql -uxxx -pxxx -h127.0.0.1 -e 'show slave statusG' | awk -F: '{if($1~/Slave_IO_Running/) print$2}' ` = "Yes" -a `mysql -uxxx -pxxx -h127.0.0.1 -e 'show slave statusG' | awk -F: '{if($1~/Slave_SQL_Running/) print$2}' ` = "Yes";then echo 0; else echo 1;fi

#!/bin/sh

ip=eth0  #网卡名称

mysql_binfile=/usr/bin/mysql

mysql_user=root  #MySQL数据库账号

mysql_pass=123456  #密码

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

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

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

status=$($mysql_binfile -u$mysql_user -p$mysql_pass -e "show slave statusG" | grep -i "running")

#验证方式:Slave_IO_Running=`echo -n "$status" | grep Slave_IO_Running | awk ' {print $1}'` (输出Slave_IO_Running为正常)

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

#验证方式:Slave_IO_Running=`echo -n "$status" | grep Slave_SQL_Running | awk ' {print $1}'` (输出Slave_SQL_Running为正常)

Slave_SQL_Running=`echo -n "$status" | grep Slave_SQL_Running | awk '{print $2}'` #网上有类似的代码,这里的echo应该加-n参数,并且变量应加引号,否则不转义换行符,实际匹配出来的结果是Slave_IO_Running参数的值

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

then

echo "Slave is Running!"

echo "$datetime $slave_ip Slave is Running!" >> $mysql_slave_logfile

else

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

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

fi

您可能感兴趣的文章:
自动配置mysql主从的shell脚本
监控mysql主从健康状态的shell脚本
mysql主从监控的shell脚本

2.在web界面创建模板:

本文地址:

图片 1

创建项目

图片 2

添加触发器;

图片 3

添加动作略。

 

 

4.分享下其他服务的key:

UserParameter=nginx.status,netstat -tunlp | grep 80 | grep nginx | wc | awk '{print $1}'

UserParameter=memcached.stats,netstat -tunlp | grep 11211 | grep memcache | wc | awk '{print $1}'

本文由星彩网app下载发布于星彩网app下载,转载请注明出处:zabbix自定义key监察和控制mysql主从同步超轻易,检

TAG标签: 星彩网app下载
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。