机器一配置

global_defs {
     notification_email {
         fdafsaf@qq.com
     }
     notification_email_from aaa@pucipuci.cn
     smtp_server 127.0.0.1
     smtp_connect_timeout 30
     router_id MySQL-ha
}

vrrp_script chk_mysql_port {
        script "/check/check_mysql.sh"
        interval 1
}

vrrp_instance VI_1 {
     state MASTER
     interface em2#注意网卡名称
     virtual_router_id 51
     priority 100   #优先级,另一台改为90
     advert_int 1
     nopreempt  #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置
     authentication {
         auth_type PASS
         auth_pass 1111
     }
     virtual_ipaddress {
         192.168.10.200
     }
     track_script {
         chk_mysql_port
     }

}

iptalbes开启 -A INPUT -d 224.0.0.18 -j ACCEPT

*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8808 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 15389 -j ACCEPT
-A INPUT -d 224.0.0.18 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

机器二配置

global_defs {
     notification_email {
         fdafsaf@qq.com
     }
     notification_email_from aaa@pucipuci.cn
     smtp_server 127.0.0.1
     smtp_connect_timeout 30
     router_id MySQL-ha2
}

vrrp_script chk_mysql_port {
        script "/check/check_mysql.sh"
        interval 1
}
vrrp_instance VI_1 {
     state BACKUP   #两台配置此处均是BACKUP
     interface em2#注意网卡名称
     virtual_router_id 51
     priority 90   #优先级
     advert_int 1
     #nopreempt  #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置
     authentication {
         auth_type PASS
         auth_pass 1111
     }
     virtual_ipaddress {
         192.168.10.200
     }
     track_script {
         chk_mysql_port
     }
}

iptalbes开启 -A INPUT -d 224.0.0.18 -j ACCEPT

*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8808 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 15389 -j ACCEPT
-A INPUT -d 224.0.0.18 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

mysql检测脚本,如果数据库挂,则停止接管VIP

MYSQL_PING=`/usr/local/mysql/bin/mysqladmin -upiaoyi_user -ppiaoyi_67saYY ping`
MYSQL_OK="mysqld is alive"
if [[ "$MYSQL_PING" != "$MYSQL_OK" ]]
    then
                echo `date '+%Y-%m-%d %H:%M:%S'` "mysql not ok \r\n" >> /check/mysql_check.log
        #service mysqld restart
                #ip change
                echo "down"
                pkill keepalived
    else
                echo "yes"
fi