通过keepalived实现 MySQL VIP 自动切换

首先配置keepalived。链接如下:
http://blog.itpub.net/28939273/viewspace-1808369/

主服务器keepalived的配置文件内容如下:

[root@yoon keepalived]# cat keepalived.conf 
vrrp_script check_mysql_port {
    script "/etc/keepalived/keepalived_check_mysql.sh"    
    interval 1
    weight -10
}


global_defs {
    router_id MYSQL_YOON
}


vrrp_instance mysql_yoon_master {
    state MASTER
    interface eth0
    virtual_router_id 245
    priority 100
    advert_int 1


    authentication {
        auth_type PASS
        auth_pass mysql_yoon
    }


    virtual_ipaddress {
        172.16.232.245
    }


    track_script {
        check_mysql_port
    }
}

备用服务器keepalived配置文件内容:
[root@back keepalived]# cat keepalived.conf 
vrrp_script check_mysql_port {
    script "/etc/keepalived/keepalived_check_mysql.sh"
    interval 1
    weight -10
}


global_defs {
    router_id MYSQL_YOON
}


vrrp_instance mysql_yoon_master {
    state MASTER
    interface eth0
    virtual_router_id 245
    priority 90
    advert_int 1


    authentication {
        auth_type PASS
        auth_pass mysql_yoon
    }


    virtual_ipaddress {
        172.16.232.245
    }


    track_script {
        check_mysql_port
    }
}


在/etc/keepalived/下创建keepalived_check_mysql.sh

[root@yoon keepalived]# vi keepalived_check_mysql.sh 
#!/bin/bash
MYSQL=/export/servers/mysql/bin/mysql
MYSQL_HOST=127.0.0.1       #如何在执行过程中,keepalived进程莫名自动关闭的话,尝试将127.0.0.1改成localhost,我是由localhsot改成了127.0.0.1才可以,一定要注意
MYSQL_USER=root
MYSQL_PASSWORD=yoon
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
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

并在备用服务器上也创建同样的脚本

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28939273/viewspace-1810965/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28939273/viewspace-1810965/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值