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

Posted jinanxiaolaohu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[转帖]MySQL+keepalived用keepalived实现MySQL主主模式的高可用相关的知识,希望对你有一定的参考价值。

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

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

 

一、实验说明

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

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

 

 
技术图片

二、实验环境

操作系统: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二选一

 
技术图片

五、软件安装

在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

        }

    }

}

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

 
技术图片

 

# vim /etc/keepalived/restartKeepalived.sh

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

#!/bin/bash

systemctl restart keepalived

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

 

 
技术图片

# 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

        }

    }

}

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

 

 
技术图片

# vim /etc/keepalived/restartKeepalived.sh

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

#!/bin/bash

systemctl restart keepalived

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

 

 
技术图片

# chmod 700 /etc/keepalived/restartKeepalived.sh

八、验证MySQL的高可用性

在serverA 

# ip addr list 

 

 
技术图片

# systemctl stop mysqld

在serverB

# ip addr list 

 
技术图片

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

两个注意点:

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

 

 
技术图片

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

 

 
技术图片
 
技术图片

 

九、参考

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

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

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

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

以上是关于[转帖]MySQL+keepalived用keepalived实现MySQL主主模式的高可用的主要内容,如果未能解决你的问题,请参考以下文章

mysql 主从配置和集群负载均衡

搭建MySQL高可用负载均衡集群

搭建MySQL高可用负载均衡集群

Keepalive工作原理

nginx+keepalived(双主)

Haproxy+Keepalived(双机热备)搭建高可用web架构