欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MySQL5.7双机热备及keepalived高可用

程序员文章站 2022-04-09 22:57:10
...

前言

环境:centos7,MySQL5.7,keepalived2.0.19
事前准备:MySQl完成双主热备或主从复制
Master1 192.168.137.128 server_id=1 +keepalived
Master2 192.168.137.129 server_id=2 +keepalived
VIP 192.168.137.130

一.下载及安装keepalived

master1 和master2都需要安装及配置keepalived
安装前准备:

yum -y install gcc gcc-c++ autoconf automake make
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

1.1 wget方式安装keepalived

wget方式安装编译keepalived,命令如下:

cd /usr/local/src/
wget https://www.keepalived.org/software/keepalived-2.0.19.tar.gz
tar -zxvf keepalived-2.0.19.tar.gz
cd keepalived-2.0.19
./configure --prefix=/usr/local/keepalived
make
make install
cd /usr/local/keepalived/sbin/
cp keepalived /usr/sbin/

设置配置文件夹并拷贝配置文件

mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

1.2 yum方式安装keepalived

yum -y install keepalived

查看安装的版本信息

/usr/sbin/keepalived -version

1.3查看服务启动状态并设置开机启动

查看keepalived系统启动状态

systemctl list-unit-files|grep keepalived

相关命令如下:

启动.关闭.状态.重启: systemctl start/stop/status/restart keepalived.service
设置开机自动启动: systemctl enable keepalived.service
取消开机自动启动: systemctl disable keepalived.service

二.配置文件修改

2.1 keepalived.conf 文件修改

vim /etc/keepalived/keepalived.conf

master1配置及说明


! Configuration File for keepalived

global_defs {
   router_id MASTER-1  #标识节点的字符串,通常为hostname  2台keepalived配置不同即可
}

## keepalived 会定时执行脚本并且对脚本的执行结果进行分析,动态调整vrrp_instance的优先级。这里的权重weight 是与下面的优先级priority有关,如果执行了一次检查脚本成功,则权重会-20,也就是由100 - 20 变成了80,Master 的优先级为80 就低于了Backup的优先级90,那么会进行自动的主备切换。
vrrp_script  check_mysql_alive {
    script "/etc/keepalived/check_mysql_alive.sh"   ##执行脚本位置
    interval 2               ##检测时间间隔    #调用脚本两次之间的间隔,默认为1秒     
    weight -5                ## 如果条件成立则权重减5(-5)
}
## 定义虚拟路由 VI_1为自定义标识
vrrp_instance VI_1 {
    state BACKUP        ## 主节点为MASTER,备份节点为BACKUP  两个keepalived都配置为BACKUP,防止脑裂,两台配置此处均是BACKUP,设为BACKUP将根据优先级决定主或从
    interface ens33     ## 绑定虚拟IP的网络网卡,与本机IP地址所在的网络接口相同,可通过ifconfig命令查看
    virtual_router_id 51       ## 虚拟路由ID号 ,主从需要配置相同,(取值0-255之间)
    mcast_src_ip 192.168.137.128  ## 指定发送组播数据包的源IP地址。默认是绑定VRRP实例的接口的主IP地址。
    priority 100        ##优先级配置(1-255的值)第二台配置为90 ,  2台keepalived配置不同即可
    nopreempt    ##非抢占模式,即允许一个priority比较低的节点作为master.通常如果master服务死掉后backup会变成master,但是当master服务又好了的时候 master此时会抢占VIP,这样就会发生两次切换对业务繁忙的网站来说是不好的。所以我们要在配置文件加入 nopreempt 非抢占,但是这个参数只能用于state 为backup,故我们在用HA的时候最好master 和backup的state都设置成backup 让其通过priority来竞争。
    advert_int 1    ## 组播信息发送间隔,多久进行一次master选举,俩个节点必须配置一致,默认1s,可以认为是健康查检时间间隔
    authentication {
        auth_type PASS   #认证区域,认证类型有PASS和HA(IPSEC),推荐使用PASS(密码只识别前8位)
        auth_pass 1111    ## 真实生产环境下对密码进行匹配
    }
    track_script {
       check_mysql_alive     ##上面定义vrrp_script的名称
    }
    virtual_ipaddress {
        192.168.137.130   ## 虚拟ip(vip),可以指定多个 ,如果master宕机,虚拟ip会自动漂移到slave上
    }
}

