mysql高可用服务部署
Mysql主从搭建完毕,(mysql 主从部署请看MySQL 主从复制)现在有一个问题,如果master服务器down机了,如何快速恢复服务呢?
我们有两套解决方法:
1.如果程序连接的是master的IP,直接在slave服务器上添加master的IP即可。这个手动去操作,而且需要花费时间比较长,可能还会出现误操作的情况,不推荐
2.可以使用keepalived、heartbeat作为HA检测软件,检查MySQL服务是否正常,不正常则自动切换到slave上,推荐使用
那么我们接下来就看一下如何实现:
cd /data
yum -y install kernel-devel popt-devel openssl-devel
wget http://keepalived.org/software/keepalived-1.2.1.tar.gz
tar zxf keepalived-1.2.1.tar.gz
./configure --prefix=/usr/local/keepalived
--with-kernel-dir=/usr/src/kernels/2.6.32-573.7.1.el6.x86_64/
在编译的时候会遇到一些问题
****************************************************************************************
注意:若此处 /usr/src/kenels/ 下没东西,可能过安装 kernel-devel 组件:
yum -y install kernel-devel
安装后,执行以上操作
yum -y install popt-devel
出现此提示,是缺少openssl-devel 组件,直接安装即可
****************************************************************************************
出现此界面,证编译成功
make && make install ;echo $?
若返回0,即证明安装成功
设置开机启动
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig
cp /usr/local/keepalived/sbin/keepalived /usr/sbin
chkconfig --add keepalived
chkconfig --level 35 keepalived on
keepalived 安装成功
mkdir -p /etc/keepalived
配置master服务器,keepalived配置文件内容如下:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
185869915@163.com
}
notification_email_from 185869915@163.com
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.10.200 #此处为虚拟IP
}
}
virtual_server 192.168.33.100 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.10.171 3306 { #此处为你的真实机IP
weight 100
notify_down /data/sh/mysql.sh #此处为关闭 keepalived 的脚本地址
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
mysql从 主机部署与主相同,只需将配置文件中的 real_server 修改为自己的IP ,将优先级修改为小于100的数字即可
mysql.sh 脚本内容为:
#!/bin/bash
pkill -0 mysqld
if [[ ! $? -eq 0 ]];then
/etc/init.d/mysqld start
sleep 5
pkill -0 mysqld
if [[ ! $? -eq 0 ]];then
pkill keepalived
fi
fi
给脚本执行权限:
chmod +x /data/sh/mysql.sh
本文转自 菜鸟的征程 51CTO博客,原文链接:http://blog.51cto.com/songqinglong/1700670