MySQL主主:http://blog.csdn.net/chengshiep/article/details/50943221
两台服务器
MySQL-master1(主):192.168.2.202
MySQL-master2(备):192.168.2.222
MySQL-VIP(虚拟ip):192.168.2.201
MySQL-master1(主)
#修改防火墙设置
vi /etc/sysconfig/iptables
#加上
iptables -A INPUT -p vrrp -j ACCEPT
service iptables restart
#安装keepalived
cd /usr/local/src
tar zxvf keepalived-1.2.19.tar.gz
cd keepalived-1.2.19
./configure --prefix=/usr/local/keepalived --with-kernel-dir
make && make install
#复制keepalived脚本
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/
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
#设置keepalived开机启动脚本
chkconfig keepalived on
#修改配置脚本
vi /etc/keepalived/keepalived.conf
#内容如下
global_defs {
#notification_email {
# 1025583836@qq.com
#}
#notification_email_from 1025583836@qq.com
#smtp_server smtp.qq.com
#smtp_connect_timeout 30
router_id MySQL-ha
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.2.201
}
}
virtual_server 192.168.2.201 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.2.202 3306 {
weight 3
notify_down /etc/rc.d/keepalived.sh
TCP_CHECK {
# 连接超时时间
connect_timeout 10
# 重连次数
nb_get_retry 3
# 重连间隔时间
delay_before_retry 3
# 健康检查端口
connect_port 3306
}
}
}
#编写检测服务down后所要执行的脚本
vi /etc/rc.d/keepalived.sh
#内容如下
----------
#!/bin/sh
/etc/init.d/keepalived stop
----------
#或者内容如下
----------
#!/bin/bash
# 环境变量
PATH=/bin:/usr/bin:/sbin:/usr/sbin; export PATH
# 暂停5秒执行,防止数据库人工正常重启
sleep 5
# mysql_id(存活=1 死掉=0)
mysql_id=`ps -C mysqld --noheader |wc -l`
# 判断mysql_id若死掉,则重启mysql一次,若仍然无法启动mysql则杀掉keepaliaved进程实现VIP切换
if [ $mysql_id -eq 0 ];then
/etc/init.d/mysqld restart
sleep 5
if [ $mysql_id -eq 0 ];then
/etc/init.d/keepalived stop
fi
fi
----------
#启动keepalived
/etc/init.d/keepalived start
#查看连接状态
ps -aux | grep keepalived
#查看VIP信息
ip addr
#查看vrrp通信
tcpdump vrrp
#查看keepalived进程
ps -aux | grep keepalived
MySQL-master2(备)
#修改防火墙设置
vi /etc/sysconfig/iptables
#加上
iptables -A INPUT -p vrrp -j ACCEPT
service iptables restart
#安装keepalived
cd /usr/local/src
tar zxvf keepalived-1.2.19.tar.gz
cd keepalived-1.2.19
./configure --prefix=/usr/local/keepalived --with-kernel-dir
make && make install
#复制keepalived脚本
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/
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
#设置keepalived开机启动脚本
chkconfig keepalived on
#修改配置脚本
vi /etc/keepalived/keepalived.conf
#内容如下
----------
global_defs {
#notification_email {
# 1025583836@qq.com
#}
#notification_email_from 1025583836@qq.com
#smtp_server smtp.qq.com
#smtp_connect_timeout 30
router_id MySQL-ha
}
vrrp_instance VI_1 {
state BACKUP
interface em1
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.2.201
}
}
virtual_server 192.168.2.201 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.2.222 3306 {
weight 1
notify_down /etc/rc.d/keepalived.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
----------
#编写检测服务down后所要执行的脚本
vi /etc/rc.d/keepalived.sh
#内容如下
----------
#!/bin/sh
/etc/init.d/keepalived stop
----------
#或者内容如下
----------
#!/bin/bash
# 环境变量
PATH=/bin:/usr/bin:/sbin:/usr/sbin; export PATH
# 暂停5秒执行,防止数据库人工正常重启
sleep 5
# mysql_id(存活=1 死掉=0)
mysql_id=`ps -C mysqld --noheader |wc -l`
# 判断mysql_id若死掉,则重启mysql一次,若仍然无法启动mysql则杀掉keepaliaved进程实现VIP切换
if [ $mysql_id -eq 0 ];then
/etc/init.d/mysqld restart
sleep 5
if [ $mysql_id -eq 0 ];then
/etc/init.d/keepalived stop
fi
fi
----------
#启动keepalived
/etc/init.d/keepalived start
#查看连接状态
ps -aux | grep keepalived
#查看VIP信息
ip addr
#查看vrrp通信
tcpdump vrrp
#查看keepalived进程
ps -aux | grep keepalived
其他命令
#设置开机启动
chkconfig mysqld on
chkconfig keepalived on
#查看连接状态
ps -aux | grep keepalived
#查看VIP信息
ip addr
#查看vrrp通信
tcpdump vrrp
#查看keepalived进程
ps -aux | grep keepalived
#注意两个数据库的用户名需要有一套相同的,用于vip链接
#主Keepalived故障重启,不希望抢占备keepalived,可以配置nopreempt属性
设置为不抢占
注:这个配置只能设置在backup主机上,而且这个主机优先级要比另外一台高
advert_int 1
nopreempt