htgefwda 发表于 2016-1-15 08:47:28

keepalived+mysql双主

***********************地址分配***********************
ip1:192.168.31.64
ip2:192.168.31.65
vip:192.168.31.16
双主搭建过程略

**********************keepalived安装及配置文件*********
安装:
yum install keepalived

keepalived配置文件(ip1和ip2一致):
/etc/keepalived/keepalived.conf:
vrrp_script check_run {
    script "/etc/keepalived/check_mysql.sh"
    interval 20
}


vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    advert_int 1
    nopreempt
    authentication {
      auth_type PASS
      auth_pass 1111
    }
    track_script {
      check_run
    }
    virtual_ipaddress {
      192.168.31.16
    }
}

mysql检测脚本
/etc/keepalived/check_mysql.sh:
#!/bin/bash
MYSQL=/usr/local/mysql/bin/mysql
MYSQL_HOST=127.0.0.1
MYSQL_PORT=3306
MYSQL_USER=keepalived
MYSQL_PASSWD=keepalived
CHECK_TIME=3
MYSQL_OK=1

function check_mysql_health(){
    $MYSQL -h $MYSQL_HOST -P $MYSQL_PORT -u $MYSQL_USER -p${MYSQL_PASSWD} -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_health
    if [ $MYSQL_OK = 1 ]
    then   
      CHECK_TIME=0   
      exit 0
    fi
    if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ]
    then
      pkill keepalived
      exit 1
    fi
    sleep 1
done
**********************keepalived启动及验证*********
启动
ip1和ip2依次执行:
service keepalived start

判断vip绑定到哪个实际ip,如下指向的是64:
# tcpdump vrrp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
14:34:55.477747 IP 192.168.31.64 > 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
14:34:56.479502 IP 192.168.31.64 > 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20

页: [1]
查看完整版本: keepalived+mysql双主