內容爲本身的一點總結,若有不對歡迎狠勁兒拍磚
mysql
本文來自http://yijiu.blog.51cto.com/轉載請經博主贊成ios
監控主從複製正常與否sql
相比各位都應該知道,監控主從是否工做正常,涉及命令以下:
shell
show slave status\G;
那麼,咱們須要關注的參數以下:
bash
1. 首先查看SQL和IO線程是否爲YES狀態(想必各位都明白了)ide
2. 是否有延遲 是否大於0 #通常生成環境延遲是否大於500秒,若是大於500則報警,如大於1000則嚴重報警函數
#好比傳遞一個sql到slave上,binlog中在eventhear中存在time stamp時間戳,在下條binlog拿到的時間會進行比較,若是當前時間是多少則顯示多少,若是更新很是頻繁500秒會產生更多的sql積累在其中spa
至少生產中監控就是這麼實現的以及包括nagios的監控插件也是這麼實現的插件
主要關注的值本文來自http://yijiu.blog.51cto.com/轉載請經博主贊成,線程
1. Master_Log_File Read_master_log_Pos 2. Relay_Master_Log_File Exec_Master_log_pos 3. Seconds_Behind_master
判斷一個庫主要觀察以上幾點,若是主庫掛了,Seconds_Behind_master 會已經成爲NULL了
那麼這樣如何去觀測從庫是否日誌同步完成,以下所示
經過shell實現監控同步的方法
廢話很少說了直接上菜
1.利用status去觀測是否已經同步完成
判斷公式
如下爲判斷依據,判斷如下值
Master_Log_File 和 Relay_Master_Log_File 的值必須相等
判斷同步的偏移量
Read_master_log_Pos 和 Exec_Master_log_pos 的值必須相等
根據以上爲最基礎的判斷依據,是否可將其從庫提高爲主庫,就會在從庫中判斷master log file 是否讀到的位置同樣並找到一個最靠前的一個節點提高爲主
shell內容以下所示:本文來自http://yijiu.blog.51cto.com/轉載請經博主贊成
#!/bin/bash user='root' password='mypass' thread_status=`/usr/local/mysql/bin/mysql -u"$user" -p"$password" -S /tmp/mysql_3308.sock -e 'show slave status\G'|grep -i yes|wc -l` status=(`/usr/local/mysql/bin/mysql -u"$user" -p"$password" -S /tmp/mysql_3308.sock -e 'show slave status\G'| egrep -i "Master_Log_File|Relay_Master_Log_File|Read_master_log_Pos|Exec_Master_log_pos|Seconds_Behind_Master" |awk -F':' '{print $2}'`) echo ${status[4]} if [[ "$thread_status" != 2 ]]; then echo "the Replication is Fault , at $(date)" > $catalog echo `uname -n` | mail umail@qq.com < $catalog exit 1 fi if [[ "${status[4]}" > '300' ]];then echo "yan chi guo gao, at $(date)" > $catalog echo `uname -n` | mail umail@qq.com < $catalog exit 2 fi if [[ ${status[0]} == ${status[2]} ]] && [[ ${status[1]} == ${status[3]} ]]; then echo 'The Replication is Normal' exit 0 else echo "the Replication is Fault , at $(date)" > $catalog echo `uname -n` | mail umail@qq.com < $catalog exit 2 fi
本文來自http://yijiu.blog.51cto.com/轉載請經博主贊成
2.依賴於程序檢測 本文來自http://yijiu.blog.51cto.com/轉載請經博主贊成
好比程序在master創建表,並隨意設置字段,並在master上獲取一個時間並寫入
now的時間在程序中自行獲得並記錄,最後在slave中執行select 查看結果是否與時間對應一致
若是時間同樣則認爲正常,若是master上的時間減去slave上的時間 出現了延遲,那麼證實延遲存在的,可是這種方法存在缺陷,好比主庫掛了那麼則不可用
#!/bin/bash user='root' password='mypass' /usr/local/mysql/bin/mysql -u"$user" -p"$password" -e 'replace into master.repl_heart set t=now(),id=1;' >/dev/null 2>&1 master_select=`/usr/local/mysql/bin/mysql -u"$user" -p"$password" -S /tmp/mysql.sock -e 'select t from master.repl_heart where id=1;' | awk '{print $2}' | tail -1` slave_select=`/usr/local/mysql/bin/mysql -u"$user" -p"$password" -S /tmp/mysql_3308.sock -e 'select t from master.repl_heart where id=1;' | awk '{print $2}' | tail -1` master_date=`date -d "$master_select" +%s` slave_date=`date -d "$slave_select" +%s` delay=`echo "$master_date"-"$slave_date" | bc` if [[ $master_date == $slave_date ]];then echo 'is ok' elif [[ $delay -le 500 ]];then echo cun zai yan chi "$delay" else echo "the Replication delay too large "$delay" , at $(date)" > $catalog echo `uname -n` | mail umail@qq.com < $catalog fi
須要注意的是:複製中,若是是行格式,就是主庫的時間;若是不是行式,這個方法能夠把now()這個內置函數在s腳本中生成再寫入
以上,爲監控mysql主從的兩種shell的寫法,若有不足,麻煩指出,感謝各位