Share a script that automatically jumps mysql from the database with 1062 errors _ MySQL

Source: Internet
Author: User
Share a script that automatically jumps to mysql slave database with the 1062 error bitsCN.com


Share a script that automatically jumps to the mysql slave database with error 1062

A sudden power loss from the database may cause the information in the log to be flushed to the hard disk. Therefore, after the slave database is started, the master and slave nodes will get stuck due to 1062 (Duplicate primary keys). here, a script automatically jumps 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.