MySql 从库根据show slave status返回参数判断延时shell脚本

该bash脚本用于监控MySQL主从复制的状态,包括SLAVE_IO及SLAVE_SQL线程、主从延迟和日志位置。如果发现SLAVE_IO_S&SLAVE_SQL_S参数异常或Seconds_Behind_Master超过1秒,会将错误信息记录到日志文件中。同时,它还会检查从库是否与主库的日志位置同步。
摘要由CSDN通过智能技术生成
#!/bin/bash

slave='slave1'
#formatDate1=`date "+%Y-%m-%d %H:%M:%S"`
user='root'
password='password'
host='127.0.0.1'
port='9310'
doCount=10

catalog='/home/script/leadLog.txt'

while [ $doCount -ge 1 ]
do  
  sleep 0.5s
  let doCount--
  
  formatDate=`date "+%Y-%m-%d %H:%M:%S"`
  #SLAVE_IO_S&SLAVE_SQL_S is YES
  thread_status=`mysql -P"$port" -h"$host" -u"$user" -p"$password" -e 'show slave status\G'|grep -i yes|wc -l`
  status=(`mysql -P"$port" -h"$host" -u"$user" -p"$password" -e 'show slave status\G'| egrep -i "Master_Log_File|Relay_Master_Log_File|Read_master_log_Pos|Exec_Master_log_pos|Seconds_Behind_Master" |awk -F':' '{print $2}'`)

  echo "${formatDate}-${slave}-INFO : SLAVE_IO_S&SLAVE_SQL_S:${thread_status},status:Master_Log_File:${status[0]},Relay_Master_Log_File:${status[1]},Read_master_log_Pos:${status[2]},Exec_Master_log_pos:${status[3]},Seconds_Behind_Master:${status[4]}" >> $catalog

  if [[ "$thread_status" != 2 ]]; then
      echo "${formatDate}-${slave}-ERROR : SLAVE_IO_S & SLAVE_SQL_S 参数异常请检查!" >> $catalog
  fi

  if [[ "${status[4]}" -gt "1" ]];then
      echo "${formatDate}-${slave}-ERROR :Seconds_Behind_Master=${status[4]},延迟过高,请注意!"  >> $catalog
  fi

  if [[ ${status[0]} == ${status[2]} ]] && [[ ${status[1]} == ${status[3]} ]]; then
      echo "${formatDate}-${slave}-INFO : 监听主从参数正常!"
    else
      echo "${formatDate}-${slave}-ERROR : 从库同步主控LOG日志点位落后,请注意!" >> $catalog
  fi

done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值