Mysql(主主)+keepalived高可用
Mysql 1: 172.16.1.155
Mysql 2: 172.16.1.195
VIP: 172.16.1.222
第一:首先安装好mysql主主 互相能够同步即可!(此步骤太过于简单,省略!)
第二步:主要说下 keepalived+ 里面需要编写的脚本keepalived_check_mysql.sh
首先安装keepavlived:
包名:keepalived-1.2.7.tar.gz
安装这个之前,有个细节注意/usr/src/kernels/2.6.18-238.el5-i686/这个下看有东西没!
如果没有:执行yum install kernel-devel -y,有则继续!
ln -s /usr/src/kernels/2.6.18-238.el5-i686/ /usr/src/linux
1、tar -zxvf keepalived-1.2.7.tar.gz
cd keepalived-1.1.17
./configure --prefix=/usr/local/keepalived
make
make install
安装后,把人家提供好的文件进行对应的复制!
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir -pv /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
2、在Mysql 1上配置keepalived.conf
[root@asdasda liang]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id [email protected]
}
vrrp_script check_run {
script "/liang/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 liang
}
track_script {
check_run
}
virtual_ipaddress {
172.16.1.222
}
}
3、在Mysql 1上配置keepalived.conf (只是priority 80 这行不一样,别的都相同!)
[root@asdasda liang]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id [email protected]
}
vrrp_script check_run {
script "/liang/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 80
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass liang
}
track_script {
check_run
}
virtual_ipaddress {
172.16.1.222
}
}
第三步:以上完成了keepalived.conf的配置,但是里面有个脚本/liang/keepalived_check_mysql.sh 为编写,下面写这个脚本!--------两个主机放同样的脚本即可(机器195和155都放这个脚本在同样的目录下即可!)
Vim keepalived_check_mysql.sh 放到对应的目录下即可:/liang/
内容如下:
#!/bin/bash
MYSQL=/usr/local/mysql/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=123456
CHECK_TIME=3
#mysql is 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
echo "hel"
if [ $MYSQL_OK = 1 ] ; then
CHECK_TIME=0
echo "time set 0"
exit 0
fi
if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ]
then
/etc/init.d/keepalived stop
echo "keepalived already stop"
exit 1
fi
sleep 1
Done
以上需要注意登录mysql中的目录和主机、用户、密码等信息的修改即可使用!------(脚本目的是检测mysql服务如果没起来就停掉这个机器上的keepalived服务!)
第四步:完成了上面的这步以后,分别启动两台机器的keepalived
172.16.1.195:
上执行:
Service keepalived start
172.16.1.155:
上执行
Service keepalived start
若不行就重启下两台机器的mysql数据库即可!
----用tail -f /var/log/messages 即可查看那个为正在使用的数据库。
----用 ip add sh 即可查看绑定的VIP的信息。