Mysql+keepalived主从切换
一、概述
Keepalived看名字就知道,保持存活,在网络里面就是保持在线了,也就是所谓的高可用或热备,用来防止单点故障(单点故障是指一旦某一点出现故障就会导致整个系统架构的不可用)的发生,那说到keepalived不得不说的一个协议不是VRRP协议,可以说这个协议就是keepalived实现的基础。
二、配置实现mysql+keepalived主从切换
网络结构:
VIP :192.168.1.30
MYSQL A:192.168.1.28
MYSQL B:192.168.1.29
1、mysql主从同步
要实现mysql+keepalived主从切换,首先要实现的就是两台mysql服务器的主从同步。关于主从同步,可以参考之前的文章
http://smalldeng.blog.51cto.com/1038075/1062192
2、安装keepalived
(1)用yum 安装
Yum -y install keepalived
(2)源码安装
#tar zxvf keepalived-1.2.7.tar.gz
#cd keepalived-1.2.7
#./configure
出错:
yum -y install openssl
yum -y install libpopt-dev
yum -y install libssl-dev
yum -y install libssl0.9.8
#make & make install
3、keepalived主备配置文件
下面将详细讲述在MYSQL A服务器上对keepalived的配置。
#vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
304749970@qq.com
}
notification_email_from 304749970@qq.com
smtp_connect_timeout 3
smtp_server 127.0.0.1
router_id LVS_DEVEL
}
vrrp_script check_run { #声明vrrp_script 的函数check_run
script "/root/mysql.sh"
interval 5
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state MASTER #指定主服务器节点为主节点,备用节点上设置为BACKUP即可
interface eth0 #指定虚拟IP的网络接口
virtual_router_id 51 #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组
priority 100 #主节点的优先级,数字越,优先级越高。(1-254之间),备用节点必须比主节点优先级低。
advert_int 1 #组播信息发送间隔,两个节点设置必须一样
nopreempt
authentication { #设置验证信息,两个节点必须一致
auth_type PASS
auth_pass 1111
}
track_script { #调用函数代码块
check_run
}
virtual_ipaddress { #虚拟IP
192.168.1.30
}
}
MYSQL B服务器的keepalived配置如下:
! Configuration File for keepalived
global_defs {
notification_email {
304749970@qq.com
}
notification_email_from 304749970@qq.com
smtp_connect_timeout 3
smtp_server 127.0.0.1
router_id LVS_DEVEL
}
vrrp_script check_run {
script "/root/mysql.sh"
interval 5
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_run
}
virtual_ipaddress {
192.168.1.30
}
}
4、mysql状态检测脚本
在目录root下创建脚本mysql.sh:
#vim /root/mysql.sh
#!/bin/bash
MYSQL=/usr/local/mysql/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=123456
CHECK_TIME=3
#mysql is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0
MYSQL_OK=1
function check_mysql_helth (){
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "show status;" &>/dev/null
if [ $? = 0 ] ;then
MYSQL_OK=1
else
MYSQL_OK=0
fi
return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check_mysql_helth
if [ $MYSQL_OK = 1 ] ; then
CHECK_TIME=0
exit 0
fi
if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ]
then
pkill keepalived
exit 1
fi
sleep 1
done
5、两台服务器同时启动mysql和keepalived
#/etc/init.d/mysqld start
#/etc/init.d/keepalived start
6、查看vrrp通读记录,进行测试
[root@master1 ~]# tcpdump vrrp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
12:04:10.537618 IP master1 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
12:04:11.540364 IP master1 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
[root@master1 ~]# ip a
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:51:6e:1f brd ff:ff:ff:ff:ff:ff
inet 192.168.1.28/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.30/32 scope global eth0
inet6 fe80::20c:29ff:fe51:6e1f/64 scope link
valid_lft forever preferred_lft forever
[root@master2 ~]# tcpdump vrrp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
15:15:02.529859 IP master1 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
15:15:03.531873 IP master1 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20
[root@master2 ~]# ip a
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:87:90:e8 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.29/24 brd 192.168.1.255 scope global eth0
inet6 fe80::20c:29ff:fe87:90e8/64 scope link
valid_lft forever preferred_lft forever
此时显示是master A服务器在提供服务。
关闭master A服务器上的mysql
[root@master1 ~]# /etc/init.d/mysqld stop
Shutting down MySQL. SUCCESS!
[root@master1 ~]# tcpdump vrrp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
14:03:00.245737 IP master2 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 51, prio 90, authtype simple, intvl 1s, length 20
14:03:01.246680 IP master2 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 51, prio 90, authtype simple, intvl 1s, length 20
[root@master1 ~]# ip a
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:51:6e:1f brd ff:ff:ff:ff:ff:ff
inet 192.168.1.28/24 brd 192.168.1.255 scope global eth0
inet6 fe80::20c:29ff:fe51:6e1f/64 scope link
valid_lft forever preferred_lft forever
[root@master2 ~]# tcpdump vrrp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
14:03:09.331826 IP master2 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 51, prio 90, authtype simple, intvl 1s, length 20
14:03:10.333790 IP master2 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 51, prio 90, authtype simple, intvl 1s, length 20
[root@master2 ~]# ip a
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:87:90:e8 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.29/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.30/32 scope global eth0
inet6 fe80::20c:29ff:fe87:90e8/64 scope link
valid_lft forever preferred_lft forever
然后由keepalived配置文件可以知道,mysql关闭的话,将会执行mysql.sh这一脚本。这个脚本在执行的时候,会判断mysql的状态,如果mysql关闭了,将会关闭keepalived进程。
还有一方式测试就是分别在两台服务器上建立远程连接用户,建立远程的用户时可以建立不同用户,或不同密码。
此方案还有一个缺点是当mysql服务器恢复正常时,需要手动启动keepalived服务,在网上有解决办法,不过我没成功。
下面两篇博客有兴趣可以参考下
http://bbs.chinaunix.net/thread-1824528-1-1.html
本文转自 deng304749970 51CTO博客,原文链接:http://blog.51cto.com/damondeng/1154197,如需转载请自行联系原作者