配置keepalive实现mysql双主高可用

1.安装keepalive

 

2。配置keepalive

global_defs {

  notification_email {

    [email protected]

    [email protected]

    [email protected]

   }

   router_idmysql_ha

}

vrrp_script check_mysqld {

        script"/etc/keepalived/mysqlcheck/keepalived_check_mysql.sh "

        interval 2

}

 

vrrp_instance HA_1 {

    state BACKUP

    interface eth0

   virtual_router_id 51

    priority 100

    advert_int 1

    nopreempt

    authentication{

        auth_typePASS

        auth_pass1111

    }

track_script {

        check_mysqld

}

   virtual_ipaddress {

       192.168.32.100/24 dev eth0

    }

}

 

两个keepalive的state都设置为BACKUP

 

检查mysql脚本keepalived_check_mysql.sh

#!/bin/bash

MYSQL=/usr/bin/mysql

MYSQL_HOST=localhost

MYSQL_USER=root

MYSQL_PASSWORD=www.123

CHECK_TIME=3

#mysql  is workingMYSQL_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

 

/etc/init.d/keepalived start

 

测试高可用:关掉DB1的mysql时.他的vip会飘到DB2

 

测试mysql主从同步功能

 

1.在远程客户端通过vip登录测试

 

2.数据复制功能测试

在DB1中创建了一个数据库。在DB2查看是否有这个数据库