MySQL高可用性之Keepalived+MySQL(双主热备)
Posted edgedance
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL高可用性之Keepalived+MySQL(双主热备)相关的知识,希望对你有一定的参考价值。
OS:CentOS6.5_X64
MASTER:192.168.196.133
BACKUP:192.168.196.134
VIP:192.168.196.135
# date -s "20170707 01:51:42"
# hwclock --systohc
2安装keepalived
[root@backup ~]# yum install -y pcre-devel openssl-devel popt-devel #安装依赖包
[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
[root@master ~]#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/
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id MYSQL_HA #标识,双主相同
}
vrrp_instance VI_1 {
state BACKUP #两台都设置BACKUP
interface eth1
virtual_router_id 51 #主备相同
priority 100 #优先级,backup设置90
advert_int 1
nopreempt #不主动抢占资源,只在master这台优先级高的设置,backup不设置
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.196.135
}
}
virtual_server 192.168.196.135 3306 {
delay_loop 2
#lb_algo rr #LVS算法,用不到,我们就关闭了
#lb_kind NAT #LVS模式,如果不关闭,备用服务器不能通过VIP连接主MySQL
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.196.133 3306 { #检测本地mysql,backup也要写检测本地mysql
weight 3
notify_down /usr/local/keepalived/mysql.sh #当mysq服down时,执行此脚本,杀死keepalived实现切换
TCP_CHECK {
connect_timeout 3 #连接超时
nb_get_retry 3 #重试次数
delay_before_retry 3 #重试间隔时间
}
}
}
[root@master ~]# /etc/init.d/keepalived start
1、通过Mysql客户端通过VIP连接,看是否连接成功。
2、停止master这台mysql服务,是否能正常切换过去,可通过ip addr命令来查看VIP在哪台服务器上。
4、master服务器故障恢复后,是否主动抢占资源,成为活动服务器。
以上是关于MySQL高可用性之Keepalived+MySQL(双主热备)的主要内容,如果未能解决你的问题,请参考以下文章
优酷土豆资深工程师:MySQL高可用之MaxScale与MHA