監控MySQL主從同步

來源:互聯網
上載者:User

標籤:mysql 主從同步監控

指令碼監控資料庫主從同步

來源:http://oldboy.blog.51cto.com/2561410/1632876

來源:

(生產實戰案例):監控MySQL主從同步是否異常,如果異常,則傳送簡訊或者郵件給管理員。提示:如果沒主從同步環境,可以用下面文本放到檔案裡讀取來類比:

階段1:開發一個守護進程指令碼每30秒實現檢測一次。

階段2:如果同步出現如下錯誤號碼(1158,1159,1008,1007,1062),則跳過錯誤。

階段3:請使用數組技術實現上述指令碼(擷取主從判斷及錯誤號碼部分


[[email protected]~]# 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:

  

################################################################

################################################################

################################################################

################################################################

  

第一關解決   

[[email protected] script]# cat mysqlzcjk.sh

#!/bin/sh

. /etc/init.d/functions

while true

do

   num=0

   array=($(egrep "_Running|Behind_Master" slave.log |awk ‘{print $NF}‘ ))

   for status in ${array[*]}

    do

      if [ "$status" = "Yes"  -o  "$status" = "0"  ]

      then

        let num=num+1

     fi 

    done

      if [ $num -eq 3 ] 

         then

            action "MySQL slave is OK" /bin/true |tee success.log

             cat success.log |mail -s "$char" [email protected]

         else

            action "MySQL slave is not OK" /bin/false | tee error.log

             cat error.log |mail -s "$char" [email protected]

            

      fi

   sleep 30

done   

##在產生情況,用下面命令取主從同步狀態

##mysql -uroot -plvnian -e "show slave status\G;"|egrep "_Running|Behind_Master" |awk ‘{print $NF}‘

=================

[[email protected] script]# sh mysqlzcjk.sh 

MySQL slave is not OK                                      [FAILED]

MySQL slave is not OK                                      [FAILED]

MySQL slave is not OK                                      [FAILED]

MySQL slave is not OK                                      [FAILED]

MySQL slave is not OK                                      [FAILED]

MySQL slave is not OK                                      [FAILED]

MySQL slave is not OK                                      [FAILED]

MySQL slave is OK                                          [  OK  ]

MySQL slave is OK                                          [  OK  ]



#######################################################################

#######################################################################

第二第三關

#!/bin/sh

. /etc/init.d/functions

while true

do

   num=0

   errorno=(1158 1159 1007 1008 1062)

   array=($(egrep "_Running|Behind_Master|SQL_Errno" slave.log |awk ‘{print $NF}‘ ))

   for status in ${array[*]}

    do

      if [ "$status" = "Yes"  -o  "$status" = "0"  ]

      then

        let num=num+1

     fi 

    done

      if [ $num -eq 3 ] 

         then

            action "MySQL slave is OK" /bin/true |tee success.log

             cat success.log |mail -s "$char" [email protected]

         else

            action "MySQL slave is not OK" /bin/false | tee error.log

             cat error.log |mail -s "$char" [email protected]

           for ((i=0;i<${#errorno[*]};i++))

                if [ $array[3] -eq ${errorno[$i]} ]

                   then

                      mysql -uroot -plvnian -e "stop slave && set global sql_slave_skip_counter=1;start slave;"

                fi

            

      fi

   sleep 10

done

##在產生情況,用下面命令取主從同步狀態

##mysql -uroot -plvnian -e "show slave status\G;"|egrep "_Running|Behind_Master|SQL_Errno" |awk ‘{print $NF}‘

#######################################################################

#######################################################################

測試命令:

stop slave sql_thread;

start slave sql_thread;

stop slave io_thread ;

start slave io_thread ;

stop slave;

start slave;


本文出自 “奮鬥吧” 部落格,請務必保留此出處http://lvnian.blog.51cto.com/7155281/1701594

監控MySQL主從同步

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.