MySQL复制状态检查脚本

2022-04-13 18:12:25 浏览数 (1)

sql_slave_skip_1.sh

代码语言:javascript复制
#!/bin/bash

source ~/.bashrc

Slave_IO_Running_S=$(mysql -uroot -p123456 -S /data1/3306/mysqldata/mysql.sock -e "show slave statusG" | grep -Po '(?<=Slave_IO_Running: )(?:.*)')
Slave_SQL_Running_S=$(mysql -uroot -p123456 -S /data1/3306/mysqldata/mysql.sock -e "show slave statusG" | grep -Po '(?<=Slave_SQL_Running: )(?:.*)')
[ -z "$Slave_IO_Running_S" -o -z "$Slave_SQL_Running_S" ] && continue
[[ ${Slave_IO_Running_S} == "Yes" ]] && S1=1 || S1=0
[[ ${Slave_SQL_Running_S} == "Yes" ]] && S2=1 || S2=0
if [[ ${S1} == 0 ]] || [[ ${S2} == 0 ]];then
    Last_SQL_Error_S="$(mysql -uroot -p123456 -S /data1/3306/mysqldata/mysql.sock -e "show slave statusG" | grep Last_SQL_Error)"
    mysql -uroot -p123456 -S /data1/3306/mysqldata/mysql.sock -e "stop slave;set global sql_slave_skip_counter=1;start slave;" 
    echo `date " %Y-%m-%d %H:%M:%S"` >> /home/mysql/sql_slave_skip_1.log
    echo "$Last_SQL_Error_S" >> /home/mysql/sql_slave_skip_1.log
fi

test_slave_status.sh

代码语言:javascript复制
#!/bin/bash

step=2 #间隔的秒数,不能大于60

for (( i = 0; i < 60; i=(i step) )); do
    /home/mysql/sql_slave_skip_1.sh
    sleep $step
done

exit 0

crontab调用:

代码语言:javascript复制
* * * * * /home/mysql/test_slave_status.sh

0 人点赞