成都网站建设设计

将想法与焦点和您一起共享

5.6MySql主从自动切换脚本

2015.07.17


  1. #!/bin/bash
  2. cat << README
  3. #####################################################################################################
  4. #Auther :zhanglin                                                                                   #
  5. #Date : 2015.07.17                                                                                  #
  6. #Step 1: point the slave IP                                                #
  7. #Step 2: check the master and slave information whether good for change                   #
  8. #Step 3: stop old slave,then get new master binlog name and postation,then execute change master to #
  9. #Step 4: start slave,and show whether change successed.                                             #
  10. #####################################################################################################
  11. README
  12. User=root
  13. PW=123456
  14. read -p "-- Please input the slave IP:" Slave
  15. Master=$(MySQL -u${User} -h${Slave} -p${PW} -e "show slave status \G;"|awk '/Master_Host/{print $2}')
  16.  if [ -n "${Master}" ]
  17. then
  18. echo -e "--Master IP:${Master},Slave IP:${Slave}"
  19. M=$(mysql -u${User} -h${Master} -p${PW} -e "show master status;"|awk 'NR==2{print $2}')
  20. S=$(mysql -u${User} -h${Slave} -p${PW} -e "show slave status \G;"|awk '/Read_Master_Log_Pos/{print $2}')
  21. echo -e "-- master pos:${M};slave pos is:${S}"
  22.  else
  23. echo "-- Slave IP input wrong,please input again ! "
  24. exit 1
  25.  fi
  26.        if [ "${M}" -eq "${S}" ]
  27. then
  28.  read -p "-- Master-Slave is accordance,input Yes to start changing:" var
  29.             case "$var" in
  30.                 [Yy]es)
  31.                     mysql -u${User} -h${Slave} -p${PW} -e "stop slave;reset slave;change master to master_host='';"
  32.                     Pos=$(mysql -u${User} -h${Slave} -p${PW} -e "show master status;"|awk 'NR==2{print $2}')
  33.                     File=$(mysql -u${User} -h${Slave} -p${PW} -e "show master status;"|awk 'NR==2{print $1}')
  34.                     mysql -u${User} -h${Master} -p${PW} -e "stop slave;
  35.                     change master to master_user='ideal',master_host='${Slave}',master_password='123456',master_log_file='${File}',master_log_pos=${Pos};
  36.                    start slave;"
  37.                    ;;
  38.                 *)
  39.                    echo "-- error input .... exit!"
  40.                    ;;
  41.            esac
  42.                echo "-- changing, please wait 3s...."
  43.                sleep 3
  44.                echo "-- change successfull,the new master is:${Slave},new slave is ${Master}"
  45.                 SlaveIOStatus=$(mysql -u${User} -h${Master} -p${PW} -e "show slave status \G;"|awk '/Slave_IO_Running/{print $2}')
  46.                 SlaveSQLStatus=$(mysql -u${User} -h${Master} -p${PW} -e "show slave status \G;"|awk '/Slave_SQL_Running/{print $2}')
  47.                 printf "The new master is: \n Slave_IO_Running=${SlaveIOStatus}\n"
  48.                 printf "Slave_SQL_Running=${SlaveSQLStatus}\n"
  49.           else
  50.           echo "node change failed please change again ! "
  51.           exit 1
  52.          fi



网页标题:5.6MySql主从自动切换脚本
网站网址:http://chengdu.cdxwcx.cn/article/picgog.html