sh mysql复制检查脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh mysql复制检查脚本相关的知识,希望对你有一定的参考价值。

#!/bin/bash

# replication delay threshold
TH_SECONDS_BEHIND=60

set -euo pipefail
IFS=$'\n\t'

SERVER=$(hostname -f)
ERRORS=()

# check that we are running on a slave
MYSQL_SLAVE=$(mysql -e 'show slave status\G')

if [ -z "${MYSQL_SLAVE}" ]
then
  echo "ERROR: this server is not a MySQL slave."
  exit 1
fi

# get mysql status
MYSQL_STATUS=( $(echo "${MYSQL_SLAVE}" | sed -e 's/^[[:space:]]*//g' ) )

for STATUS in "${MYSQL_STATUS[@]}"
do
  case "${STATUS}" in
    'Master_Host: '*)
      MYSQL_MASTER=${STATUS##*: }
      ;;
    'Slave_IO_Running: '*)
      MYSQL_SLAVE_IO=${STATUS##*: }
      ;;
    'Slave_SQL_Running: '*)
      MYSQL_SLAVE_SQL=${STATUS##*: }
      ;;
    'Last_Errno: '*)
      MYSQL_LAST_ERRNO=${STATUS##*: }
      ;;
    'Seconds_Behind_Master: '*)
      MYSQL_SECONDS_BEHIND=${STATUS##*: }
      ;;
  esac
done

# Check For Last Error
if [ ${MYSQL_LAST_ERRNO} != 0 ]
then
  ERRORS+=("ERROR: expected last_errno=0 but found last_errno=${MYSQL_LAST_ERRNO}.")
fi

# Check if IO thread is running
if [ "${MYSQL_SLAVE_IO}" != "Yes" ]
then
  ERRORS+=("ERROR: expected slave_io_running=Yes but found slave_io_running=${MYSQL_SLAVE_IO}.")
fi

# Check for SQL thread
if [ "${MYSQL_SLAVE_SQL}" != "Yes" ]
then
  ERRORS+=("ERROR: expected slave_sql_running=Yes but found slave_sql_running=${MYSQL_SLAVE_SQL}.")
fi

# Check replication delay
if [ ${MYSQL_SECONDS_BEHIND} != 'NULL' ] && [ ${MYSQL_SECONDS_BEHIND} -gt ${TH_SECONDS_BEHIND} ]
then
  ERRORS+=("ERROR: expected seconds_behind_master<${TH_SECONDS_BEHIND} but found seconds_behind_master=${MYSQL_SECONDS_BEHIND}.")
fi

# process (output/log/email any errors we found
if [ "${#ERRORS[@]}" -gt 0 ]
then
  for ERROR in "${ERRORS[@]}"
  do
    echo ${ERROR}
  done
  exit 1
fi

echo 'Replication Health is OK.'

以上是关于sh mysql复制检查脚本的主要内容,如果未能解决你的问题,请参考以下文章

MySQL复制状态检查脚本

sh 用于检查MySQL是否正在运行的简单bash脚本。

sh mysql自动检查运行状态自动重新启动脚本邮件提醒

sh Bash脚本安装Apache,MySQL和PHP以及PHPMyAdmin和一些调整。对于Debian和Ubuntu。要运行,请将脚本复制到se

mysql 自动备份脚本

mysql性能检查脚本-部分