mysql集群2-双主故障转移keepalived

Mysql双主故障转移

架构图

mysql集群2-双主故障转移keepalived_第1张图片

说明:

Mysql1(.3)已存在使用中数据库实例已经开始binlog

Mysql2(.4)后添加.3冷备还原

 

目的:

实现异常宕机或mysql服务异常中断时正常提供对外服务

缺点:正常情况下只有一台服务器提供服务,当出现故障时候才会转移

不可用情况:服务器hang

环境:

264centos6.3虚拟机部署mysql5.6.19 serverrpm安装包

 

实施步骤:

  1. 配置双主备份

  1. 首先配置主从见文档mysql读写分离

  2. 授权mysql1访问mysql2的权限

    Mysql2

    grant  all privileges on *.* to 'backup'@'192.168.137.3' identified by 'backup'; flush privileges;

    show master status;--记下fileposition

    mysql1

    change master to master_host='192.168.137.4',master_user='backup',master_password='backup',master_log_file='mysql-bin.000002',master_log_pos=120;

    start slave;

    show slave status \G

    mysql集群2-双主故障转移keepalived_第2张图片

  1. 安装编译keeepalived配置故障转移

     

  1. 下载http://www.keepalived.org/download.html

    mysql集群2-双主故障转移keepalived_第3张图片

  2. 解压tar -zxvf keepalived-1.2.13.tar.gz

  3. 安装依赖软件包

yum -y install gcc gcc-c++ gcc-g77 ncurses-devel bisonlibaio-devel cmake libnl* libpopt* openssl-devel

  1. cd keepalived-1.2.13

    ./configure--prefix=/usr/local/keepalived

    Make

    Make install

---同上分别在两台服务器上面安装

  1. 配置keepalived,两台服务器

    Mysql2

  vi/usr/local/keepalived/etc/keepalived/keepalived.conf

  !Configuration File for keepalived

 

global_defs {

  router_id mysql-ha

}

 

vrrp_instance VI_1 {

   state BACKUP

   interface eth1

   virtual_router_id 201

   priority 90 #slave

   advert_int 1

 

   authentication {

       auth_type PASS

       auth_pass mysql

   }

   virtual_ipaddress {

       192.168.137.2

   }

}

 

virtual_server 192.168.137.2 3306 {

   delay_loop 2

   lb_algo rr

   lb_kind DR

   persistence_timeout 60

   protocol TCP

   real_server 192.168.137.4 3306 {

       weight 1

       notify_down /usr/local/keepalived/etc/keepalived/mysql.sh

       TCP_CHECK {

           connect_port 3306

           connect_timeout 3

           nb_get_retry 2

           delay_before_retry 1

       }

   }

}

Mysql1

修改interface eth1  interface eth0

    prority 100

       real_server 192.168.137.1

Mysql1修改完后如下

! Configuration File for keepalived

 

global_defs {

  router_id mysql-ha

}

 

vrrp_instance VI_1 {

   state BACKUP

   interface eth0

   virtual_router_id 201

   priority 100 #master

   advert_int 1

   nopreempt #

   authentication {

       auth_type PASS

       auth_pass mysql

   }

   virtual_ipaddress {

       192.168.137.2

   }

}

 

virtual_server 192.168.137.2 3306 {

   delay_loop 2

   lb_algo rr

   lb_kind DR

   persistence_timeout 60

   protocol TCP

   real_server 192.168.137.3 3306 {

       weight 1

       notify_down /usr/local/keepalived/etc/keepalived/mysql.sh

       TCP_CHECK {

           connect_port 3306

           connect_timeout 3

           nb_get_retry 2

           delay_before_retry 1

       }

   }

}

  1. 配置停止脚本(两台都需要)

    Mysql.sh


#!/bin/bash
pkill keepalived

 

  1. 两台主机上面启动keepalived

    /usr/local/keepalived/sbin/keepalived -f/usr/local/keepalived/etc/keepalived/keepalived.conf -D

   mysql集群2-双主故障转移keepalived_第4张图片

到这里keepalived已经配置完成

  1. 测试故障转移

Start.sh

mysql集群2-双主故障转移keepalived_第5张图片

  1. Mysql1服务停止

    mysql集群2-双主故障转移keepalived_第6张图片

     

可以看到mysql1服务停止后可以正常使用

 

  1. 启动mysql1停止mysql2服务

    启动myslq1

mysql集群2-双主故障转移keepalived_第7张图片

 

 停止mysql2

mysql集群2-双主故障转移keepalived_第8张图片

 

可以看到刚才mysql服务是在mysql2上面查询时报错然后再次查询就正常了没有一次已经实现故障转移

 

  1. Myslq1服务器异常中断

    mysql集群2-双主故障转移keepalived_第9张图片

    mysql集群2-双主故障转移keepalived_第10张图片

    可以看到同上

     

  2. 网络故障

    mysql集群2-双主故障转移keepalived_第11张图片

     

    服务在mysql2上面跑停止mysql1时没有影响,当停止mysql2的时候会发出错误然后再次执行正常已经转移到服务已经转移到mysql1上面,现在启动mysql2,我们已经确定服务是在mysql1上面了

  1. 停止mysql2网卡模拟网络故障

    mysql集群2-双主故障转移keepalived_第12张图片

    可以看到mysql2网卡故障没有任何影响

  2. 启动mysql2网卡,停掉mysql1

    mysql集群2-双主故障转移keepalived_第13张图片

    已经自动转移到了mysql2

     

    停止正在服务的mysql1后再mysql2上面插入数据再启动mysql1

    mysql集群2-双主故障转移keepalived_第14张图片

    可以看到数据正常同步

     

    当正在使用的服务器断网时候会出现无法使用,需要重启另外一台服务器的mysql服务即可正常

    mysql集群2-双主故障转移keepalived_第15张图片

    mysql集群2-双主故障转移keepalived_第16张图片

     

    在写入数据时候突然断网的时候不是很稳定有时候断一下连接正常,有时候需要重启服务





你可能感兴趣的:(mysql集群2-双主故障转移keepalived)