keepalived+MySQL主主高可用

发布时间:2020-02-19 15:02:04 作者:DB柠檬茶
来源:网络 阅读:207

keepalived+MySQL主主高可用

一、环境介绍:

10.1.1.174:23316 M1
10.1.1.237:23316 M2

二、配置双主:略。
三、keepalive安装
[root@master1 ~]# cd /usr/local/src/

[root@master1 src]# wget https://www.keepalived.org/software/keepalived-2.0.17.tar.gz

[root@master1 src]# tar -xf keepalived-2.0.17.tar.gz

[root@master1 src]# cd keepalived-2.0.17

[root@master1 keepalived-2.0.17]# yum install openssl* libnl‐dev* gcc-c++

[root@master1 keepalived-2.0.17]# ./configure --prefix=/usr/local/keepalived

[root@master1 keepalived-2.0.17]# make

[root@master1 keepalived-2.0.17]# make install
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
echo "/etc/init.d/keepalived start" >> /etc/rc.local
mkdir -p  /etc/keepalived/
mv /etc/keepalived/keepalived.conf /root/keepalived.conf.bak
M1:keepalive配置文件:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

vrrp_script chk_mysql_port {     #检测mysql服务是否在运行。有很多方式,比如进程,用脚本检测等等
    script "/etc/keepalived/chk_mysql.sh"   #这里通过脚本监测
    interval 2                   #脚本执行间隔,每2s检测一次
    weight -5                    #脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5
    fall 2                    #检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)
    rise 1                    #检测1次成功就算成功。但不修改优先级
}

vrrp_instance VI_1 {
    state MASTER    
    interface ens192      #指定虚拟ip的网卡接口,改成你自己的 eth0或者其它什么的
   # mcast_src_ip 10.1.1.174
    virtual_router_id 51    #路由器标识,MASTER和BACKUP必须是一致的
    priority 101            #定义优先级,数字越大,优先级越高,在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级。这样MASTER故障恢复后,就可以将VIP资源再次抢回来 
    unicast_src_ip  10.1.1.174 ##(本地IP地址)
    unicast_peer {
                  10.1.1.237 ##(对端IP地址)此地址一定不能忘记
                       }
    advert_int 1         
    authentication {   
        auth_type PASS 
        auth_pass 1111     
    }
    virtual_ipaddress {    
        10.1.1.111
    }

track_script {               
   chk_mysql_port             
}
}
[root@crm-db01 ~]# cat /etc/keepalived/chk_mysql.sh         #####注意可执行权限
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "23316"|wc -l)
if [ "${counter}" -eq 0 ]; then
    /etc/init.d/keepalived stop
fi
M2:keepalived配置文件:
[root@#localhost keepalived]# cat keepalived.conf
! Configuration File for keepalived

vrrp_script chk_mysql_port {
    script "/etc/keepalived/chk_mysql.sh"
    interval 2
    weight -5
    fall 2
    rise 1
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens192             #指定虚拟ip的网卡接口,改成你自己的 eth0或者其它什么的        
    #mcast_src_ip 10.1.1.237
    virtual_router_id 51
    priority 90
    unicast_src_ip  10.1.1.237 ##(本地IP地址)
    unicast_peer {
                  10.1.1.174       ##(对端IP地址)此地址一定不能忘记
                       }
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.1.1.111
    }

track_script {
   chk_mysql_port
}
}

[root@#localhost keepalived]# cat chk_mysql.sh           #####注意可执行权限
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "23316"|wc -l)
if [ "${counter}" -eq 0 ]; then
    /etc/init.d/keepalived stop
fi
启动keepalived服务
[root@master1 ~]# /etc/init.d/keepalived start
常用命令:
/etc/init.d/keepalived restart|start|stop
/etc/init.d/mysqld restart|start|stop
查看keepalive状态:
systemctl status keepalived.service
查看日志:
tail -f /var/log/messages
四、可能出现的问题:
1.启动两个keepalived ,两边都有VIP。
我这个配置文件已经修改过,应该不会出现这个情况。
2.VIP起来后,无法通过VIP访问MySQL数据库。
关闭防火墙或者开放相关端口。
查看你的MySQL配置文件,看看有没有这个参数#bind-address = 10.1.1.237,有的话注释掉,重启数据库。
推荐阅读:
  1. MySQL高可用方案——双主
  2. Memcached高可用群集(Memcached主主复制+Keepalived)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysql双主+keepalive高用 alived

上一篇:通过vCenter Server管理多台ESXi主机

下一篇:10分钟使用 Azure Media Services 搭建

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》