資源描述:
《使用keepalived實現(xiàn)對mysql主從復(fù)制的主備自動切換》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、keepalived實現(xiàn)對mysql主從復(fù)制的主備自動切換使用MySQL+keepalived是一種非常好的解決方案,在MySQL-HA環(huán)境中,MySQL互為主從關(guān)系,這樣就保證了兩臺MySQL數(shù)據(jù)的一致性,然后用keepalived實現(xiàn)虛擬IP,通過keepalived自帶的服務(wù)監(jiān)控功能來實現(xiàn)MySQL故障時自動切換。實驗環(huán)境中用兩臺主機(jī)搭建了一個mysql主從復(fù)制的環(huán)境,兩臺機(jī)器分別安裝了keepalived,用一個虛IP實現(xiàn)mysql服務(wù)器的主備自動切換功能.模擬環(huán)境:VIP:192.168.1.197:虛擬IP地址Master
2、:192.168.1.198:主數(shù)據(jù)庫IP地址Slave:192.168.1.199:從數(shù)據(jù)庫IP地址備注:MySQL的主從同步配置不在此文檔中說明(前提:主從同步已完成)安裝步驟:1、keepalived的安裝Yuminstall-ykeepalivedChkconfigkeepalivedon2、keepalived.conf文件的配置Master:keepalived.confvi/etc/keepalived/keepalived.conf!ConfigurationFileforkeepalivedglobal_defs{n
3、otification_email{kenjin@xishiqu.com}notification_email_fromkenjin@xishiqu.comsmtp_connect_timeout3smtp_serversmtp.xishiqu.comrouter_idMySQL-ha}vrrp_scriptcheck_run{#聲明vrrp_script的函數(shù)check_runscript"/root/keepalived_check_mysql.sh"#監(jiān)控MySQL的腳本interval5}vrrp_sync_groupVG1{
4、group{VI_1}}vrrp_instanceVI_1{stateMASTER#指定主服務(wù)器節(jié)點為主節(jié)點,備用節(jié)點上設(shè)置需一致interfaceeth0#指定虛擬IP的網(wǎng)絡(luò)接口virtual_router_id88#VRRP組名,兩個節(jié)點的設(shè)置必須一樣,以指明各個節(jié)點屬于同一VRRP組priority100#主節(jié)點的優(yōu)先級(1-254之間),備用節(jié)點必須比主節(jié)點優(yōu)先級低。advert_int1#組播信息發(fā)送間隔,兩個節(jié)點設(shè)置必須一樣nopreemptauthentication{#設(shè)置驗證信息,兩個節(jié)點必須一致auth_typeP
5、ASSauth_pass1111}track_script{#調(diào)用函數(shù)代碼塊check_run}virtual_ipaddress{#虛擬IP(對外提供MySQL服務(wù)的IP地址)192.168.1.197#不能和網(wǎng)內(nèi)其他IP地址沖突}}Slave:keepalived.conf!ConfigurationFileforkeepalivedglobal_defs{notification_email{kenjin@xishiqu.com}notification_email_fromkenjin@xishiqu.comsmtp_conn
6、ect_timeout3smtp_serversmtp.xishiqu.cnrouter_idMySQL-ha}vrrp_scriptcheck_run{script"/root/keepalived_check_mysql.sh"interval5}vrrp_sync_groupVG1{group{VI_1}}vrrp_instanceVI_1{stateMASTERinterfaceeth1#本地網(wǎng)口按實際情況配置virtual_router_id88priority99advert_int1nopreemptauthentica
7、tion{auth_typePASSauth_pass1111}track_script{check_run}virtual_ipaddress{192.168.1.197}}3、mysql狀態(tài)檢測腳本在目錄root下創(chuàng)建腳本keepalived_check_mysql.sh:vim/root/keepalived_check_mysql.sh#!/bin/bashMYSQL=/usr/local/mysql/bin/mysqlMYSQL_HOST=localhostMYSQL_USER=rootMYSQL_PASSWORD=1234
8、56CHECK_TIME=3#mysqlisworkingMYSQL_OKis1,mysqldownMYSQL_OKis0MYSQL_OK=1functioncheck_mysql_helth(){$MYSQL-h$MY