兩臺機子,一臺是192.168.223.143作主,一臺是192.168.223.144主從,如今要實現的需求是兩臺機子作主從+高可用,VIP這裏設置爲192.168.223.100mysql
首先在兩臺機子上裝好mysql,作好主從複製,linux
參考http://pc1990.blog.51cto.com/10541224/1687171sql
2、在兩臺機子上安裝keepalivedvim
tar –zxvf keepalived-1.2.7.tar.gzbash
cd keepalived-1.2.7ide
yum install kernel-devel openssl-devel popt-devel -yoop
./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.i686/測試
make && make installspa
拷貝啓動腳本等router
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 forkeepalived global_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,防火牆是否關閉等