MySQL双主互备+keepalived高可用架构介绍
MySQL主从复制架构可以在很大程度保证MySQL的高可用,在一主多从的架构中还可以利用读写分离将读操作分配到从库中,减轻主库压力。但是在这种架构中,主库出现故障时需要手动将一台从库提升为主库。在对写操作要求较高的环境中,主库故障在主从架构中会成为单点故障。因此需要主主互备架构,避免主节点故障造成写操作失效。
在双主互备的架构中,每台MySQL都充当主服务器,同时充当对方的从服务器。在任意一台服务器上的写操作都会被复制到另一台服务器上,从而保证了数据的可靠性。
在双主互备的基础上加上keepalived,在其中一台机器上绑定虚拟ip(VIP)。利用vip统一对外服务,可以避免在两个节点同时写数据造成冲突。同时当keepalived主节点发生故障时,keeplived会自动将VIP切换到备节点上,从而实现主服务器的高可用。
环境介绍:
mysql-5.7.16
端口号:3306
IP:192.168.209.130(优先级高)
192.168.209.133
keepalived-1.2.7
************************************************************************
一、MySQL5.7安装
建配置文件目录
mkdir -p /data/ini/mysql3306/
把配置文件上传到这个目录下
创建数据目录
tar -zxvf mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.7.16-linux-glibc2.5-x86_64 mysql
mkdir -p /data/mysql3306/
mkdir -p /data/log/mysql3306
初始化
/data/mysql/bin/mysqld --defaults-file=/data/ini/mysql3306/my.cnf --initialize-insecure --explicit_defaults_for_timestamp --basedir=/data/mysql --datadir=/data/mysql3306/ --user=sys
ls /data/log/mysql3306 检查初始化是否成功
启动服务
/data/mysql/bin/mysqld_safe --defaults-file=/data/ini/mysql3306/my.cnf --user=sys &
ps -ef|grep mysql 检查进程是否启动
登陆mysql
/data/mysql/bin/mysql -uroot -p --socket = /data/log/mysql3306/mysql.sock
重设密码
update mysql.user set authentication_string=password('root123') where user='root' and Host = 'localhost';
flush privileges;
登陆mysql
/usr/local/mysql/bin/mysql -uroot -p --socket = /data/log/mysql3306/mysql.sock
查看密码,用户密码情况
select user,host,authentication_string from mysql.user;
*******************************************************************************************
二、生产新环境配置双主
生产中搭建主从
reset master;只适用于新环境
30主库授权
use mysql;
grant replication slave on *.* to 'reply1'@'192.168.209.%' identified by '123456';
flush privileges;
SHOW MASTER STATUS; 记录下来binlog文件和position号
stop slave;
33从库配置
CHANGE MASTER TO MASTER_HOST='192.168.209.130',MASTER_USER='reply1',MASTER_PASSWORD='123456',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=154;
start slave;
show slave status\G;
reset master;只适用于新环境
33主库授权
use mysql;
grant replication slave on *.* to 'reply2'@'192.168.209.%' identified by '123456';
flush privileges;
SHOW MASTER STATUS; 记录下来binlog文件和position号
stop slave;
30从库配置
CHANGE MASTER TO MASTER_HOST='192.168.209.133',MASTER_USER='reply2',MASTER_PASSWORD='56789',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=154;
start slave;
show slave status\G;
*********************************************************************************************************
三、生产新环境双主GTID模式
在前面主主搭建好的基础上
从库配置用户及binlog位置信息(推荐使用GTID模式)
#GTID模式配置
CHANGE MASTER TO MASTER_HOST='192.168.209.130',MASTER_USER='reply1',MASTER_PORT=3306,MASTER_PASSWORD='123456',MASTER_AUTO_POSITION = 1 FOR CHANNEL '111';
CHANGE MASTER TO MASTER_HOST='192.168.209.133',MASTER_USER='reply2',MASTER_PORT=3306,MASTER_PASSWORD='123456',MASTER_AUTO_POSITION = 1 FOR CHANNEL '112';
mysql> start slave;
mysql> show slave status\G;
**********************************************************************************************************
四、30和33安装keepalived
30上配置keepalived
1、安装依赖包
[root@backup ~]# yum install -y pcre-devel openssl-devel popt-devel #安装依赖包
2、
[root@master ~]# wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
[root@master ~]# tar zxvf keepalived-1.2.7.tar.gz
[root@master ~]# cd keepalived-1.2.7
[root@master ~]#./configure --prefix=/usr/local/keepalived
3、
./configure --prefix=/usr/local/keepalived报错
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/root/keepalived/keepalived-1.2.2':
configure: error: no acceptable C compiler found in $PATH
4、
依据报错显示缺少依赖包
yum install gcc -y
5、
make && make install
6、
#将keepalived配置成系统服务
[root@master ~]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@master ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@master ~]# mkdir /etc/keepalived/
[root@master ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@master ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
7、配置文件详情:
30上keepalived的配置文件详情
[root@master ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
### keepalived有自己的报警机制,但一般不用,所以注释掉,只留下router_id
# notification_email {
# [email protected]
# [email protected]
# [email protected]
# }
# notification_email_from [email protected]
# smtp_server 10.10.10.2
# smtp_connect_timeout 30
router_id Mysql-Master1-96 # 全网唯一标识,不能有重复出现
}
vrrp_instance VI_1{
# 两台配置此处均是BACKUP
state BACKUP
# 注意网卡接口
interface eth0
virtual_router_id 201 #虚拟路由标识,MASTER和BACKUP是一致的,但在整个VRRP中是唯一的
priority 100 # 优先级,另一台改为200 值越大优先级越高
advert_int 1
# 不主动抢占资源
authentication {
auth_type PASS
auth_pass MysqlMaster196
}
virtual_ipaddress {
192.168.209.201 #虚拟IP
}
}
virtual_server 192.168.209.201 3306 {
# 每个2秒检查一次real_server状态
delay_loop 2
# LVS算法
lb_algo wrr
# LVS模式
lb_kind DR
# 会话保持时间
persistence_timeout 60
protocol TCP
real_server 192.168.209.130 3306 {
weight 3 #权重值,值越大,权重越高,可以承担的负载雨大,服务器硬件比较好,可以使用高一点的权重
# 检测到服务down后执行的脚本
notify_down /etc/rc.d/keepalived.sh
TCP_CHECK {
# 连接超时时间
connect_timeout 10
# 重连次数
nb_get_retry 3
# 重连间隔时间
delay_before_retry 3
# 健康检查端口
connect_port 3306
}
}
}
8、cat /etc/rc.d/keepalived.sh
#!/bin/sh
pkill keepalived
9、
启动keepalived服务
[root@master ~]# /etc/init.d/keepalived start
34上配置keepalived
1、
tar -zxvf keepalived-1.2.7.tar.gz
[root@master ~]# cd keepalived-1.2.7
[root@master ~]#./configure --prefix=/usr/local/keepalived
2、
make && make install
3、
#将keepalived配置成系统服务
[root@master ~]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@master ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@master ~]# mkdir /etc/keepalived/
[root@master ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@master ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
4、
12服务器上的keepalived的配置文件
! Configuration File for keepalived
global_defs {
### keepalived有自己的报警机制,但一般不用,所以注释掉,只留下router_id
# notification_email {
# [email protected]
# [email protected]
# [email protected]
# }
# notification_email_from [email protected]
# smtp_server 10.10.10.2
# smtp_connect_timeout 30
router_id Mysql-Master1-97 # 全网唯一标识,不能有重复出现
}
vrrp_instance VI_1{
# 两台配置此处均是BACKUP
state BACKUP
# 注意网卡接口
interface eth0
virtual_router_id 201 #虚拟路由标识,MASTER和BACKUP是一致的,但在整个VRRP中是唯一的
priority 50 # 优先级,另一台改为200 值越大优先级越高
advert_int 1
# 不主动抢占资源
nopreempt
authentication {
auth_type PASS
auth_pass MysqlMaster196
}
virtual_ipaddress {
192.168.209.201
}
}
virtual_server 192.168.209.201 3306 {
# 每个2秒检查一次real_server状态
delay_loop 2
# LVS算法
lb_algo wrr
# LVS模式
lb_kind DR
# 会话保持时间
persistence_timeout 60
protocol TCP
real_server 192.168.209.133 3306 {
weight 3 #权重值,值越大,权重越高,可以承担的负载雨大,服务器硬件比较好,可以使用高一点的权重
# 检测到服务down后执行的脚本
notify_down /etc/rc.d/keepalived.sh
TCP_CHECK {
# 连接超时时间
connect_timeout 10
# 重连次数
nb_get_retry 3
# 重连间隔时间
delay_before_retry 3
# 健康检查端口
connect_port 3306
}
}
}
5、cat /etc/rc.d/keepalived.sh
#!/bin/sh
pkill keepalived
6、
启动keepalived服务
[root@master ~]# /etc/init.d/keepalived start
*******************************************************************************************************************
五、配置防火墙
1、
因为线上服务器之间防火墙是需要开着的这样的话keepalived之间不能互信就会出现两个虚拟IP
需要在两边防火墙配置文件中配置LVS
vi /etc/sysconfig/iptables
#LVS
-A INPUT -s 192.168.10.0/24 -p vrrp -j ACCEPT
2、
重启防火墙即可
service iptables restart
*****************************************************************************************************************
六、测试(略)