Get the App
SLTechnology News&Howtos  ›  Database  › 

Mysql master-slave status monitoring script

Shulou Source: shulou.com Published: 2022-06-01 07:19:04 11月17日 Update

Target: monitor the status of the Mysql slave library, and give an alarm if an exception is copied from the library

Train of thought:

1. First look up the four parameter values of the Mysql slave library (show slave status according to this command); they are the IO thread, SQL thread, replication delay time of the library (this is not used in the following script, you can write another script to alarm the delay), error code

2. Function Mysql_status judgment, logic and judgment of IO thread, SQL thread and error code.

3. Execute the function Mysql_Errno if it is false

4. If the mysql error code is 1158 1159 1008 1007 1062, we can stop slave, skip the current number of copies, and proceed to the next copy.

[root@zabbix script] # cat mysql_check.sh

#! / bin/bash

#

# 2017.5.8

# mysql status Monitoring alarm

. / etc/init.d/functions

Mysql_IO_cmd= `mysql-h 10.222.1.5-uroot-p123456-e 'show slave status\ G' | grep Slave_IO_Running | awk' {print $NF}'`

Mysql_SQL_cmd= `mysql-h 10.222.1.5-uroot-p123456-e 'show slave status\ G' | grep Slave_SQL_Running | awk' {print $NF}'`

Mysql_Be_cmd= `mysql-h 10.222.1.5-uroot-p123456-e 'show slave status\ G' | grep Seconds_Behind_Master | awk' {print $NF}'`

Mysql_Errno_cmd= `mysql-h 10.222.1.5-uroot-p123456-e 'show slave status\ G' | grep Last_SQL_Errno | awk' {print $NF}'`

ErrorNo= (1158 1159 1008 1007 1062)

# # judging error codes copied by mysql

Mysql_Errno () {

For sum in $ErrorNo

Do

If ["$Mysql_Errno_cmd" = = "$sum"]; then

Echo "$(date +% Fitch% R) error status code: $Mysql_Errno_cmd" > > / tmp/mysql_error.log

Mysql-h 10.222.1.5-uroot-p123456-e'stop slave;set global sql_slave_skip_counter=1;start slave;'

Echo `sort-r / tmp/mysql_ error.log` | mail-s "MySQL Slave is error" zabbix_client@jt56.org

Else

Echo "$(date +% Fitch% R) error status code: $Mysql_Errno_cmd" > > / tmp/mysql_error.log

# echo `sort-r / tmp/mysql_ error.log` | mail-s "MySQL Slave is error" zabbix_client@jt56.org

Uniq / tmp/mysql_error.log | mail-s "MySQL Slave is error" zabbix_client@jt56.org

Fi

Done

}

# # Monitoring mysql status function

Mysql_status () {

If ["$Mysql_IO_cmd" = = "Yes"-a "$Mysql_SQL_cmd" = = "Yes"-a "$Mysql_Errno_cmd" = = "0"]; then

Echo

Action "mysql status" / bin/true

Else

Echo

Action "mysql status" / bin/false

Mysql_Errno

Fi

}

Mysql_status

Tags: Errors states threads functions scripts delays monitoring monitoring parameters commands ideas time state monitoring goals logic master and slave Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno macOS MySQL Microsoft