MySQL master-slave synchronous monitoring shell script under Linux

Source: Internet
Author: User

Description

Operating system: CentOS

Objective: To monitor whether the MySQL master-slave database is synchronized, if it is not synchronized, record the failure time, and execute the command to restore the synchronization status.

1. Create script file

Vi/home/crontab/check_mysql_slave.sh #编辑, add the following code

#!/bin/sh

# Check_mysql_slave Status

# author Www.111cn.net

Ip=eth0 #网卡名称

Mysql_binfile=/usr/local/mysql/bin/mysql

Mysql_user=root #MySQL数据库账号

mysql_pass=123456 #密码

Mysql_sockfile=/tmp/mysql.sock

Datetime= ' date + '%y-%m-%d/%h:%m:%s ' #获取当前时间

Mysql_slave_logfile=/home/logs/check_mysql_slave.log #日志文件路径, must be created in advance.

slave_ip= ' ifconfig $ip |grep "inet addr" | awk-f[: ""]+ ' {print $} '

status=$ ($mysql _binfile-u$mysql_user-p$mysql_pass-s $mysql _sockfile-e "show slave statusg" | grep-i "Running")

Slave_io_running= ' echo $status | grep slave_io_running | awk ' {print $} '

Slave_sql_running= ' echo $status | grep slave_sql_running | awk ' {print $} '

If ["$Slave _io_running" = "yes"-a "$Slave _sql_running" = "yes"]

Then echo "Slave is running!"

Else

echo "$datetime $slave _ip slave is not running!" >> $mysql _slave_logfile

$mysql _binfile-u$mysql_user-p$mysql_pass-s $mysql _sockfile-e "SLAVE STOP;"

$mysql _binfile-u$mysql_user-p$mysql_pass-s $mysql _sockfile-e "SET GLOBAL sql_slave_skip_counter=1;"

$mysql _binfile-u$mysql_user-p$mysql_pass-s $mysql _sockfile-e "SLAVE START;"

$mysql _binfile-u$mysql_user-p$mysql_pass-s $mysql _sockfile-e "EXIT"

Fi

: wq! #保存退出

chmod +x/home/crontab/check_mysql_slave.sh #添加脚本执行权限

2, add task plan, modify/etc/crontab

Vi/etc/crontab #在最后一行添加

*/10 * * * * root/home/crontab/check_mysql_slave.sh #表示每10分钟执行一次

: wq! #保存退出

3, restart Crond to make the settings effective

/etc/rc.d/init.d/crond Restart #yum install-y Vixie-cron installation Scheduled Tasks, some systems may not have pre-installed

Chkconfig Crond on #设为开机启动

Service Crond Start #启动

Can view MySQL master-slave sync status based on log file/home/logs/check_mysql_slave.log

Original from: osyunwei.com

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: info-contact@alibabacloud.com 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.