監控mysql主從同步
阿新 • • 發佈:2018-11-24
quest mat 線程 condition passwd eth0 時間 bre slaves
1,昨天看到shell一道面試題,需求如下:
監控MySQL主從同步是否異常,如果異常,則發送短信或者郵件給管理員。提示:如果沒主從同步環境,可以用下面文本放到文件裏讀取來模擬:
階段1:開發一個守護進程腳本每30秒實現檢測一次。
階段2:如果同步出現如下錯誤號(1158,1159,1008,1007,1062),則跳過錯誤。
階段3:請使用數組技術實現上述腳本(獲取主從判斷及錯誤號部分)
[root@oldboy~]# mysql -uroot -p‘oldboy‘ -S /data/3307/mysql.sock -e "show slavestatus\G;" *************************** 1. row *************************** Slave_IO_State:Waiting for master to send event Master_Host:10.0.0.179 #當前的mysql master服務器主機 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File:mysql-bin.000013 Read_Master_Log_Pos: 502547 Relay_Log_File:relay-bin.000013 Relay_Log_Pos:251 Relay_Master_Log_File:mysql-bin.000013 Slave_IO_Running:Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: mysql Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 502547 Relay_Log_Space:502986 Until_Condition:None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 #和主庫比同步延遲的秒數,這個參數很重要 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error:
2,好久沒寫過腳本,所以就寫了一個,如果各位看觀發現有問題,還請指教,謝謝!
#!/bin/bash #author:linsir #qq:384108391 #檢查mysql同步情況 #定義初始變量 mysql_sync_status=/tmp/mysql_sync_status.txt error_log=/tmp/mysql_sync_error.txt user=root passwd=linlin CMD="/usr/local/mysql2/bin/mysql -S /tmp/3307.sock -u$user -p$passwd -e" mail_address=‘[email protected]‘ time=`date +%F-%T` ip=`ifconfig eth0|awk -F ‘[ :]+‘ ‘{print $4}‘|awk "{if(NR==2) print}"` errorcode=(1158 1159 1008 1007 1062) #mysql主從同步會出現錯誤代碼1158 1159 1008 1007 1062,這些錯誤代碼不會對主從同步影響非常大。所以如果遇到這個錯誤代碼,就到下個需要執行的sql即可 #檢查mysql status中的Last_Errno的值,正常一般為0. Continue_errorcode() { $CMD "show slave status\G" > $mysql_sync_status 2>/dev/null code=`awk ‘/Last_Errno/ {print $2}‘ $mysql_sync_status` if [ "$code" != "0" ];then for ccode in ${errorcode[*]} do if [ "$code" = "$ccode" ];then $CMD "stop slave;set global sql_slave_skip_counter = 1;start slave;" 2>/dev/null $CMD "show slave status\G" > $mysql_sync_status 2>/dev/null flag=1 && break fi done else flag=1 fi } Check_ok() { IO=`awk ‘/Slave_IO_Running/ {print $2}‘ $mysql_sync_status` SQL=`awk ‘/Slave_SQL_Running:/ {print $2}‘ $mysql_sync_status` # echo $IO # echo $SQL if [ "$IO" = "No" -o "$SQL" = "No" ];then Error_format "$time" "$ip" "mysql同步失敗,IO或SQL線程down" fi } Delay_time() { t=`awk ‘/Seconds_Behind_Master/ {print $2}‘ $mysql_sync_status` if [ "$t" = "NULL" ] ;then Error_format "$time" "$ip" "mysql同步出錯,Seconds_Behind_Master is NUll" else if [ $t -gt 0 ];then Error_format "$time" "$ip" "mysql同步超時,超時時間為$t" fi fi } Error_format() { echo " Mysql master-slave synchronization state ---------------------------------------------------------- time: $1 host: $2 question: $3 ---------------------------------------------------------- " >> $error_log } main() { while true do cat /dev/null >$error_log Continue_errorcode if [ "$flag" != "1" ];then Error_format "$time" "$ip" "mysql同步出錯,錯誤代碼:$code" fi Check_ok Delay_time [ `cat $error_log|wc -l` -ne 0 ] && mail -s ‘mysql同步異常,請及時處理‘ -a $mysql_sync_status $mail_address < $error_log sleep 30 done } main
監控mysql主從同步