MySQL 主从状态监控

2021/4/25 19:26:37

本文主要是介绍MySQL 主从状态监控,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

#!/bin/bash
USERNAME="monitor"
PASSWORD='123456'
SLAVE_HOST="127.0.0.1"
SLAVE_PORT="3307"
MYSQL="/usr/local/mysql/bin/mysql -u$USERNAME -p$PASSWORD"
SLAVE="$MYSQL -h $SLAVE_HOST -P $SLAVE_PORT"

datetime=`date +"%Y-%m-%d %H:%M:%S"`
#查看复制状态
MySQL_Status=`$SLAVE -e "SHOW SLAVE STATUS\G"  | grep -E "Running|Seconds_Behind_Master"|head -n3 `
#获取状态
Slave_IO_Running=`echo $MySQL_Status | grep Slave_IO_Running |awk '{print $2}'`
Slave_SQL_Running=`echo $MySQL_Status |grep Slave_SQL_Running |awk '{print $4}'`
Seconds_Behind_Master=`echo $MySQL_Status |grep Seconds_Behind_Master |awk '{print $6}'`
#IO和SQL线程监控
if [ "$Slave_IO_Running" != "Yes" -o "$Slave_SQL_Running" != "Yes" ]
then

    BODY="$datetime Slave_IO_Running:$Slave_IO_Running Slave_SQL_Running:$Slave_SQL_Running Seconds_Behind_Master:$Seconds_Behind_Master 外网MySQL slave replication error!"
    echo  $BODY
  else
#延时监控,不精准
   if [ "$Seconds_Behind_Master" -gt "180" ]
    then
      BODY="$datetime Slave_IO_Running:$Slave_IO_Running Slave_SQL_Running:$Slave_SQL_Running Seconds_Behind_Master:$Seconds_Behind_Master 外网MySQL slave replication error!"
      echo  $BODY

    fi

fi

 



这篇关于MySQL 主从状态监控的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程