mysql+keepalived主從切換指令碼 轉

來源:互聯網
上載者:User

標籤:

Keepalived MySQL故障自動切換指令碼 

MySQL架構為master-slave(主從),master故障自動切換到slave上。當然也可以設定為雙master,但這裡有個弊端:就是當主的壓力很大時,從上延時很大,比如落後2000秒,此時主掛了,從接管(VIP漂移到從),使用者剛才發表的文章,此時因為同步延時大,還沒複製過來,於是使用者又發表了一篇文章,當原來的master修好後,因從的IO和SQL線程還在開啟狀態,還會繼續同步剛才沒有同步複製完的資料,這時有可能把使用者新發表的文章更改掉,造成使用者資料丟失。

考慮到這種情況,我這裡還是用的master-slave(主從)架構。

keepalive安裝很簡單,這裡不再囉嗦。主要看下設定檔和指令碼:


  1. # more /etc/keepalived/keepalived.conf  

  2. global_defs {

  3.   router_id KeepAlive_Mysql

  4. }  

  5.  

  6. vrrp_script check_run {

  7. script "/root/sh/mysql_check.sh"

  8. interval 300 

  9. }

  10.  

  11. vrrp_sync_group VG1 {

  12. group {

  13. VI_1

  14. }

  15. }

  16.  

  17. vrrp_instance VI_1 {

  18.    state BACKUP

  19.    interface eth0  

  20.    virtual_router_id 51

  21.    priority 100  

  22.    advert_int 1

  23.    nopreempt

  24.    authentication {

  25.        auth_type PASS

  26.        auth_pass 1111

  27.    }

  28.    track_script {

  29.    check_run

  30.    }

  31.  

  32.    notify_master /root/sh/master.sh

  33.    notify_backup /root/sh/backup.sh

  34.    notify_stop /root/sh/stop.sh

  35.  

  36.    virtual_ipaddress {

  37.        192.168.8.150

  38.    }

  39. }

notify_master <STRING>|<QUOTED-STRING>    # 狀態改變為MASTER後執行的指令碼notify_backup <STRING>|<QUOTED-STRING>    # 狀態改變為BACKUP後執行的指令碼notify_fault <STRING>|<QUOTED-STRING>    # 狀態改變為FAULT後執行的指令碼notify_stop <STRING>|<QUOTED-STRING>    # VRRP停止後後執行的指令碼notify <STRING>|<QUOTED-STRING>        # (1)任意狀態改變後執行的指令碼

下面解釋下這4個指令碼的用法:

mysql_check.sh(健全狀態檢查指令碼,當發現mysql串連不上,會把keepalive進程關閉,並切換。)

  1. # more mysql_check.sh  

  2. #!/bin/bash

  3.  

  4. . /root/.bash_profile

  5.  

  6. count=1

  7.  

  8. while true

  9. do

  10.  

  11. mysql -e "show status;" > /dev/null 2>&1

  12. i=$?

  13. ps aux | grep mysqld | grep -v grep > /dev/null 2>&1

  14. j=$?

  15. if [ $i = 0 ] && [ $j = 0 ]

  16. then

  17.   exit 0

  18. else

  19.   if [ $i = 1 ] && [ $j = 0 ]

  20.   then

  21.       exit 0

  22.   else

  23.        if [ $count -gt 5 ]

  24.        then

  25.              break

  26.        fi

  27.   let count++

  28.   continue

  29.   fi

  30. fi

  31.  

  32. done

  33.  

  34. /etc/init.d/keepalived stop

master.sh(狀態改變為MASTER後執行的指令碼)  (首先判斷同步複製是否執行完畢,如果未執行完畢,等1分鐘後,不論是否執行完畢,都跳過,並停止同步複製進程。) (其次,更改前端程式串連的業務帳號admin的許可權和密碼,並記錄當前切換以後的日誌和POS點。)

  1. # more master.sh

  2. #!/bin/bash

  3.  

  4. . /root/.bash_profile

  5.  

  6. Master_Log_File=$(mysql -e "show slave status\G" | grep -w Master_Log_File | awk -F": " ‘{print $2}‘)

  7. Relay_Master_Log_File=$(mysql -e "show slave status\G" | grep -w Relay_Master_Log_File | awk -F": " ‘{print $2}‘)

  8. Read_Master_Log_Pos=$(mysql -e "show slave status\G" | grep -w Read_Master_Log_Pos | awk -F": " ‘{print $2}‘)

  9. Exec_Master_Log_Pos=$(mysql -e "show slave status\G" | grep -w Exec_Master_Log_Pos | awk -F": " ‘{print $2}‘)

  10.  

  11. i=1

  12.  

  13. while true

  14. do

  15.  

  16. if [ $Master_Log_File = $Relay_Master_Log_File ] && [ $Read_Master_Log_Pos -eq $Exec_Master_Log_Pos ]

  17. then

  18.   echo "ok"

  19.   break

  20. else

  21.   sleep 1

  22.  

  23.   if [ $i -gt 60 ]

  24.   then

  25.      break

  26.   fi

  27.   continue

  28.   let i++

  29. fi

  30. done

  31.  

  32. mysql -e "stop slave;"

  33. mysql -e "flush logs;GRANT ALL PRIVILEGES ON *.* TO ‘admin‘@‘%‘ IDENTIFIED BY ‘admin‘;flush privileges;"

  34. mysql -e "show master status;" > /tmp/master_status_$(date "+%y%m%d-%H%M").txt

backup.sh(狀態改變為BACKUP後執行的指令碼)

  1. # more backup.sh  

  2. #!/bin/bash

  3.  

  4. . /root/.bash_profile

  5.  

  6. mysql -e "GRANT ALL PRIVILEGES ON *.* TO ‘admin‘@‘%‘ IDENTIFIED BY ‘1q2w3e4r‘;flush privileges;"

stop.sh(keepalived停止後後執行的指令碼) (首先把admin密碼更改掉) (其次,設定參數,保證不遺失資料) (最後,查看是否還有寫操作,不論是否執行完畢,1分鐘後都退出。)

  1. # more stop.sh  

  2. #!/bin/bash

  3.  

  4. . /root/.bash_profile

  5.  

  6. mysql -e "GRANT ALL PRIVILEGES ON *.* TO ‘admin‘@‘%‘ IDENTIFIED BY ‘1q2w3e4r‘;flush privileges;"

  7. mysql -e "set global innodb_support_xa=1;"

  8. mysql -e "set global sync_binlog=1;"

  9. mysql -e "set global innodb_flush_log_at_trx_commit=1;"

  10.  

  11. M_File1=$(mysql -e "show master status\G" | awk -F‘: ‘ ‘/File/{print $2}‘)

  12. M_Position1=$(mysql -e "show master status\G" | awk -F‘: ‘ ‘/Position/{print $2}‘)

  13. sleep 1

  14. M_File2=$(mysql -e "show master status\G" | awk -F‘: ‘ ‘/File/{print $2}‘)

  15. M_Position2=$(mysql -e "show master status\G" | awk -F‘: ‘ ‘/Position/{print $2}‘)

  16.  

  17. i=1

  18.  

  19. while true

  20. do

  21.  

  22. if [ $M_File1 = $M_File2 ] && [ $M_Position1 -eq $M_Position2 ]

  23. then

  24.   echo "ok"

  25.   break

  26. else

  27.   sleep 1

  28.  

  29.   if [ $i -gt 60 ]

  30.   then

  31.      break

  32.   fi

  33.   continue

  34.   let i++

  35. fi

  36. done

mysql+keepalived主從切換指令碼 轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.