Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use shell script to monitor mysql master-slave synchronization status

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to use shell script to monitor mysql master-slave synchronization status, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

#! / bin/bash#monitor_mysql_slave statusUSERNAME=xx# if the password contains special characters, you can put in quotation marks Avoid escaping PASSWORD='xx' SLAVE_HOST=ipSLAVE_PORT=port MYSQL= "mysql-u$USERNAME-p$PASSWORD" SLAVE= "$MYSQL-h $SLAVE_HOST-P $SLAVE_PORT" EMAIL_ADDR=zhangsan@qq.com datetime= `date + "% Y-%m-%d% H:%M:%S" `# View MySQL_Status=` $SLAVE-e "SHOW SLAVE STATUS\ G" | grep-E "Running | Seconds_Behind_Master" | head-n3 `# from the server's two process information 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} '`if ["$Slave_IO_Running" = "Yes"-a "$Slave_SQL_Running" = "Yes"-a "$Seconds_Behind_Master"-lt "10"] then echo "Replication OK" else SUBJECT= "ERRORS in replication" BODY= "$datetime $SLAVE_HOST MySQL slave replication error" echo $SUBJECT echo $BODY # send alarm email echo $BODY | mail-s "$SUBJECT"-t $EMAIL_ADDRfi

After you have written it, save it. Grant the executable permission chmod + x file name, and then execute it. If there are errors, you can debug using the sh-x filename.

You can also add the script to a scheduled task and execute it regularly.

Using the crontab-e command, add the following command to the scheduled task

Scheduled tasks are performed every 15 minutes

* / 15 * / usr/local/xx.sh

Restart crontab

The above is how to use shell scripts to monitor mysql master-slave synchronization status. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report