Mysql + keepalive高可用搭建
系统环境:centos6.8
Ip:192.168.137.36 主库
192.168.137.38 从库
VIP(虚拟ip/浮动ip):192.168.137.100
软件版本:mysql 5.7.20
Keepalived 1.2.13
主从搭建略
Mysql1
vim /etc/my.cnf
binlog-do-db=test
log-bin=mysql-bin
server-id=1
gtid-mode=ON
enforce-gtid-consistency
Mysql2
Vim /etc/my.cnf
gtid-mode=ON
enforce-gtid-consistency
log-bin=mysql-bin
log-slave-updates=ON
server-id=3
binlog-do-db=test
安装keepalived
yum install keepalived
在192.168.137.36主库修改keepalived的配置文件
注意:加颜色部分为修改部分
[root@zabbix ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs{
notification_email {
root@localhost
}
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
#VIP1
vrrp_instance VI_1 {
state BACKUP
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 151
priority 100
advert_int 5
nopreempt
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
192.168.137.100
}
}
virtual_server 192.168.137.100 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.137.36 3306 {
weight 100
notify_down /data/sh/mysql.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
保存退出
在192.168.137.38从库修改keepalived的配置文件
[root@localhost sh]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs{
notification_email {
root@localhost
}
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
#VIP1
vrrp_instance VI_1 {
state BACKUP
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 151
priority 98
advert_int 5
nopreempt
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
192.168.137.100
}
}
virtual_server 192.168.137.100 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.137.38 3306 {
weight 98
notify_down /data/sh/mysql.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
保存退出
上面的配置简单说明:
state BACKUP 表示为备模式,nopreempt 为不抢占,双方都配为备模式,并且不抢占,可以避免“脑裂”问题,priority 100 为权重,数字越高,权重越高,当双方都配为BACKUP模式,并且配置nopreempt时,keepalived靠这个数字来判断谁是主,谁是备
主从都要创建/data/sh/mysql.sh脚本
mkdir /data/sh
vim /data/sh/mysql.sh 加入以下内容:
#!/bin/bash
/etc/init.d/keepalived stop
chmod 755 /data/sh/mysql.sh
modprobe ip_vs #加载ip_vs模块 虚拟IP要用
lsmod | grep ip_vs 查看ip_vs模块有没有加载,如果看到下面的内容,就表示加载成功:
/etc/init.d/mysqld start #启动mysql
/etc/init.d/keepalive start #启动keepalived
在36上的操作和38上一样,只是keepalived.conf配置文件里priority 100 改为 priority 98
real_server 192.168.137.36 改为 real_server 192.168.137.38其他不变。
查看VIP情况命令: ip a
至此mysql的主从高可用就做好了,可以在36上测试挺掉mysql服务,看看vip会不会漂移到38上,一般来说都是没问题的,如果有问题,请检查你的配置,步骤是不是有错误,还有selinux,防火墙是否关闭等
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。