MySql教程

mysql HA

本文主要是介绍mysql HA,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
MySQL5.7+Keepalived双主HA架构 https://blog.csdn.net/cuiruidu3106/article/details/100438947

HA部署步骤

1、mysql_check.sh脚本

发现mysql挂了 杀死本机的keepalived 主备均部署
cat > /etc/keepalived/mysql_check.sh << "EOF"
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
   echo "$(date +'%F %T') mysql down, keepalived will stop" >> /var/log/keepalived.log
   systemctl stop keepalived
fi
EOF

 

赋予执行权限 chmod +x /etc/keepalived/mysql_check.sh  

2、Master配置文件编写

只主部署
cat >/etc/keepalived/keepalived.conf << EOF
global_defs {
    router_id mysql_01        # 设置master的id,在一个网络应该是唯一的
}
vrrp_script chk_script {
    script "/etc/keepalived/mysql_check.sh"    #最后手动执行下此脚本,以确保此脚本能够正常执行
    interval 2                          #(检测脚本执行的间隔,单位是秒)
    weight 20
}
vrrp_instance VI_1 {
    state BACKUP
    nopreempt
    priority 100            # 优先级,数值越大,获取处理请求的优先级越高
    
    advert_int 1            # 检查间隔,默认为1s(vrrp组播周期秒数)
    virtual_router_id 77        # 虚拟路由编号,主从要一致
    interface eth0            # 当前进行vrrp通讯的网络接口卡(当前centos的网卡)

    authentication {
        auth_type PASS
        auth_pass yunzhihui123
    }
    track_script {
        chk_script           #(调用检测脚本)
    }
    virtual_ipaddress {
        172.16.1.254           # 定义虚拟ip(VIP),可多设,每行一个
    }
}
EOF

3、Backup配置文件编写

只备部署
cat >/etc/keepalived/keepalived.conf << EOF
global_defs {
    router_id mysql_02       # 设置backup的id,在一个网络应该是唯一的
}
vrrp_script chk_script {
    script "/etc/keepalived/mysql_check.sh"    #最后手动执行下此脚本,以确保此脚本能够正常执行
    interval 2                          #(检测脚本执行的间隔,单位是秒)
    weight 20
}
vrrp_instance VI_1 {
    state BACKUP
    nopreempt
    priority 90            # 优先级,数值越大,获取处理请求的优先级越高
    
    advert_int 1            # 检查间隔,默认为1s(vrrp组播周期秒数)
    virtual_router_id 77        # 虚拟路由编号,主从要一致
    interface eth0            # 当前进行vrrp通讯的网络接口卡(当前centos的网卡)

    authentication {
        auth_type PASS
        auth_pass yunzhihui123
    }
    track_script {
        chk_script            #(调用检测脚本)
    }
    virtual_ipaddress {
        172.16.1.254            # 定义虚拟ip(VIP),可多设,每行一个
    }
}
EOF

4、配置文件生效

systemctl stop keepalived systemctl start keepalived

HA验证

1、查看VIP在哪台机器 ip a VIP 目前在主,备无VIP 如果两台都有VIP,请检查防火墙,确保只有一台有VIP   3、杀死主上的Mysql 推荐systemctl stop mysql   4、验证VIP漂移 VIP 现在跑到了备,符合VIP现状   6、HA验证完毕后恢复原状 主 备 启动Mysql (先) 和 Keepalived (后)    

其他

恢复

Mysql启动后(先),可以重新启动Keepalived(后),进行恢复 如果发现VIP正常,但打不开网页,可能网关arp没有刷新,在VIP所在机器上执行以下命令一两次 172.16.1.254 为VIP 172.16.1.1 为网关 查看网关 netstat -rn (以0.0.0.0开始的行的gateway是默认网关) /sbin/arping -I eth0 -c 5 -s 172.16.1.254 172.16.1.1 &>/dev/null

 

HA部署步骤
1、mysql_check.sh脚本
发现mysql挂了 杀死本机的keepalived
主备均部署
cat > /etc/keepalived/check_mysql.sh << "EOF"
#!/bin/bash
MYSQL=/data/app/mysql/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=yunzhihui123
CHECK_TIME=3
  
#mysql  is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0
  
MYSQL_OK=1
  
