Home > Database > Mysql Tutorial > share一个自动跳mysql从库上1062错误的脚本_MySQL

share一个自动跳mysql从库上1062错误的脚本_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-01 13:37:41
Original
1349 people have browsed it

bitsCN.com


share一个自动跳mysql从库上1062错误的脚本

 

 从库突然掉电可能会导致log里的信息没flush到硬盘,于是从库启动之后主从会因为1062(主键重复)而卡住,这里提供一个自动跳1062的脚本

[plain] 

#!/bin/sh  

  

MYSQL=mysql  

  

lastPos=0  

while [ 1 ]; do  

        $MYSQL -uroot -e "show slave status/G" > /tmp/.skip  

        lastError=`cat /tmp/.skip|grep "Last_SQL_Errno"|awk '{print $2}'`  

        nowPos=`cat /tmp/.skip|grep "Exec_Master_Log_Pos"|awk '{print $2}'`  

        if [ $lastError -eq 1062 ]; then  

                if [ $lastPos -ne $nowPos ]; then  

                        echo "blocked, skip one"  

                        $MYSQL -uroot -e "slave stop; set global sql_slave_skip_counter =1; slave start;"  

                        lastPos=$nowPos  

                else  

                        echo "sleep one second"  

                        sleep 1  

                fi  

        elif [ $lastError -eq 0 ]; then  

                secondsBehind=`cat /tmp/.skip|grep "Seconds_Behind_Master"|awk '{print $2}'`  

                if [ $secondsBehind -eq 0 ]; then  

                        echo "done"  

                        break  

                else  

                        echo "$secondsBehind seconds behind server"  

                        sleep 3  

                fi  

        else  

                echo "error $lastError found"  

                break  

        fi  

done  

 

 

bitsCN.com
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
MySQL stops process
From 1970-01-01 08:00:00
0
0
0
Error when installing mysql on linux
From 1970-01-01 08:00:00
0
0
0
phpstudy cannot start mysql?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template