| 角色 | IP 地址 | 說明 |
|---|---|---|
| MySQL-VIP | 192.168.7.253 | 虛擬 IP,對外提供服務 |
| MySQL-Master1 | 192.168.7.201 | 主節點,Priority 100 |
| MySQL-Master2 | 192.168.7.249 | 備節點,Priority 90 |
系統:CentOS 6.3 x64 | MySQL:5.6.10 | Keepalived:1.2.7
# 關閉防火牆與 SELinux(生產環境建議開放 vrrp 協議而非關閉)
service iptables stop
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux
# 若需保留 iptables,請放行 VRRP 協議
iptables -A INPUT -p vrrp -j ACCEPT
wget https://www.keepalived.org/software/keepalived-1.2.7.tar.gz
tar zxvf keepalived-1.2.7.tar.gz
cd keepalived-1.2.7
# 注意 kernel 版本需與當前系統一致
./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-279.el6.x86_64
make && make install
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
chkconfig keepalived on
global_defs {
notification_email {
lzyangel@126.com
}
notification_email_from lzyangel@126.com
smtp_server smtp.126.com
smtp_connect_timeout 30
router_id MySQL-ha
}
vrrp_instance VI_1 {
state BACKUP # 兩節點均設 BACKUP,配合 nopreempt 避免搶佔
interface eth0
virtual_router_id 51
priority 100 # Master2 改為 90
advert_int 1
nopreempt # 非搶佔模式,穩定性優先
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.7.253
}
}
virtual_server 192.168.7.253 3306 {
delay_loop 2
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.7.201 3306 {
weight 3
notify_down /etc/rc.d/keepalived.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
#!/bin/bash
/etc/init.d/keepalived stop
chmod +x /etc/rc.d/keepalived.sh
real_server(本機 MySQL)健康檢查失敗,觸發 notify_down 執行腳本停止本機 Keepalived下篇將介紹 MySQL 主主同步配置、Keepalived 雙節點完整部署驗證、腦裂防護與生產環境調優建議。