安装keepalive
tar zxf keepalived-1.2.2.tar.gz
cd keepalived-1.2.2
./configure --sysconf=/etc --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-x86_64
sudo make
sudo make install
sudo cp /usr/local/sbin/keepalived /sbin/keepalived
service keepalived start
service keepalived stop
make时报错如下:
解决方法:
进入源码目录
cd keepalived-1.2.2
vi keepalived/libipvs-2.6/ip_vs.h
找到下面一行:
#include <linux/types.h> /* For __beXX types in userland */
把他放到如下一行的下面:
#include <sys/types.h>
接下来顺利make了。
-----------编辑keepalived资源配置文件-----------
vim/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id MYSQL-ha
}
vrrp_script check_running {
script"/etc/keepalived/keepalived_check_mysql.sh"
interval 5
}
vrrp_instance PWRD_1 {
state BACKUP
interface bond0
virtual_router_id 88
priority 150 #[另一台是100]
advert_int 1
nopreempt #[另一台不加]
authentication{
auth_type PASS
auth_pass 99999
}
virtual_ipaddress {
10.142.132.49
}
track_script {
check_running
}
notify_master/etc/keepalived/master.sh
notify_backup/etc/keepalived/slave.sh
notify_fault/etc/keepalived/notify_fault.sh
notify_stop/etc/keepalived/down.sh
}
vikeepalived_check_mysql.sh
#!/bin/bash
MYSQL=/app/mysql/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=mysql
CHECK_COUNTS=3
#mysql is working MYSQL_OK is 0 , mysql isnot working MYSQL_OK is 1
MYSQL_OK=0
function check_Mysql_Runing (){
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD}-e "show grants;" >/dev/null 2>&1
if [ $? = 0 ] ;then
MYSQL_OK=0
else
MYSQL_OK=1
fi
return $MYSQL_OK
}
while [ $CHECK_COUNTS -ne 0 ]
do
let"CHECK_COUNTS -= 1"
check_Mysql_Runing
if [ $MYSQL_OK= 0 ] ; then
echo "mysql is runing!"
exit 0
fi
if [ $MYSQL_OK-eq 1 ] && [ $CHECK_COUNTS -eq 0 ]
then
echo "mysql is not runing!"
exit 1
fi
sleep 1
done
vim/etc/keepalived/master.sh
#!/bin/bash
echo "#`date +"%Y-%m-%d%H:%M"` change master status!!! " >> /etc/keepalived/change.log
vim/etc/keepalived/slave.sh
#!/bin/bash
echo "#`date +"%Y-%m-%d%H:%M"` change slave status!!!" >> /etc/keepalived/change.log
vim/etc/keepalived/notify_fault.sh
#!/bin/bash
echo "#`date +"%Y-%m-%d%H:%M"` change notify_fault status!!!" >>/etc/keepalived/change.log
sleep 3
echo "#`date +"%Y-%m-%d%H:%M"` try startup mysql!!!" >> /etc/keepalived/change.log
service mysql start
vim/etc/keepalived/down.sh
#!/bin/bash
echo "#`date +"%Y-%m-%d%H:%M"` keepalived is down!!!" >> /etc/keepalived/change.log
chmod +x keepalived_check_mysql.sh
chmod +x /etc/keepalived/master.sh
chmod +x /etc/keepalived/slave.sh
chmod +x /etc/keepalived/notify_fault.sh
chmod +x /etc/keepalived/down.sh
mysql -h10.142.132.49 -ulipengfei-plipengfei
测试
SHOW VARIABLES LIKE ‘server_id‘;【查看当前是哪个mysql主机】