1:准备工作:
CentOS release 6.7 (Final)
master(slave)-192.168.0.171
slave(master)-192.168.0.170
VIP=192.168.0.200
2:开始配置mysql主从配置:
参考:
http://linuxgentoo.blog.51cto.com/7678232/1556647
我是用的这个账号:
mysql> grant all on *.* to 'ab'@'192.168.0.%' identified by '123';
mysql> change master to master_host='192.168.0.170',master_port=3306,master_user='ab',master_password='123',master_log_file='mysql-bin.000012',master_log_pos=1192;
你们需咬根据需求来设置权限
3:安装配置keepalived
A:配置192.168.0.171
[root@test3 ~]# cat /etc/keepalived/keepalived.conf
! 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 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP #两台配置此处均是BACKUP
interface eth0
virtual_router_id 51
priority 100 #优先级,另一台改为90
advert_int 1
nopreempt #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.200
}
}
virtual_server 192.168.0.200 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 50 #会话保持时间
protocol TCP
real_server 192.168.0.171 3306 {
weight 3
notify_down /usr/local/keepalived/mysql.sh
TCP_CHECK {
connect_timeout 10 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连间隔时间
connect_port 3306 #健康检查端口
}
}
}
配置:192.168.0.170
[root@mysql ~]# cat /etc/keepalived/keepalived.conf
! 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 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP #两台配置此处均是BACKUP
interface eth0
virtual_router_id 51
priority 90 #优先级,另一台改为90
advert_int 1
nopreempt #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.200
}
}
virtual_server 192.168.0.200 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 50 #会话保持时间
protocol TCP
real_server 192.168.0.170 3306 {
weight 3
notify_down /usr/local/keepalived/mysql.sh
TCP_CHECK {
connect_timeout 10 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连间隔时间
connect_port 3306 #健康检查端口
}
}
}
配置完成之后,定义脚本
mkdir /usr/local/keepalived
[root@mysql ~]# cat /usr/local/keepalived/mysql.sh
#!/bin/bash
pkill keepalived
chmod +x /usr/local/keepalived/mysql.sh
线上最好用这个
#!/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
这样就可以启动mysql和keepalived来测试了
我们使用Navicat 来连接测试
发现数据是一致的
我们关闭主mysql(192.168.0.171)来看看是不是可以切换到从mysql(192.168.0.170)
参考:
阅读(1794) | 评论(1) | 转发(0) |