I dag i denne artikel får vi en idé om, hvordan man overvåger MySQL-replikering. Det er dagens krav at opsætte replikering i dit miljø, men er det nok? Mit svar er 'NEJ'. Vi er nødt til at overvåge vores replikering fra tid til anden. Jeg skrev et lille script til at overvåge vores miljø, som jeg vil dele med jer for at gøre jeres liv lige så nemt, som det gjorde mit.
Ting, der skal overvåges på din slaveside:
LAST_ERRNO
SECONDS_BEHIND_MASTER
IO_IS_RUNNING
SQL_IS_RUNNING
MASTER_LOG_FILE
RELAY_MASTER_LOG_FILE
Alle disse variabler er en del af 'vis slavestatus'
Gem nedenstående script i .sh-filen og kør som nedenfor:
./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
du kan også tilføje dette script til dine cron-jobs og sende "echo -e $MESSAGE"-delen til dig selv via e-mail.