mysql双主+keepalived实现高可用集群

1、安装并配置keepalived

master-a端:

1)安装keepalived
[root@CentOS ~]#
[root@CentOS ~]# yum install keepalived 安装keepalived
[root@CentOS ~]#
[root@CentOS ~]# rpm -qa keepalived
keepalived-1.3.5-16.el7.x86_64
[root@CentOS ~]#

2)修改 keepalived配置文件
[root@CentOS ~]#
[root@CentOS ~]# > /etc/keepalived/keepalived.conf #清空原来的配置文件
[root@CentOS ~]#
[root@CentOS ~]# vim /etc/keepalived/keepalived.conf #自定义配置文件
! Configuration File for keepalived

global_defs {
notification_email {br/>111111@qq.com
}
smtp_server 192.168.23.1
smtp_connect_timeout 30
router_id MySQL-HA
}

vrrp_script chk_mysql {
script "/etc/keepalived/chk_mysql.sh"
interval 2
weight -5
fall 3
}

vrrp_instance VI_1 {
state BACKUP
interface ens32
mcast_src_ip 192.168.18.103
virtual_router_id 55
priority 100
advert_int 1
nopreempt

authentication {
auth_type PASS
auth_pass 123456
}

virtual_ipaddress {
192.168.18.110/24
}

track_script {
chk_mysql
}
}

[root@CentOS ~]#
[root@CentOS ~]# cat /etc/keepalived/keepalived.conf #查看配置
mysql双主+keepalived实现高可用集群

3)编写mysql状态检测脚本
[root@CentOS ~]#
[root@CentOS ~]# vim /etc/keepalived/chk_mysql.sh

#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
systemctl stop keepalived
fi

[root@CentOS ~]#
[root@CentOS ~]# cat /etc/keepalived/chk_mysql.sh #查看脚本内容
mysql双主+keepalived实现高可用集群
[root@CentOS ~]#
[root@CentOS ~]# chmod +x /etc/keepalived/chk_mysql.sh #赋予脚本可执行权限

4)启动keepalived
[root@CentOS ~]#
[root@CentOS ~]# systemctl start keepalived
[root@CentOS ~]# systemctl enable keepalived

master-b端:
1)安装keepalived
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# yum install keepalived

2)修改keepalived配置文件
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# > /etc/keepalived/keepalived.conf #清空原配置
[root@CentOS-2 ~]# vim /etc/keepalived/keepalived.conf #自定义配置文件

! Configuration File for keepalived

global_defs {
notification_email {br/>111111@qq.com
}
smtp_server 192.168.23.1
smtp_connect_timeout 30
router_id MySQL-HA
}

vrrp_script chk_mysql {
script "/etc/keepalived/chk_mysql.sh"
interval 2
weight -5
fall 3
}

vrrp_instance VI_1 {
state BACKUP
interface ens32
mcast_src_ip 192.168.18.104
virtual_router_id 55
priority 95
advert_int 1

authentication {
auth_type PASS
auth_pass 123456
}

virtual_ipaddress {
192.168.18.110/24
}

track_script {
chk_mysql
}
}

[root@CentOS-2 ~]#
[root@CentOS-2 ~]# cat /etc/keepalived/keepalived.conf
mysql双主+keepalived实现高可用集群

3)编写mysql状态检测脚本
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# vim /etc/keepalived/chk_mysql.sh

#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
systemctl stop keepalived
fi

[root@CentOS-2 ~]#
[root@CentOS-2 ~]# cat /etc/keepalived/chk_mysql.sh
mysql双主+keepalived实现高可用集群
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# chmod +x /etc/keepalived/chk_mysql.sh #赋予脚本可执行权限

4)启动keepalived
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# systemctl start keepalived
[root@CentOS-2 ~]# systemctl enable keepalived

2、测试keepalived+MySQL双主可用性

1)查看mysql状态

master-a端:
[root@CentOS ~]#
[root@CentOS ~]# systemctl status mysqld #查看mysql是否在运行
mysql双主+keepalived实现高可用集群

master-b端:
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# systemctl status mysqld
mysql双主+keepalived实现高可用集群

2)查看VIP在哪个服务器上

master-a端:
[root@CentOS ~]#
[root@CentOS ~]# ip a #发现VIP此时位于master-a端
mysql双主+keepalived实现高可用集群

master-b端:
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# ip a
mysql双主+keepalived实现高可用集群

3)停掉master-a端的mysql服务,查看VIP在哪台服务器上

master-a端:
[root@CentOS ~]#
[root@CentOS ~]# systemctl stop mysqld
[root@CentOS ~]# ip a
mysql双主+keepalived实现高可用集群

master-b端:
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# ip a #去master-b端查看
mysql双主+keepalived实现高可用集群

4)启动master-a端的mysql服务以及keepalived服务,停止master-b端的mysql服务,此时在查看VIP在哪台服务器上
[root@CentOS ~]#
[root@CentOS ~]# systemctl start mysqld #启动master-a端的mysql
[root@CentOS ~]# systemctl start keepalived #启动master-a端的keepalived
[root@CentOS ~]#

[root@CentOS-2 ~]#
[root@CentOS-2 ~]# systemctl stop mysqld #停止master-b端的mysql
[root@CentOS-2 ~]#

[root@CentOS ~]#
[root@CentOS ~]# ip a #查看master-a端的网卡情况
mysql双主+keepalived实现高可用集群

[root@CentOS-2 ~]#
[root@CentOS-2 ~]# ip a #查看master-b端的网卡情况
mysql双主+keepalived实现高可用集群

3、实验结论

通过验证对比发现,MySQL双主集群通过keepalived成功实现了高可用

mysql双主+keepalived实现高可用集群

上一篇:MongoDB学习入门


下一篇:mysql