吴贺华 发表于 2015-9-4 08:42:40

Keepalived+mysql 故障自动切换

  Keepalived+mysql 故障自动切换
网络结构:
VIP 192.168.20.50
mysq11 192.168.20.37 主
mysql2 192.168.20.38 备

一 mysql 主主同步
二 安装keepalived
三 keepalived 主备配置文件
四 mysql状态检测脚本/root/bin/keepalived_check_mysql.sh
五 防火墙设置
注 keepalived之间使用vrrp组播方式通信使用的IP地址是224.0.0.18
  一 mysql 主主同步
请看mysql主主同步
二 安装keepalived
  



1 wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
2 tar zxvf keepalived-1.2.2.tar.gz
3 cd keepalived-1.2.2
4 ./configure --prefix=/
5 make
6 make install
  
  
  三 keepalived 主备配置文件
  主备置文件不同处有 statenopreempt priority 参考资料里有详细说明作用
192.168.20.37 主配置
vim /etc/keepalived/keepalived.conf
  



! Configuration File for keepalived
global_defs {
router_id 9ai9.net
}
vrrp_script check_run {
script "/root/keepalived_check_mysql.sh"
interval 5
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 88
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 9ai9.net
}
track_script {
check_run
}
virtual_ipaddress {
192.168.20.50
}
}
  192.168.20.38 备用配置文件
vim /etc/keepalived/keepalived.conf
  



! Configuration File for keepalived
global_defs {
router_id 9ai9.net
}
vrrp_script check_run {
script "/root/keepalived_check_mysql.sh"
interval 5
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 88
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 9ai9.net
}
track_script {
check_run
}
virtual_ipaddress {
192.168.20.50
}
}
  四 mysql状态检测脚本/root/bin/keepalived_check_mysql.sh
vim/root/keepalived_check_mysql.sh
  



#!/bin/bash
MYSQL=/usr/local/mysql/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=9ai9.net
CHECK_TIME=3
#mysqlis working MYSQL_OK is 1 , mysql down MYSQL_OK is 0
MYSQL_OK=1
function check_mysql_helth (){
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "show status;" >/dev/null 2>&1
if [ $? = 0 ] ;then
MYSQL_OK=1
else
MYSQL_OK=0
fi
return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check_mysql_helth
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
exit 1
fi
sleep 1
done
  赋予执行权限
  



1 chmod 755 /root/keepalived_check_mysql.sh
  五 防火墙设置
vrrp协议 使用224.0.0.18地址组播




iptables -I RH-Firewall-1-INPUT-d 224.0.0.18 -j ACCEPT
  
  转自 http://yinxiulei.cn
  
页: [1]
查看完整版本: Keepalived+mysql 故障自动切换