透过翻看 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 status\G’ >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新闻
【澳门金沙国际】Linux下MySQL主从同步监督shell脚本,shell脚本修复MySQL主从同步。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 status\G’ | awk -F:
‘{if($1~/Slave_IO_Running/) print$2}’ ` = “Yes” -a `mysql -uxxx
-pxxx -h127.0.0.1 -e ‘show slave status\G’ | 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
status\G” | 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}’

相关文章