首页 > 代码库 > MySQL高可用性之Keepalived+MySQL(双主热备)

MySQL高可用性之Keepalived+MySQL(双主热备)

MySQL双主复制,即互为Master-Slave(只有一个Master提供写操作),可以实现数据库服务器的热备,但是一个Master宕机后不能实现动态切换。而Keepalived通过虚拟IP,实现了双主对外的统一接口以及自动检查、失败切换机制。联合使用,可以实现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

1按照之前的文章,搭建双主备机

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/


4修改配置文件
[root@master ~]# vi /etc/keepalived/keepalived.conf  

[root@master ~]# vi /usr/local/keepalived/mysql.sh 
    #!/bin/bash
    pkill keepalived
[root@master ~]# chmod +x /usr/local/keepalived/mysql.sh 
[root@master ~]# /etc/init.d/keepalived start

#backup服务器只修改priority为90、nopreempt不设置、real_server设置本地IP。

5授权两台Mysql服务器允许root远程登录,用于在其他服务器登陆测试!
mysql>grant all privileges on *.* to ‘root‘@‘%‘ identified by ‘root‘;
mysql>flush privileges

6、测试高可用性

    1、通过Mysql客户端通过VIP连接,看是否连接成功。
    2、停止master这台mysql服务,是否能正常切换过去,可通过ip addr命令来查看VIP在哪台服务器上。

技术分享
    3、可通过查看/var/log/messges日志,看出主备切换过程
    4、master服务器故障恢复后,是否主动抢占资源,成为活动服务器。



MySQL高可用性之Keepalived+MySQL(双主热备)