• mysql+keepalived主从切换脚本 转


    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. vrrp_script check_run {

    6. script "/root/sh/mysql_check.sh"

    7. interval 300 

    8. }

    9. vrrp_sync_group VG1 {

    10. group {

    11. VI_1

    12. }

    13. }

    14. vrrp_instance VI_1 {

    15.    state BACKUP

    16.    interface eth0  

    17.    virtual_router_id 51

    18.    priority 100  

    19.    advert_int 1

    20.    nopreempt

    21.    authentication {

    22.        auth_type PASS

    23.        auth_pass 1111

    24.    }

    25.    track_script {

    26.    check_run

    27.    }

    28.    notify_master /root/sh/master.sh

    29.    notify_backup /root/sh/backup.sh

    30.    notify_stop /root/sh/stop.sh

    31.    virtual_ipaddress {

    32.        192.168.8.150

    33.    }

    34. }

    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. . /root/.bash_profile

    4. count=1

    5. while true

    6. do

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

    8. i=$?

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

    10. j=$?

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

    12. then

    13.   exit 0

    14. else

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

    16.   then

    17.       exit 0

    18.   else

    19.        if [ $count -gt 5 ]

    20.        then

    21.              break

    22.        fi

    23.   let count++

    24.   continue

    25.   fi

    26. fi

    27. done

    28. /etc/init.d/keepalived stop

    master.sh(状态改变为MASTER后执行的脚本)  (首先判断同步复制是否执行完毕,如果未执行完毕,等1分钟后,不论是否执行完毕,都跳过,并停止同步复制进程。) (其次,更改前端程序连接的业务账号admin的权限和密码,并记录当前切换以后的日志和POS点。)
    
    
    1. # more master.sh

    2. #!/bin/bash

    3. . /root/.bash_profile

    4. Master_Log_File=$(mysql -e "show slave statusG" | grep -w Master_Log_File | awk -F": " '{print $2}')

    5. Relay_Master_Log_File=$(mysql -e "show slave statusG" | grep -w Relay_Master_Log_File | awk -F": " '{print $2}')

    6. Read_Master_Log_Pos=$(mysql -e "show slave statusG" | grep -w Read_Master_Log_Pos | awk -F": " '{print $2}')

    7. Exec_Master_Log_Pos=$(mysql -e "show slave statusG" | grep -w Exec_Master_Log_Pos | awk -F": " '{print $2}')

    8. i=1

    9. while true

    10. do

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

    12. then

    13.   echo "ok"

    14.   break

    15. else

    16.   sleep 1

    17.   if [ $i -gt 60 ]

    18.   then

    19.      break

    20.   fi

    21.   continue

    22.   let i++

    23. fi

    24. done

    25. mysql -e "stop slave;"

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

    27. 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. . /root/.bash_profile

    4. 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. . /root/.bash_profile

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

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

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

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

    8. M_File1=$(mysql -e "show master statusG" | awk -F': ' '/File/{print $2}')

    9. M_Position1=$(mysql -e "show master statusG" | awk -F': ' '/Position/{print $2}')

    10. sleep 1

    11. M_File2=$(mysql -e "show master statusG" | awk -F': ' '/File/{print $2}')

    12. M_Position2=$(mysql -e "show master statusG" | awk -F': ' '/Position/{print $2}')

    13. i=1

    14. while true

    15. do

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

    17. then

    18.   echo "ok"

    19.   break

    20. else

    21.   sleep 1

    22.   if [ $i -gt 60 ]

    23.   then

    24.      break

    25.   fi

    26.   continue

    27.   let i++

    28. fi

    29. done

  • 相关阅读:
    pgloader-pg迁移神器
    PostgreSQL备份工具-pg_probackup
    5、pgpool-II高可用性(一)数据库的高可用性
    4、pgpool-II 流复制模式
    3、pgpool-II 内置复制模式
    pgpool-II 的配置
    pgpool-II安装
    PG时间相减
    PostgreSQL 流复制解惑
    PostgreSQL改造非分区表为分区表
  • 原文地址:https://www.cnblogs.com/liqing1009/p/4904769.html
Copyright © 2020-2023  润新知