2 sets of MySQL master-slave cross-cutting scripts

Source: Internet
Author: User

Due to some problems in MySQL master, such as Phantom Reading, and so on master-slave synchronization occurs when the main fault, need to master and slave cross-cutting, just learning python with Python wrote a master-slave cross-cutting script, beginning also useful shell writing.

The main steps are:

From top: Stop slave

Flush logs

Re-use show Master status\g to take binary log and position as variable

Your Excellency: Stop slave

Then use change master to cut from (using the above variables)

Start slave

Then show slave status\g judge


The following is a master-slave script written by the shell (which can be further researched when operating from MySQL)

#!/bin/bash#coding=utf-8#author=min#date=2015.10.16#use:change slave to master when  master was badmaster_ip=192.168.1.136                       # source master ipslave _ip=192.168.1.134                        # source slave iproot_passwd=******                             # root ' password for logining myself  mysqluser=paixian                                  #  user to login&Nbsp;master ' s mysqlpasswd=********                          # password for  user to login master ' s mysqlmaster_user=myslave                            # user to change master for master ' s mysqlmaster_password=*****                        # user ' S password to change master for master ' S mysqlmaster_host=$ (mysql -uroot -p$root_passwd -e  ' Show slave status\G ' | Grep master_host|awk -f ':  '   ' {print $2} ')   2>/dev/null  #find   the master host now#echo  $master _host     #to  test the master host[ $ master_host =  $master _ip ] && slave_host= $slave _ip | |  slave_host= $master _ip  2>/dev/null  #definition  the slave host#echo   $slave _host      #to  test the slave host             mysql -uroot -p$root_passwd -e  ' Stop  slave '  2>/dev/null   #stop  the mysql slave thread#echo $?1     #test  the stop slave is yes or okmysql -uroot - p$root_passwd -e  ' flush logs '    2>/dev/null  #flush  the binlogs #echo  $?2                                         #test   flush logs is ok or nobin_log=$ (mysql -uroot -p$root_passwd -e  ' Show master status\g ' |grep file|awk -f ':  '   ' {print $2} ')  2>/dev/ null  #find  the master_log_file#echo  $bin _log    #test  the master_ log_filepos=$ (mysql -uroot -p$root_passwd -e  ' show master status\g ' |grep  Position|awk -f ':  '   ' {print $2} ')  2>/dev/null  #find  the master_log_ pos#echo  $pos       #test  the master_log_posmysql -u$user -p$ passwd -h$master_host -e  "Stop slave"  2>/dev/null  #stop  the mysql  slave thread#echo $?3       #03  ok   13  No   to&nBsp;test the stop slave threa is ok?   mysql -u$user -p $passwd  -h$master_host -e  ' change  master to master_host= ' $slave _host ', Master_user= ' $master _user ', master_password= ' $master _password ', master_log_file= ' $bin _log ', master_log_pos= $pos "  2>/dev/null  #change  slave to master  #echo  $?4        #04  ok  14 no    to test the change  slave to master is ok?mysql -u$user -p$passwd -h$master_host -e  " Start slave "   2>/dev/null   #slave  start  slave   thread#echo $?5       #05  ok   15 no    to test the start slave thread is ok?mysql -u$user -p$ Passwd -h$master_host -e  "Show slave status\g"  2>/dev/null  #slave  show slave status# echo $?6   #06  ok 16 no  to test slave status

The following master-slave script written for Python (can operate on a server that manages MySQL)

#!/usr/sbin/python#  ^-^  coding: utf-8  ^-^ #  ^-^  author: chen min ^ -^#  ^-^  time: 2015-10-22  ^-^ import mysqldbslave=raw_input (' please input  SLAVE_IP: ') if slave== ' 192.168.1.134 ':     master= ' 192.168.1.136 ' else:     master= ' 192.168.1.134 ' Global slave,masterdef getpos ():    conn  = mysqldb.connect (        host =  '%s '%slave,         port = 3306,         db =  ' test ',        user =  ' Paixian ',         passwd =  ' *********** ',         charset =  ' UTF8 '     )     cursor =  conn.cursor (cursorclass=mysqldb. Cursors. Dictcursor)     try:#        print slave, Master,type (slave)         cursor.execute ("Stop slave")          cursor.execute ("Flush logs")          sql =  "Show master status"          n = cursor.execute (SQL)         a =  Cursor.fetchall ()         bin_log = a[0].get (' File ')          pos = a[0].get (' Position ')          global bin_log,pos        conn.commit ()      except exception as e:        print  e    finally:         cursor.close ()          conn.close () Def changeslave ():     conn1 = mysqldb.connect (         host =  '%s '%master,         port = 3306,        db =  ' Test ',         user =  ' Paixian ',         passwd =  ' *********** ',         charset =   ' UTF8 '          cursor1=conn1.cursor (cursorclass= MySQLdb.cursors.DictCursor)     try:#        print  bin_log,pos,slave,master        cursor1.execute ("Stop slave ")        &nbsP;cursor1.execute ("change master to master_host= '%s ',  master_user= ' Myslave ',  master_ Password= ' 123123 ',  master_log_file= '%s ',  master_log_pos=%d "% (slave,bin_log,pos))          cursor1.execute ("Start slave")          m = cursor1.execute ("Show slave status")          m1 = cursor1.fetchall ()         y = m1[0].get (' slave_io_running ')         x = m1[0].get (' Slave_SQL_Running ') #        print x,y         if x== ' yes '  and y== ' yes ':             print  "Slave to master is ok"          conn1.commit ()  &Nbsp;  except exception as e:        print  e    finally:        cursor1.close ()          conn1.close        try:     getpos ()     changeslave () Except exception as e:     print e


This article is from the "innovation sharing gallop inside and out" blog, please be sure to keep this source http://10554846.blog.51cto.com/10544846/1705298

2 sets of MySQL master-slave cross-cutting scripts

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.