转自
MySQL 高可用:mysql+Lvs+Keepalived 负载均衡及故障转移 - KK ——专注数据 - 博客频道 - CSDN.NET
http://blog.csdn.net/kk185800961/article/details/51115264#
系统信息:
mysql主库192.168.1.152 CentOS 5.6 mysql 5.6.22mysql从库192.168.1.153 CentOS 5.6 mysql 5.6.22VIP192.168.1.150
mysql 主主同步都设置 auto-increment-offset,auto-increment-increment ,以避免自增列冲突。
参考:MySQL 高可用:主主复制(双主复制)
yum -y install gcc gcc-c++ gcc-g77 ncurses-devel bison libaio-devel
cmake libnl* libpopt* popt-static openssl-devel
# LVS 安装配置:(yum -y install ipvsadm)
cd /usr/local/src
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
tar zxvf ipvsadm-1.24.tar.gz
cd ipvsadm-1.24ln-s /usr/src/kernels/2.6.18-402.el5-i686/ /usr/src/linux #找自己的目录看看/usr/src/kernels
make&& make install
# Keepalived 安装配置
cd /usr/local/src
wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz
tar zxvf keepalived-1.2.19.tar.gz
cd keepalived-1.2.19./configure --prefix=/usr/local/keepalived --disable-fwmark
make&&make install
cp/usr/local/keepalived/sbin/keepalived /usr/sbin/cp/usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/cp/usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/mkdir-p /etc/keepalived/cp/usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
#编辑或者新建(主备除两地方,其他一样.备库改为 :state BACKUP ,priority 50)
vi /etc/keepalived/keepalived.conf
! Configuration File forkeepalived
global_defs { #全局标识模块
notification_email { #定义邮件通知
test@163.com #目标邮箱
}
notification_email_from test@163.com #发送邮箱
smtp_server127.0.0.1#发送邮箱的smtp服务器
smtp_connect_timeout30#smtp服务器连接超时时间
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER #本实例启动状态:MASTER/BACKUPinterfaceeth0 #监控的网络接口
virtual_router_id51#vrrp实例(同一个组主备服务器设置一样)
priority100#优先级高的为master,不能超过255。(BACKUP可设置为50)
advert_int1#均衡器检测间隔1秒(服务器设置都一样)
authentication { #验证类型及密码(服务器设置都一样)
auth_type PASS #认证方式,PASS或AH
auth_pass123456#认证密码
}
virtual_ipaddress { #虚拟ip地址virtual_ipaddress,可以定义多个192.168.1.150}
}
virtual_server192.168.1.150 3306{ #定义虚拟服务器,与上面的virtual_server一样
delay_loop6#健康检查时间间隔,6秒
lb_algo rr #负载均衡调度算法:rr|wrr|lc|wlc|sh|dh|lblc
lb_kind NAT #负载均衡转发规则:NAT|DR|TUN
nat_mask255.255.255.0persistence_timeout50#回话保持时间50秒,动态服务建议开启
protocol TCP #转发协议protocol,一般有tcp和udp两种
#后端真实服务器,有几台就设置几个
real_server192.168.1.152 3306{
weight1#权重越大负载分越大,0表示失效
SSL_GET { #健康检查方式:HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK
connect_timeout3nb_get_retry3delay_before_retry3}
}
real_server192.168.1.153 3306{
weight1SSL_GET {
connect_timeout3nb_get_retry3delay_before_retry3}
}
}
#启动 keepalived 服务/etc/rc.d/init.d/keepalived start
#添加到rc.local自启动中
echo"/etc/rc.d/init.d/keepalived start" >> /etc/rc.local
#152服务器IP
[root@centos152 keepalived]# ip a1: lo: mtu 16436qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet127.0.0.1/8scope host lo2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000link/ether 00:0c:29:3b:c4:a5 brd ff:ff:ff:ff:ff:ff
inet192.168.1.152/24 brd 192.168.1.255 scope globaleth0
inet192.168.1.150/32 scope global eth0
#现在在第三台mysql服务器中连接VIP测试
#ping VIP 正常
ping192.168.1.150#连接到 VIP 正常
mysql-umysqlproxy -pmysqlproxy -h192.168.1.150 --port3306
#到 mysql 服务器152 和 153查看进程,查看用户 mysqlproxy 连接到哪台服务器
mysql>show processlist;
#停止 mysqlproxy 所在的服务器keepalived 服务/etc/rc.d/init.d/keepalived stop
此时在150 连接的mysql切换到另一台服务器的mysql数据库了。/etc/rc.d/init.d/keepalived start
末尾有些测试还没有规范,仅供参考。
更多参考: