Mysql主從狀態的簡單監視

Mysql主從搭建成功後,需要監視從庫的狀態

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: XXXXXXX
                  Master_User: reptor
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000093
          Read_Master_Log_Pos: 205773579
               Relay_Log_File: Report-relay-bin.000075
                Relay_Log_Pos: 205773742
        Relay_Master_Log_File: mysql-bin.000093
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

比較簡單的方法是,監視Slave_IO_Running 和 Slave_SQL_Running是否爲yes。

網上找了一個腳本

#!/bin/sh
ip=`ifconfig eth1|sed -n 2p|awk  '{ print $2 }'|awk -F : '{ print $2 }'`
port='3306'
array=($(mysql -uroot -pXXXX  -e "show slave status\G"|grep "Running" |awk '{print $2}'))
if [ "$port" == "3306" ]
   then
     if [ "${array[0]}" == "Yes" ] || [ "${array[1]}" == "Yes" ]
          then
            echo "slave is OK"
       else
            echo "slave: ${ip} replication error" | mail -s 'slave replication is error' [email protected] [email protected]
    fi
else
   echo "slave: ${ip} mysql is stop" | mail -s 'slave mysql is stop' [email protected] [email protected]

然後設置定時任務,比如每個小時執行一次

* */1 * * * sh /mnt/erp/batch/checkSlave.sh

設定爲crontab任務後,執行shell時,用戶的環境變量會丟失。

所以在#!/bin/sh下面添加

. /etc/profile
. ~/.bash_profile

以啓用用戶的環境變量

執行之後可能會發不出mail,還需要進一步安裝配置mail服務。

我用的是centOS,直接安裝sendmail

yum install sendmail

安裝好sendmail以後執行以下命令啓動sendmail

service sendmail start

安裝成功後測試一下就OK了

echo "This is test mail" | mail -s 'Test mail' [email protected]

這樣一個簡單的主從狀態監視就搭建完畢了。

但即使不報錯,比不能保證主從同步的正確性。

因爲,即使主從的數據不一致,只要不發生類似於主鍵衝突的error,狀態依然顯示爲兩個yes。

要想完全確認主從一致性,需要另一個工具

pt-table-checksum

後面有空了我會部署它。




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