mysql 主從錯誤以及監控

tangr206發表於2013-08-12
同步中的常見的錯誤和處理
1、現象:在從庫上面show slave status\G;出現下列情況,
          Slave_IO_Running: Yes
          Slave_SQL_Running: No
          Seconds_Behind_Master: NULL
原因:
a.程式可能在slave上進行了寫操作;
b.也可能是slave機器重起後,事務回滾造成的;
c.有可能是在同步過程中遇到某種錯誤,這個會在檢視從庫中狀態時看到錯誤提示,最少見的就是主鍵重複1062的錯誤。
解決方法:
進入master
mysql> show master status;
+----------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+----------------------+----------+--------------+------------------+
| mysql-bin.000040 | 324 |adb | mysql|
+----------------------+----------+--------------+------------------+
然後到slave伺服器上執行手動同步
slave stop;
change master to
master_host='10.14.0.140',
master_user='repl',
master_password='1q2w3e4r',
master_port=3306,
master_log_file='mysql-bin.000040',
master_log_pos=324;
slave start;
show slave status\G;
2、現象:從資料庫無法同步,show slave status顯示:
          Slave_IO_Running: No
          Slave_SQL_Running: Yes
          Seconds_Behind_Master: NULL
   解決:首先檢視資料庫的err日誌,檢視是什麼錯誤提示,看從庫連線主庫的IP、使用者、密碼等相關資訊是否有誤,如果有誤,重新執行同步;如果確認無誤,重啟主資料庫。
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 98 | adb| mysql|
+------------------+----------+--------------+------------------+
進入從庫mysql,執行:
slave stop;
change master to Master_Log_File='mysql-bin.000001',Master_Log_Pos=98;
slave start;
或是這樣:
stop slave;
set global sql_slave_skip_counter =1;
start slave;
這個現象主要是master資料庫存在問題,由於連線主庫資訊錯誤、主庫資料庫掛掉如果說常見錯等原因引起的,我在實際的操作中先重啟master後重啟slave即可解決這問題,出現此問題,必須要要重啟master資料庫。
四、mysql主主和主主叢集
1、mysql主主的實現
    在實際的生產應用中,為了在主庫出現崩潰或是主伺服器出現嚴重故障時快速的恢復業務,會直接切換到從庫上,當主庫故障處理完成後讓他直接作為叢庫來執行,此時主主就是一個不錯的選擇。
  
五、mysql主從的監控
在mysql主從的應用中,只要進行了合理設定,基本上不會出現問題,但是對他的監控是必不可少的,以免由於真的出現問題又不知道而造成不必要的資料損失。
1mysql主從監控的主要思路
Mysql主從的監控,其主要是監控從庫上的一些重要引數:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Master_Log_File: bin-log.003
Relay_Master_Log_File: bin-log.003
Read_Master_Log_Pos: 4
Exec_master_log_pos: 4
Seconds_Behind_Master: 0(5.0之前版本沒有這個選項)
通過以上的引數可以反映出主庫和從庫狀態是否正常,從庫是否落後於主庫等。值得一提的是在mysql5.0以前的版本,Slave_IO_Running這個狀態指標不可靠,會在主庫直接掛掉的情況下不會變成NO,Seconds_Behind_Master引數也不存在。監控以上引數即可監控mysql主從。
2mysql主從監控的實現
不管mysql是那個版本,其中的從庫上的Exec_master_log_pos、Exec_master_log_pos;主庫上的 Master上的Log_File, Position,這四個引數可以判斷出當前主從的狀態。以下是適用於mysql所有版本的主從監控shell指令碼:
#/bin/sh
user=repl
passwd=123415
master_ip="192.168.1.2"
log="/data3/check_repl.log"
value()
{
 master=`/usr/local/mysql/bin/mysql -u$user -p$passwd -h$master_ip -e "show master status\G;"|egrep "File|Position"`
 #mysql 4.0
 slave=`/usr/local/mysql/bin/mysql -u$user -p$passwd -h127.0.0.1 -e "show slave status\G;"|egrep "Relay_Master_Log_File|Exec_master_log_pos"`
 #mysql 5.0
 #slave=`mysql -u$user -p$passwd -e "show slave status\G;"|egrep "Relay_Master_Log_File|Exec_Master_Log_Pos"`
 #取主庫上的bin-log號及寫入的當前日誌位置   
 Master_Log=`echo $master |awk '{print $2}'|awk -F "." '{print $2}'`
 Master_Log_Pos=`echo $master |awk '{print $4}'`
 #取從庫上當前同步主庫的位置
 Relay_Master_Log_File=`echo $slave |awk '{print $2}'|awk -F "." '{print $2}'`
 Exec_Master_Log_Pos=`echo $slave |awk '{print $4}'`
 echo "Master_Log:"$Master_Log>>$log
 echo "Master_Log_Pos:"$Master_Log_Pos>>$log
 echo "Relay_Master_Log_File:"$Relay_Master_Log_File>>$log
 echo "Exec_Master_Log_Pos:"$Exec_Master_Log_Pos>>$log
}
for((i=1;i<=10;i++));
do
 echo "#################################">>$log
 value
 time=`date +"%Y-%m-%d %H:%M:%S"`
 if [ $Master_Log -eq $Relay_Master_Log_File ];then
       A=`expr $Master_Log_Pos - $Exec_Master_Log_Pos`
       if [ $A -lt 0 ];then
             A=`expr 0 - $A`
       fi
       echo $A>>$log
       if [ $A -lt 10000 ];then
             echo "$time Master-Slave is OK.">>$log
             #echo "$i"
             break
       else
             if [ $i ge 3 ];then              
                  echo "$time Warning:Slave-Master lag $A " >>$log
                  echo "$i"
             fi
             sleep 30
             continue
       fi
 else
       sleep 60
       fi
       if [ $i -eq 10 ];then
             echo "$i"
             echo "$time Error:Slave-Master must be check !" >>$log
       fi
done
在mysql5.0以後的版本,mysql主從已經相當的成熟了,可以只監控Slave_IO_Running,Slave_SQL_Running,Seconds_Behind_Master狀態就可以了,這裡不再做說明。

相關文章