mysql master/slave結構最大的缺點:當master宕機時不能切換到slave,這樣就影響了業務的運行。為了彌補這個缺陷,我採用了keepalived+master/master結構。
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/214425D02-0.png" title="QQ20130912141147.png" alt="141414707.png" />
關於mysql的配置請參考:http://liuping0906.blog.51cto.com/2516248/879877
安裝keepalived
tar zxf keepalived-1.2.8.tar.gzcd keepalived-1.2.8./configure --prefix=/opt/app/keepalived-1.2.8makemake install
在192.168.110.128上設定檔keepalived.conf
! Configuration File for keepalivedglobal_defs { router_id MYSQL-HA}vrrp_instance keepalive_mysql { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 preempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.110.8/24 }}
在192.168.110.137上設定檔keepalived.conf
! Configuration File for keepalivedglobal_defs { router_id MYSQL-HA}vrrp_instance keepalive_mysql { state BACKUP interface eth0 virtual_router_id 51 priority 99 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.110.8/24 }}
說明:
配置上注意priority的值,192.168.110.128是master,優先順序要高。
preempt表示搶佔服務,當192.168.110.128恢複後,重新把服務搶過來。
keepalived預設網卡eth0和自身服務出現問題時才會切換。
通過編寫指令碼來監控mysql的狀態進而控制keeepalived服務
#!/bin/bashMYSQL="/usr/bin/mysql"MYSQL_HOST=127.0.0.1MYSQL_USER=adminMYSQL_PASSWORD=adminLOG_FILE="/opt/app/keepalived/check_mysql.log"MYSQL_OK=1 # mysql is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0check_mysql_helth(){ $MYSQL -h$MYSQL_HOST -u$MYSQL_USER -p${MYSQL_PASSWORD} -e "show status" &>/dev/null if [ $? = 0 ] ; then MYSQL_OK=1 else MYSQL_OK=0 fi return $MYSQL_OK}while :do CHECK_TIME=3 while [ $CHECK_TIME -ne 0 ] do let "CHECK_TIME -= 1" check_mysql_helth [ $MYSQL_OK == 1 ] && break if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ]; then pkill keepalived echo `date --date=today +"%Y-%M-%d %H:%m:%S"` - [INFO] - mysql invaild. keepalived stop. >> $LOG_FILE fi donedone
說明:
通過監控mysql服務狀態來控制keepalived服務,這裡mysql停掉則kill掉keepalived。
分別在兩台機器上執行下面命令
shell>/opt/app/keepalived/sbin/keepalived -f \ /opt/app/keepalived/etc/keepalived/keepalived.conf -Dshell>nohup sh /opt/app/keepalived/bin/check_mysql.sh &
把第二條命令寫入/etc/rc.local。
測試:
> ip addr
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/214425LH-1.png" title="QQ20130912145934.png" alt="150148345.png" />
>killall mysqld
查看192.168.110.137
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/214425A57-2.png" title="QQ20130912150325.png" alt="150540468.png" />
此時192.168.110.137充當臨時伺服器。
恢複192.168.110.128上的業務後查看
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/214425B64-3.png" title="QQ20130912145934.png" alt="150719861.png" />
此時業務被192.168.110.128搶佔過來,192.168.110.137充當備份、臨時伺服器。
遺留的問題:
1、本來在keepalived使用vrrp_script和strack_script來監控這個指令碼,但是測試過幾遍指令碼都不運行。網上很多人都談到這個問題,我不知道其他人怎麼成功的。
2、當寫入資料時主伺服器宕機,從伺服器來不及同步資料。此時造成使用者資料寫入但不能讀取。緩解這個問題可以採用同步或者半同步。
3、主從切換時會造成業務閃斷。
4、對於第一個問題可以結合lvs來解決。
5、主鍵衝突問題
參考文檔
http://bbs.ywlm.net/thread-855-1-1.html
http://bbs.ywlm.net/thread-845-1-1.html
http://bbs.ywlm.net/thread-790-1-1.html
本文出自 “fly天地” 部落格,請務必保留此出處http://liuping0906.blog.51cto.com/2516248/1295092