當(dāng)前位置:首頁(yè) > IT技術(shù) > 數(shù)據(jù)庫(kù) > 正文

配置Mysql+Keepalived故障轉(zhuǎn)移的高可用環(huán)境
2021-10-12 14:27:08

安裝keepalived并將其配置成系統(tǒng)服務(wù)。master1和master2兩臺(tái)機(jī)器上同樣進(jìn)行如下操作:



yum install -y openssl-devel keepalived


在db01配置keepalived


cd /etc/keepalived/
mv keepalived.conf keepalived.conf.bak #重命名
vi keepalived.conf
global_defs {
router_id db01
}

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

vrrp_instance VI_1 {
state MASTER
interface eth0
mcast_src_ip 192.168.40.216
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.40.218
}
track_script {
chk_mysql_port
}
}

編寫切換腳本,KeepAlived做心跳檢測(cè),如果db01的MySQL服務(wù)3306端口不可用,slave接管VIP


vi /opt/chk_mysql.sh
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
systemctl stop keepalived
fi

chmod +x /opt/chk_mysql.sh
systemctl restart keepalived.service
systemctl enable keepalived.service

在db02配置keepalived


cd /etc/keepalived/
mv keepalived.conf keepalived.conf.bak #重命名

vi keepalived.conf

global_defs {
router_id db02
}

vrrp_script chk_mysql_port {
script "/opt/chk_mysql.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
mcast_src_ip 192.168.40.217
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.40.218
}

track_script {
chk_mysql_port
}
}

編寫切換腳本,KeepAlived做心跳檢測(cè),如果db01的MySQL服務(wù)3306端口不可用,slave接管VIP


vi /opt/chk_mysql.sh

#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
systemctl stop keepalived
fi

chmod +x /opt/chk_mysql.sh
systemctl restart keepalived.service
systemctl enable keepalived.service


本文摘自 :https://blog.51cto.com/u

開通會(huì)員,享受整站包年服務(wù)立即開通 >