两台机子,一台是192.168.223.143做主,一台是192.168.223.144主从,现在要实现的需求是两台机子做主从+高可用,VIP这里设置为192.168.223.100
首先在两台机子上装好mysql,做好主从复制,
参考
二、在两台机子上安装keepalived
tar –zxvf keepalived-1.2.7.tar.gz
cd keepalived-1.2.7
yum install kernel-devel openssl-devel popt-devel -y
./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.i686/
make && make install
拷贝启动脚本等
cd /usr/local/keepalived
cp etc/rc.d/init.d/keepalived /etc/init.d/
cp etc/sysconfig/keepalived /etc/sysconfig/
cp sbin/keepalived /usr/sbin/
mkdir /etc/keepalived
在143主上编辑/etc/keepalived/keepalived.conf写入文件内容:
!Configuration File forkeepalivedglobal_defs{ notification_email { wgkgood@139.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.223.100 } } virtual_server 192.168.223.100 3306 { delay_loop 6 lb_algo wrr lb_kind DR persistence_timeout 60 protocol TCP real_server 192.168.223.143 3306 { weight 100 notify_down /data/sh/mysql.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } }
上面的配置简单说明:
state BACKUP 表示为备模式,nopreempt 为不抢占,双方都配为备模式,并且不抢占,可以避免“脑裂”问题,priority 100 为权重,数字越高,权重越高,当双方都配为BACKUP模式,并且配置nopreempt时,keepalived靠这个数字来判断谁是主,谁是备
创建/data/sh/mysql.sh脚本
mkdir /data/sh
vim /data/sh/mysql.sh 加入以下内容:
#!/bin/bash
/etc/init.d/keepalived stop
chmod 755 /data/sh/mysql.sh
modprobe ip_vs #加载ip_vs模块 虚拟IP要用
lsmod | grep ip_vs 查看ip_vs模块有没有加载,如果看到下面的内容,就表示加载成功:
/etc/init.d/mysqld start #启动mysql
/etc/init.d/keepalive start #启动keepalived
在144上的操作和143上一样,只是keepalived.conf配置文件里priority 100 改为 priority 90
real_server 192.168.223.143 改为 real_server 192.168.223.144,其他不变。
查看VIP情况命令: ip addr list
至此mysql的主从高可以就做好了,可以在143上测试挺掉mysql服务,看看vip会不会漂移到144上,一般来说都是没问题的,如果有问题,请检查你的配置,步骤是不是有错误,还有selinux,防火墙是否关闭等