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数据库。
4、mysql主主和主主集群
一、mysql主主的实现
     在实际的生产应用中,为了在主库出现崩溃或是主服务器出现严重故障时快速的恢复业务,会直接切换到从库上,当主库故障处理完成后让他直接做为丛库来运行,此时主主就是一个不错的选择。
  
5、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状态就能够了,这里再也不作说明。
相关文章
相关标签/搜索