码迷,mamicode.com
首页 > 数据库 > 详细

【MySQL进阶】Keepalived1.4.0结合MySQL 5.7.19实现主备高可用

时间:2018-10-27 19:44:11      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:log   本地ip   pki   mkdir   stop   本地   bak   amp   src   

1、基本环境

数据库安装及主备同步接上一篇文章:http://blog.51cto.com/13946719/2309514
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
技术分享图片

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进程

技术分享图片

3.3、测试Keepalived

2台均启动Keepalived后,分别查看网卡
技术分享图片
技术分享图片
从上图可以看出,VIP地址在主节点
 
使用Navicat工具分别连接2个节点和VIP节点
技术分享图片
这个时候我们将节点1的MySQL停止,模拟主节点数据库故障来测试VIP是否会漂移到备节点
技术分享图片
如上图,主节点MySQL停止后,Keepalived进程也结束了,VIP地址不见了
 
查看备节点的网卡,发现VIP已经漂移过来了
技术分享图片
 
用Navicat打开VIP,可以正常访问,主节点无法打开
技术分享图片
重新启动主节点MySQL服务和Keepalived服务,在停止备节点测试(略过)
 
到此,结合前2篇文章,MySQL 5.7.19在CentOS 7.4操作系统上的安装,同步配置,高可用配置已经全部完成!

【MySQL进阶】Keepalived1.4.0结合MySQL 5.7.19实现主备高可用

标签:log   本地ip   pki   mkdir   stop   本地   bak   amp   src   

原文地址:http://blog.51cto.com/13946719/2309610

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!