監控MySQL主從同步是否異常並報警企業案例模擬

   

企業面試題1:(生產實戰案例):監控MySQL主從同步是否異常,如果異常,則發送短信或者郵件給管理員。提示:如果沒主從同步環境,可以用下面文本放到文件裏讀取來模擬:
階段1:開發一個守護進程腳本每30秒實現檢測一次。
階段2:如果同步出現如下錯誤號(1158,1159,1008,1007,1062),則跳過錯誤。
階段3:請使用數組技術實現上述腳本(獲取主從判斷及錯誤號部分)

此題來自:http://oldboy.blog.51cto.com/2561410/1632876


解答參考1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/sh
#oldboy linux training
#2015-05-17
#說明:本腳本來自老男孩linux21期學員張耀開發!
# Source function library.
. /etc/init.d/functions
 
# Defined variables
MysqlUser=root
MysqlPass=oldboy123
MysqlPort=3307
Mysqlsock=/data/$MysqlPort/mysql.sock
ErrorNo=(1158 1159 1008 1007 1062)
errorlog=/tmp/error_skip.log
MysqlCmd="/application/mysql/bin/mysql -u$MysqlUser -p$MysqlPass -S $Mysqlsock"
 
# Judge mysql server is ok?
[ -S $Mysqlsock ] ||{
    echo "Maybe MySQL have someting wrong"
    exit 1
}
 
# Defined skip error Functions
function error_skip(){
    local flag
    flag=0
    for num in ${ErrorNo[@]}
      do
        if [ "$1" == "$num" ];then
           $MysqlCmd -e'stop slave;set global sql_slave_skip_counter=1;start slave;'
           echo "$(date +%F_%R) $1" >>$errorlog
        else
           echo "$(date +%F_%R) $1" >>$errorlog
           ((flag++))
        fi
    done
    [ "$flag" == "${#ErrorNo[@]}" ] &&{
        action "MySQL Slave" /bin/false
        uniq $errorlog|mail -s "MySQL Slave is error" [email protected]
    }
}
 
# Defined check slave Functions
function check_slave(){
    MyResult=`$MysqlCmd -e'show slave status\G'|egrep '_Running|Behind_Master|SQL_Errno' |awk '{print $NF}'`
    array=($MyResult)
  if [ "${array[0]}" == "Yes" -a "${array[1]}" == "Yes" -a "${array[2]}" == "0" ]
    then
    action "MySQL Slave" /bin/true
  else
    error_skip ${array[3]}
  fi
}
 
# Defined main Functions
function main(){
    while true
       do
         check_slave
         sleep 60
    done
}
main

解答參考2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/sh
#oldboy linux
#2015-05-17
#說明:本腳本來自老男孩教育21期學員李新宇!
USER=root
PASSWORD=123456
PORT=3307
error=(1158 1159 1008 1007 1062)
MYSQLCMD="mysql -u$USER -p$PASSWORD -S /data/$PORT/mysql.sock"
 
is_run(){
  [ `lsof -i:$PORT|wc -l` -lt 2 ]&&{
    echo "mysql server is stopped"
    exit 1
  }
}
 
status_array(){
 status=($($MYSQLCMD -e "show slave status\G"|egrep "_Running|Last_Errno|Behind_Master"|awk '{print $NF}'))
}
 
status_error(){
for((i=0;i<${#error[*]};i++))
do
  if [ "$1" == "${error[$i]}" ]
    then
      $MYSQLCMD -e "stop slave;set global sql_slave_skip_counter=1;start slave;"
  else 
      echo "MySQL slave is failed, errorno is $1"
  fi
done
}
 
judge_slave(){
  status_array
  if [ "${status[0]}" == "Yes" -a "${status[1]}" == "Yes" -a "${status[3]}" = "0" ]
  then
    echo "MySQL slave is ok"
  else
    status_error ${status[2]}
  fi
}
 
main(){
while true
do
  is_run
  judge_slave
  sleep 60
done
}
main


老男孩老師提示:企業實際場景,還可能根據主庫寫時間戳的方式更嚴格的判斷數據庫是否同步。

本文出自 “老男孩linux培訓” 博客,請務必保留此出處http://oldboy.blog.51cto.com/2561410/1652086

   


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章