如果我们使用的MHA时,app1.cnf 中的master_ip_failover_script和master_ip_online_change_script两项没有配,即没有使用MHA的VIP方式,可使用keepalived来设置VIP。
本例中keepalived的master端和backup端ip:
master:10.1.5.8
backup:10.1.5.9
MHA配置见:http://blog.csdn.net/lichangzai/article/details/50470771
linux6自带keepalived,yum installkeepalived安装,配置样本是vi /etc/keepalived/keepalived.conf
linux5需下载安装,如下:
下载:http://www.keepalived.org/download.html
# tar zxvf keepalived-1.2.19.tar.gz
# cd keepalived-1.2.19
# ./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.39-300.26.1.el5uek
如遇到configure: error: No SO_MARK declaration in headers,则用./configure --prefix=/usr/local/keepalived --disable-fwmark
# make
# make install
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# mkdir /etc/keepalived
master配置:
# cat/etc/keepalived/keepalived.conf
! Configuration File forkeepalived
global_defs {
notification_email {
}
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
10.1.5.21/24
}
}
backup配置:
# cat /etc/keepalived/keepalived.conf
! Configuration File forkeepalived
global_defs {
notification_email {
}
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
10.1.5.21/24
}
}
安装keepalieved的节点,定时检查MySQL实例是否存活,宕掉时自动杀掉keepalived进程,从而实现VIP转移到backup节点。
# crontab -l
* * * * * /data/mysqldata/scripts/check_keepalived.sh > /dev/null 2>&1 &
# cat /data/mysqldata/scripts/check_keepalived.sh
#/bin/bash
source /root/.bash_profile
# source /data/mysqldata/scripts/mysql_env.ini
MYSQL_CMD=/usr/local/mysql/bin/mysql
CHECK_TIME=3 #check 3 times
MYSQL_OK=1 #MYSQL_OK values to 1 when mysql servicework,else values to 0
function check_mysql_health (){
$MYSQL_CMD -uroot -p123456 -S /var/lib/mysql/mysql.sock -e "show status;" > /dev/null 2>&1
if [ $? = 0 ]
then
MYSQL_OK=1
/etc/init.d/keepalived start
else
MYSQL_OK=0
fi
return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -=1"
check_mysql_health
if [ $MYSQL_OK = 1 ]
then
CHECK_TIME=0
exit 0
fi
if [ $MYSQL_OK -eq 0 ] && [$CHECK_TIME -eq 0 ]
then
/etc/init.d/keepalived stop
echo "mysql 3306实例宕掉,keepalived已关闭" |mutt -s "master keepalived已关闭"[email protected]
exit 1
fi
sleep 1
done