[转帖]【MySQL+keepalived】用keepalived实现MySQL主主模式的高可用

tangjianft 2019-11-01

【MySQL+keepalived】用keepalived实现MySQL主主模式的高可用

https://www.jianshu.com/p/8694d07595bc

一、实验说明

MySQL主主模式,是两台MySQL数据库互为主从。

此实验是用keepalived实现MySQL主主模式的高可用,基于已经安装好了主主架构的MySQL,然后配置keepalived,验证高可用性!

 
[转帖]【MySQL+keepalived】用keepalived实现MySQL主主模式的高可用

二、实验环境

操作系统:CentOS 7.5 

serverA:192.168.1.104

serverB: 192.168.1.105

VIP: 192.168.1.110

三、关闭selinux

# setenforce 0

# sed -i ‘s/^SELINUX=.*/SELINUX=permissive/g‘  /etc/selinux/config

四、防火墙设置

firewalld和iptables二选一

 
[转帖]【MySQL+keepalived】用keepalived实现MySQL主主模式的高可用

五、软件安装

在serverA和serverB

#  yum  -y install epel-release

# yum -y install keepalived

六、在serverA配置keepalived

# vim  /etc/keepalived/keepalived.conf

###########################################

! Configuration File for keepalived

global_defs {

  router_id LVS_DEVEL

}

vrrp_instance VI_1 {

    state BACKUP

    interface ens33

    virtual_router_id 150

    priority 100

    advert_int 1

    nopreempt

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.1.110/24

    }

}

virtual_server 192.168.1.110 3306 {

    delay_loop 2

    lb_algo rr

    lb_kind DR

    persistence_timeout 50

    protocol TCP

    real_server 192.168.1.104 3306 {

        weight 3

        notify_down /etc/keepalived/restartKeepalived.sh

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

}

###################################################

 
[转帖]【MySQL+keepalived】用keepalived实现MySQL主主模式的高可用

# vim /etc/keepalived/restartKeepalived.sh

#################################

#!/bin/bash

systemctl restart keepalived

##################################

 
[转帖]【MySQL+keepalived】用keepalived实现MySQL主主模式的高可用

# chmod 700 /etc/keepalived/restartKeepalived.sh

七、在serverB配置keepalived

在serveB

# vim  /etc/keepalived/keepalived.conf

###########################################

! Configuration File for keepalived

global_defs {

  router_id LVS_DEVEL

}

vrrp_instance VI_1 {

    state BACKUP

    interface ens33

    virtual_router_id 150

    priority 90

    advert_int 1

    nopreempt

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.1.110/24

    }

}

virtual_server 192.168.1.110 3306 {

    delay_loop 2

    lb_algo rr

    lb_kind DR

    persistence_timeout 50

    protocol TCP

real_server 192.168.1.105 3306 {

        weight 3

 notify_down /etc/keepalived/restartKeepalived.sh

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

}

###################################################

 
[转帖]【MySQL+keepalived】用keepalived实现MySQL主主模式的高可用

# vim /etc/keepalived/restartKeepalived.sh

#################################

#!/bin/bash

systemctl restart keepalived

##################################

 
[转帖]【MySQL+keepalived】用keepalived实现MySQL主主模式的高可用

# chmod 700 /etc/keepalived/restartKeepalived.sh

八、验证MySQL的高可用性

在serverA 

# ip addr list 

 
[转帖]【MySQL+keepalived】用keepalived实现MySQL主主模式的高可用

# systemctl stop mysqld

在serverB

# ip addr list 

 
[转帖]【MySQL+keepalived】用keepalived实现MySQL主主模式的高可用

从上可以看出,关闭serverA上的MySQL,vip成功漂移到了severB,数据库的连接不受影响,实现了一定程度的高可用。

两个注意点:

1. 具体通过vip连接到serverA或者severB 时,服务器上的vip才监听3306端口,无连接时,不监听

 
[转帖]【MySQL+keepalived】用keepalived实现MySQL主主模式的高可用

2. vip在serverA时,在severB上无法通过vip连接上数据库,反之亦然

 
[转帖]【MySQL+keepalived】用keepalived实现MySQL主主模式的高可用
 
[转帖]【MySQL+keepalived】用keepalived实现MySQL主主模式的高可用

九、参考

keepalived + mysql 实现单向备份、故障转移

https://www.jianshu.com/p/35030921dab5

Centos7+Keepalived实现Mariadb(MYSQL)的高可用(HA)

http://blog.51cto.com/gaowenlong/1888036

相关推荐