function check_mysql_helth (){
    $MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "select 1 from dual;" >/dev/null 2>&1
    if [ $? = 0 ] ;then
    MYSQL_OK=1
    else
    MYSQL_OK=0
    fi
    return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
    let "CHECK_TIME -= 1"
    check_mysql_helth
if [ $MYSQL_OK = 1 ] ; then
    CHECK_TIME=0
    exit 0
fi
if [ $MYSQL_OK -eq 0 ] &&  [ $CHECK_TIME -eq 0 ]
then
    echo "$(date +'%F %T') mysql down, keepalived will stop" >> /var/log/keepalived.log
    pkill keepalived
    exit 1
fi
sleep 1
done
EOF

赋予执行权限
chmod u+x /etc/keepalived/check_mysql.sh

2、Master配置文件编写
只主部署
cat >/etc/keepalived/keepalived.conf << EOF
global_defs {
    router_id mysql_01        # 设置master的id,在一个网络应该是唯一的
}
vrrp_script chk_script {
    script "/etc/keepalived/check_mysql.sh"    #最后手动执行下此脚本,以确保此脚本能够正常执行
    interval 5                          #(检测脚本执行的间隔,单位是秒)
    weight 20
}
vrrp_instance VI_1 {
    state BACKUP
    nopreempt
    priority 100            # 优先级,数值越大,获取处理请求的优先级越高
    
    advert_int 1            # 检查间隔,默认为1s(vrrp组播周期秒数)
    virtual_router_id 77        # 虚拟路由编号,主从要一致
    interface eth0            # 当前进行vrrp通讯的网络接口卡(当前centos的网卡)

    authentication {
        auth_type PASS
        auth_pass mysqlyunzhihui123
    }
    track_script {
        chk_script           #(调用检测脚本)
    }
    virtual_ipaddress {
        172.16.1.254           # 定义虚拟ip(VIP),可多设,每行一个
    }
    notify_master /etc/keepalived/notify_master_mysql.sh
}
EOF

3、Backup配置文件编写
只备部署
cat >/etc/keepalived/keepalived.conf << EOF
global_defs {
    router_id mysql_02       # 设置backup的id,在一个网络应该是唯一的
}
vrrp_script chk_script {
    script "/etc/keepalived/check_mysql.sh"    #最后手动执行下此脚本,以确保此脚本能够正常执行
    interval 5                          #(检测脚本执行的间隔,单位是秒)
    weight 20
}
vrrp_instance VI_1 {
    state BACKUP
    nopreempt
    priority 90            # 优先级,数值越大,获取处理请求的优先级越高
    
    advert_int 1            # 检查间隔,默认为1s(vrrp组播周期秒数)
    virtual_router_id 77        # 虚拟路由编号,主从要一致
    interface eth0            # 当前进行vrrp通讯的网络接口卡(当前centos的网卡)

    authentication {
        auth_type PASS
        auth_pass mysqlyunzhihui123
    }
    track_script {
        chk_script            #(调用检测脚本)
    }
    virtual_ipaddress {
        172.16.1.254            # 定义虚拟ip(VIP),可多设,每行一个
    }
    notify_master /etc/keepalived/notify_master_mysql.sh
}
EOF

4、配置文件生效
systemctl stop keepalived
systemctl start keepalived
HA验证
1、查看VIP在哪台机器
ip a
VIP 目前在主,备无VIP
如果两台都有VIP,请检查防火墙,确保只有一台有VIP

3、杀死主上的Mysql
推荐systemctl stop mysql

4、验证VIP漂移
VIP 现在跑到了备,符合VIP现状

6、HA验证完毕后恢复原状
 主 备 启动Mysql (先) 和 Keepalived (后)


其他
恢复
Mysql启动后(先),可以重新启动Keepalived(后),进行恢复
如果发现VIP正常,但打不开网页,可能网关arp没有刷新,在VIP所在机器上执行以下命令一两次
172.16.1.254 为VIP
172.16.1.1 为网关
查看网关
netstat -rn
(以0.0.0.0开始的行的gateway是默认网关)
/sbin/arping -I eth0 -c 5 -s 172.16.1.254 172.16.1.1 &>/dev/null

关于SUSE 12防火墙
关于SUSE 12防火墙 

 

这篇关于mysql HA的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!