Keepalived1.4.0应用在MySQL5.7.19上实现主备高可用

本文主要给大家介绍Keepalived1.4.0应用在MySQL 5.7.19上实现主备高可用,文章内容都是笔者用心摘选和编辑的,具有一定的针对性,对大家的参考意义还是比较大的,下面跟笔者一起了解下Keepalived1.4.0应用在MySQL 5.7.19上实现主备高可用吧。

创新互联坚实的技术研发基础赢得了行业内的良好口碑,公司成立十余年来,为上千多家企业提供过网站建设、软件开发、搜索引擎优化技术、互联网大数据整合营销服务,多年的技术服务成功经验、众多的客户使我们能懂得更多,做得更好。"让您的网站跑起来"是我们一直追求的目标!

1、基本环境

JDK 1.8_171
MySQL 5.7.19
CentOS 7.4
Keepalived 1.4.0
DB1:192.168.200.180
DB2:192.168.200.181
VIP: 192.168.200.99
Keepalived1.4.0应用在MySQL 5.7.19上实现主备高可用

2、下载安装Keepalived

2.1、keepalived下载
下载地址:wget http://www.keepalived.org/software/keepalived-1.4.0.tar.gz 
2.2、keepalived安装

2台均按照此种方式进行安装

yum install gcc gcc-c++ make openssl openssl-devel net-snmp-devel psmisc ipvsadm libnfnetlink-devel popt popt-devel popt-static openssl-devel kernel-devel libnl libnl-devel -y    # 安装依赖

cd /usr/local/src
tar -zvxf keepalived-1.4.0.tar.gz    # 解压
cd keepalived-1.4.0
./configure --prefix=/usr/local/keepalived --enable-snmp    # 编译
make && make install    # 安装
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/    # 拷贝配置
ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
ln -s /usr/local/keepalived/sbin/keepalived /sbin/keepalived
2.3、Keepalived配置

DB1上面的配置:

[root@mysql01 keepalived-1.4.0]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak
[root@mysql01 keepalived-1.4.0]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     nvidiacheng@163.com
   }
   notification_email_from nvidiacheng@163.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id Node_Master
}

vrrp_instance VI_1 {
    state BACKUP          # 可配置master和backup模式,为了防止脑裂现象,主备均需要设置为backup模式,master模式会抢占VIP
    interface ens33      # 网卡名
    virtual_router_id 43     # VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组
    priority 100      # 权重,主节点要大于备节点
    unicast_src_ip  192.168.200.180     # 本地IP地址
    unicast_peer {
                  192.168.200.181     # 对端IP地址,此地址一定不能忘记
                 }
    nopreempt            # 配合backup,防止切换后,主库服务恢复正常后,IP漂移过来
    advert_int 1          # 组播信息发送间隔,两个节点设置必须一样
    authentication {     # 设置验证信息,两个节点必须一致
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {   
        192.168.200.99      # VIP地址
    }
}
virtual_server 192.168.200.99 3306 {
    delay_loop 6
    lb_algo wrr          
    lb_kind DR           
    persistence_timeout 50
    protocol TCP

    real_server 192.168.200.180 3306 {
      weight 1
      notify_down /etc/keepalived/mysql.sh 
      TCP_CHECK {
      connect_timeout 10
      nb_get_retry 3 
      delay_before_retry 3
      connect_port 3306 
        }
    }
}

 
DB2上面配置

[root@mysql02 keepalived-1.4.0]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak
[root@mysql02 keepalived-1.4.0]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived
  global_defs {
      notification_email {
      nvidiacheng@163.com
      }
      notification_email_from nvidiacheng@163.com
      smtp_server 127.0.0.1
      smtp_connect_timeout 30
      router_id Node_backup
     }
 vrrp_instance VI_1 {
      state BACKUP
      interface ens33
      virtual_router_id 43
      priority 90                  
      unicast_src_ip  192.168.200.181
      unicast_peer {
                  192.168.200.180
                   }
      advert_int 1
      authentication {
      auth_type PASS
      auth_pass 1111
      }
      virtual_ipaddress {
      192.168.200.99
      }
      }
 virtual_server 192.168.200.99 3306 {
      delay_loop 2                 
      lb_algo wrr                  
      lb_kind DR                  
      persistence_timeout 60 
      protocol TCP 

      real_server 192.168.200.181 3306{
      weight 1
      notify_down /etc/keepalived/mysql.sh
      echo '3' >  /etc/keepalived/t.log
      TCP_CHECK {
      connect_timeout 10
      nb_get_retry 3     
      delay_before_retry 3
      connect_port 3306 
      }
      }
}

mysql.sh脚本配置内容(2台一样):

[root@mysql01 keepalived]# vim /etc/keepalived/mysql.sh
#!/bin/bash
run_status=`service keepalived status|grep running|wc -l`
pro_status=`ps -ef |grep keepalived|grep -v grep |wc -l`

service keepalived stop

if [ ${run_status} != 0 ] || [ ${pro_status} != 0 ]
then
   pkill keepalived
fi

给mysql.sh赋可执行权限

[root@mysql01 keepalived]# chmod +x mysql.sh

都配置好后启动keepalived进行测试:

service keepalived start   # 启动Keepalived
service keepalived restart   # 重启Keepalived
service keepalived stop   # 停止Keepalived
service keepalived status   # 查看Keepalived运行状态
ps aux | grep keepalived   # 查看Keepalived进程

Keepalived1.4.0应用在MySQL 5.7.19上实现主备高可用

3.3、测试Keepalived

2台均启动Keepalived后,分别查看网卡
Keepalived1.4.0应用在MySQL 5.7.19上实现主备高可用
Keepalived1.4.0应用在MySQL 5.7.19上实现主备高可用
从上图可以看出,VIP地址在主节点
 
使用Navicat工具分别连接2个节点和VIP节点
Keepalived1.4.0应用在MySQL 5.7.19上实现主备高可用
这个时候我们将节点1的MySQL停止,模拟主节点数据库故障来测试VIP是否会漂移到备节点
Keepalived1.4.0应用在MySQL 5.7.19上实现主备高可用
如上图,主节点MySQL停止后,Keepalived进程也结束了,VIP地址不见了
 
查看备节点的网卡,发现VIP已经漂移过来了
Keepalived1.4.0应用在MySQL 5.7.19上实现主备高可用
 
用Navicat打开VIP,可以正常访问,主节点无法打开
Keepalived1.4.0应用在MySQL 5.7.19上实现主备高可用
重新启动主节点MySQL服务和Keepalived服务,在停止备节点测试(略过)

看完以上关于Keepalived1.4.0应用在MySQL 5.7.19上实现主备高可用,很多读者朋友肯定多少有一定的了解,如需获取更多的行业知识信息 ,可以持续关注我们的行业资讯栏目的。


当前题目:Keepalived1.4.0应用在MySQL5.7.19上实现主备高可用
本文地址:http://azwzsj.com/article/jsijie.html