mysql 主从错误以及监控

同步中的常见的错误和处理
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状态就可以了,这里不再做说明。
posted @ 2013-08-12 09:46  tangr206  阅读(712)  评论(0编辑  收藏  举报