keepalived+MySQL主主高可用
一、环境介绍:
10.1.1.174:23316 M1
10.1.1.237:23316 M2
二、配置双主:略。
三、keepalive安装
[[email protected] ~]# cd /usr/local/src/
[[email protected] src]# wget https://www.keepalived.org/software/keepalived-2.0.17.tar.gz
[[email protected] src]# tar -xf keepalived-2.0.17.tar.gz
[[email protected] src]# cd keepalived-2.0.17
[[email protected] keepalived-2.0.17]# yum install openssl* libnl‐dev* gcc-c++
[[email protected] keepalived-2.0.17]# ./configure --prefix=/usr/local/keepalived
[[email protected] keepalived-2.0.17]# make
[[email protected] 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 /etc/keepalived/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
}
}
[[email protected] ~]# 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配置文件:
[[email protected]#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
}
}
[[email protected]#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服务
[[email protected] ~]# /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,有的话注释掉,重启数据库。
原文地址:https://blog.51cto.com/woniu123/2465006
时间: 2024-10-12 06:07:52