# 以下代码也可以不设置
virtual_server 192.168.137.130 3306 {
    delay_loop 2  #设置运行情况检查时间,单位是秒
    lb_algo rr   #设置后端调度算法,这里设置为rr,即轮询算法
    lb_kind DR #设置LVS实现负载均衡的机制,有NAT、TUN、DR三个模式可选
    persistence_timeout 50 #会话保持时间,单位是秒。这个选项对动态网页是非常有用的,为集群系统中的session共享提供了一个很好的解决方案。有了这个会话保持功能,用户的请求会被一直分发到某个服务节点,直到超过这个会话的保持时间。
    protocol TCP #指定转发协议类型,有TCP和UDP两种
    real_server 192.168.137.128 3306 {
        weight 3 #配置服务节点的权值,权值大小用数字表示,数字越大,权值越高,设置权值大小为了区分不同性能的服务器
        notify_stop /etc/keepalived/shutdown_keepalived.sh //检测到mysql服务down后执行的脚本,在一些版本中叫notify_down
        TCP_CHECK {
            connect_timeout 3 //连接超时时间
            nb_get_retry 3 //重连次数
            delay_before_retry 3 //重连间隔时间
            connect_port 3306 //健康检查端口
        }
}}

master2配置参数如master1,修改参数如下:

router_id MASTER-2
mcast_src_ip 192.168.137.129 
priority 90
如配置下方参数的话:
 real_server 192.168.137.128 3306 改为 real_server 192.168.137.129 3306

keepalived 配置文件参数详解参考:
https://blog.csdn.net/mofiu/article/details/76644012
https://www.cnblogs.com/along1226/p/5027838.html

2.2 检测脚本编写

vim /etc/keepalived/check_mysql_alive.sh

脚本内容如下:(也可在监听mysql失败后停顿几秒然后重启mysql和keepalived)

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

如配置下方参数的话,停止keepalived服务的脚本如下:

vim /etc/keepalived/shutdown_keepalived.sh

#!/bin/bash
systemctl stop keepalived 

给脚本添加执行权限

chmod +x /etc/keepalived/check_mysql_alive.sh
chmod +x /etc/keepalived/shutdown_keepalived.sh

三.测试

因为执行脚本的的存在,需要先启动mysql服务,再启动keepalive服务
master1和master2两台服务器都要授权允许root用户远程登录,用于在客户端登陆测试!

mysql> grant all on . to aaa@qq.com’%’ identified by “123456”;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

用130的虚拟ip连接mysql,测试master1和master2分别挂掉后,130连接的是哪个mysql
MySQL5.7双机热备及keepalived高可用

看mysql挂掉后的keepalived状态

systemctl status keepalived

注意:重启时记得先启动mysql,再启动 keepalived

四.配置keepalived单独的日志文件

默认keepalived的日志在/var/log/messages中,修改的话:

--1、修改/etc/sysconfig/keepalived,从KEEPALIVED_OPTIONS="-D" 改为 KEEPALIVED_OPTIONS="-D -d -S 0"
[aaa@qq.com ~]# vi /etc/sysconfig/keepalived
# Options for keepalived. See `keepalived --help' output and keepalived(8) and
# keepalived.conf(5) man pages for a list of all options. Here are the most
# common ones :
#
# --vrrp               -P    Only run with VRRP subsystem.
# --check              -C    Only run with Health-checker subsystem.
# --dont-release-vrrp  -V    Dont remove VRRP VIPs & VROUTEs on daemon stop.
# --dont-release-ipvs  -I    Dont remove IPVS topology on daemon stop.
# --dump-conf          -d    Dump the configuration data.
# --log-detail         -D    Detailed log messages.
# --log-facility       -S    0-7 Set local syslog facility (default=LOG_DAEMON)
#
 
KEEPALIVED_OPTIONS="-D -d -S 0"
 
--2、修改/etc/rsyslog.conf文件 ,最后增加 local0.*    /var/log/keepalived.log
[aaa@qq.com ~]# vi /etc/rsyslog.conf
#keepalived#
local0.*                                                /var/log/keepalived.log
 
--重启rsyslog和keepalived
[aaa@qq.com ~]# systemctl restart rsyslog.service
[aaa@qq.com ~]# systemctl restart keepalived.service
 
--查看/var/log/keepalived.log 文件有日志生成
[aaa@qq.com ~]# tail -10 /var/log/keepalived.log

五.相关问题

keepalived问题: 可能遇到脚本不执行的情况

1、权限不足问题:给脚本加上相关权限,放到/etc/keepalived/下面

2、keepalived配置问题:修改配置vrrp_script check_mysql_alive后面的{换行,并且不必要的空格和注释都删除

3、脚本本身问题;执行脚本测试

4、vrrp_script 需要放到vrrp_instance VI_1的上面才行。

5、查看keepalived启动日志: tail -f /var/log/messages

临时和永久关闭SELINUX

1.临时关闭:

[aaa@qq.com ~]# getenforce
Enforcing
[aaa@qq.com ~]# setenforce 0
[aaa@qq.com ~]# getenforce
Permissive

2.永久关闭:

[aaa@qq.com ~]# vim /etc/sysconfig/selinux
SELINUX=enforcing 改为 SELINUX=disabled
重启服务 reboot