基于mysql主主互备实现keepalived高可用 方案

文章目录

  • keepalived安装与配置
      • 安装包获取:https://www.keepalived.org
    • 二进制安装:
      • 配置规范启动
      • 修改配置文件
    • 验证
        • 两端都开启keepalived ---> systemctl start keepalived
        • mysql01端停止keepalived
      • 注意
        • 此时重启mysql01端keepalived服务
        • 此时我们将mysql02端服务停掉,ip漂移到mysql01
        • 用master主机测试
      • 错误:此时发现copy用户登录上去只有一个库,不能创建,也不能 查看其他内容。

keepalived安装与配置

安装包获取:https://www.keepalived.org

二进制安装:

# 同步时钟源
# yum -y install ntp ntpdate
# ntpdate cn.pool.ntp.org
# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
#获取安装包
[root@mysql01 ~]# ls
keepalived-2.0.20.tar.gz
[root@mysql01 ~]# tar xzf keepalived-2.0.20.tar.gz -C /usr/local/src/
# 配置编译环境
# yum install openssl-devel gcc gcc-c++ make
# 创建安装目录
# mkdir -p /data/keepalived
# cd /usr/local/src/keepalived-2.0.20/
# ./configure --prefix=/data/keepalived
# echo $?
0
# make
# echo $?
0
# make install
# echo $?
0

配置规范启动

# cd /usr/local/src/keepalived-2.0.20/keepalived/etc/
# cp -R init /data/keepalived/
# cp -R init /data/keepalived/etc/
# cp -R init.d /data/keepalived/etc/
# ll /data/keepalived/etc/
总用量 0
drwxr-xr-x. 2 root root  86 7月  31 21:58 init
drwxr-xr-x. 2 root root 135 7月  31 21:58 init.d
drwxr-xr-x. 3 root root  44 7月  31 18:01 keepalived
drwxr-xr-x. 2 root root  24 7月  31 17:59 sysconfig

修改配置文件

cp -p /etc/keepalived/keepalived.conf{,.bak}
#mysql01端
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id HA_MYSQL01
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_script check_mysqld {
    script "/etc/keepalived/check_mysqld.sh"
    interval 2
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.10.201
    }

    track_script {
        check_mysqld
    }
}

# mysql02端
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id HA_MYSQL01
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script check_mysqld {
    script "/etc/keepalived/check_mysqld.sh"
    interval 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.10.201
    }

    track_script {
        check_mysqld
    }
}

验证

两端都开启keepalived —> systemctl start keepalived

基于mysql主主互备实现keepalived高可用 方案_第1张图片
基于mysql主主互备实现keepalived高可用 方案_第2张图片

mysql01端停止keepalived

基于mysql主主互备实现keepalived高可用 方案_第3张图片
基于mysql主主互备实现keepalived高可用 方案_第4张图片

注意

mysql01端恢复后ip又漂移到mysql01端上
故虽然此时能够完成故障的切换,但数据库的应用场景上不建议频繁的切换,因此将其配置为vip不抢占模式,具体配置如下
只需修改mysql01端的配置文件如下:

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id HA_MYSQL01
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script check_mysqld {
    script "/etc/keepalived/check_mysqld.sh"
    interval 2
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 50
    advert_int 1
    nopreempt   #添加此选项,不抢占模式
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.10.201
    }

    track_script {
        check_mysqld
    }
}

此时重启mysql01端keepalived服务

相当于故障了,并随之恢复;此时ip漂移到mysql02端且不会漂移回mysql01
基于mysql主主互备实现keepalived高可用 方案_第5张图片
基于mysql主主互备实现keepalived高可用 方案_第6张图片

此时我们将mysql02端服务停掉,ip漂移到mysql01

基于mysql主主互备实现keepalived高可用 方案_第7张图片
基于mysql主主互备实现keepalived高可用 方案_第8张图片

用master主机测试

基于mysql主主互备实现keepalived高可用 方案_第9张图片
基于mysql主主互备实现keepalived高可用 方案_第10张图片

错误:此时发现copy用户登录上去只有一个库,不能创建,也不能 查看其他内容。

经过查询得知,按照之前主主互备配置备份的时候,copy的权限只有备份权限:
基于mysql主主互备实现keepalived高可用 方案_第11张图片
只需要修改权限(用root登录),刷新即可;
基于mysql主主互备实现keepalived高可用 方案_第12张图片
此时用第三台登录mysql(copy用户)
基于mysql主主互备实现keepalived高可用 方案_第13张图片
mysql服务器上登录查看
基于mysql主主互备实现keepalived高可用 方案_第14张图片

------------------------------------------------------------------------------------------------------- 返回目录

你可能感兴趣的:(linux,服务,mysql)