Today in this article we will get an idea about how to monitor MySQL replication. It is today’s requirement to setup replication in your environment but is it enough? My answer is ‘NO’. We need to monitor our replication from time to time. I wrote a small script to monitor our environment, which I am going to share with you guys to make your life as easy as it made mine.
Things that need to monitor on your slave side:
LAST_ERRNO
SECONDS_BEHIND_MASTER
IO_IS_RUNNING
SQL_IS_RUNNING
MASTER_LOG_FILE
RELAY_MASTER_LOG_FILE
These all variables are the part of ‘show slave status’
Save the below script in .sh file and execute is as below:
./mysqlhealthcheck.sh
MYSQL_CHECK=$(./mysql-uUsername -ppassword -e "SHOW VARIABLES LIKE '%version%';" || echo 1) #echo $MYSQL_CHECK STATUS_LINE=$(./mysql-uUsername -ppassword -e "SHOW SLAVE STATUS\G")"1" LAST_ERRNO=$(grep "Last_Errno" <<< "$STATUS_LINE" | awk '{ print $2 }') SECONDS_BEHIND_MASTER=$( grep "Seconds_Behind_Master" <<< "$STATUS_LINE" | awk '{ print $2 }') IO_IS_RUNNING=$( grep "Slave_IO_Running" <<< "$STATUS_LINE" | awk '{ print $2 }') SQL_IS_RUNNING=$(grep "Slave_SQL_Running" <<< "$STATUS_LINE" | awk '{ print $2 }') MASTER_LOG_FILE=$(grep " Master_Log_File" <<< "$STATUS_LINE" | awk '{ print $2 }') RELAY_MASTER_LOG_FILE=$(grep "Relay_Master_Log_File" <<< "$STATUS_LINE" | awk '{ print $2 }') ERRORS=() MESSAGE="NO ERROR" bold=$(tput bold) normal=$(tput sgr0) echo "${bold}MYSQL_CHECK : ${normal} $MYSQL_CHECK " echo "${bold}LAST_ERRNO : ${normal} $LAST_ERRNO " printf "\n" echo "${bold}SECONDS_BEHIND_MASTER : ${normal} $SECONDS_BEHIND_MASTER" printf "\n" echo "${bold}IO_IS_RUNNING : ${normal} $IO_IS_RUNNING" printf "\n" echo "${bold}SQL_IS_RUNNING : ${normal} $SQL_IS_RUNNING" printf "\n" echo "${bold}MASTER_LOG_FILE : ${normal} $MASTER_LOG_FILE" printf "\n" echo "${bold}RELAY_MASTER_LOG_FILE : ${normal} $RELAY_MASTER_LOG_FILE" printf "\n" ### if there is an error ### if [ "${#ERRORS[@]}" -gt 0 ] then MESSAGE="An error has been detected involving the mysql replciation. Below is a list of the reported errors:\n\n $(for i in $(seq 1 ${#ERRORS[@]}) ; do echo "\t${ERRORS[$i]}\n" ; done) Please correct this ASAP " echo -e $MESSAGE else echo -e $MESSAGE fi
OUTPUT:
MYSQL_CHECK : Variable_name Value innodb_version 1.1.8 protocol_version 10 slave_type_conversions version 5.5.19-enterprise-commercial-advanced-log version_comment MySQL Enterprise Server - Advanced Edition (Commercial) version_compile_machine i686 version_compile_os linux2.6 LAST_ERRNO : 0 SECONDS_BEHIND_MASTER : 0 IO_IS_RUNNING : Yes SQL_IS_RUNNING : Yes MASTER_LOG_FILE : mysql-bin.000007 RELAY_MASTER_LOG_FILE : mysql-bin.000007 NO ERROR
you can also add this script into your cron jobs and sending the “echo -e $MESSAGE” part to your self by email.