Scripting MySQL master-slave replication status monitoring

Source: Internet
Author: User

actual production case: monitoringMysqlMaster-Slave synchronization is abnormal, if the exception, send SMS or mail to the administrator. Tip: If there is no master-slave synchronization environment,The following text can be placed in the file to be read to simulate:
Stage1: Develop a daemon script for each -The second implementation is detected once.
Stage2: If the synchronization appears with the following error number (1158,1159,1008,1007,1062), the error is skipped.
Stage3: Use array technology to implement the above script (get master-slave judgment and Error number section)



# NO1 Define Variables

array= (1158 1159 1008 1007 1062)

date=$ (Date +%f)

Mysql_cmd= "Mysql-uroot-poldboy123-s/data/3307/mysql.sock-e"

ip=$ (Ifconfig eth2|awk-f "[:]+" ' Nr==2{print $4} ')


# No2 Daemon for monitor MySQL replication

While True


# SKIP Error num 1158 1159 1008 1007 1062

error_num=$ (${mysql_cmd} "show slave Status\g" |grep "Last_sql_errno" |awk ' {print $NF} ')

For n in${array[@]}


If [$Error _num-eq $n];then

${mysql_cmd} "Stop Slave;set global sql_slave_skip_counter=1;startslave;"



# No3 Check MySQL replication Status

Mysql_status= ($ ($Mysql _cmd "show slave Status\g" |egrep "_running|_behind_master" |awk ' {print $NF} '))


For n in${mysql_status[@]}


If ["$n"! = "Yes"-a "$n"! = "0"];then




# No4 Mail to System Manager

If [$Flag-ne 0];then

echo "[$Date] there is $Flag problems on MySQL slave. IP: $IPPort: $Port ">>/tmp/slave.log

Tail-1/tmp/slave.log|mail-s "Slave Warning" [email protected]

Sleep 300


echo "Mysqlslave is OK"


# No5 complete!

Sleep 30


This article is from the "architects of the Day" blog, be sure to keep this source

Scripting MySQL master-slave replication status monitoring

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.