新聞中心
本篇文章為大家展示了MySQL中的MHA怎么用,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
提到MySQL高可用性,很多人會想到,亦或者+,不過這些方案的復(fù)雜性常常讓人望而卻步,與之相對,利用MySQL復(fù)制實現(xiàn)高可用性則顯得容易很多,目前大致有,,等方案可供選擇:MMM是最常見的方案,可惜它問題太多(,);至于PRM,它還是個新項目,暫時不推薦用于產(chǎn)品環(huán)境,不過作為的作品,它值得期待;如此看來目前只能選MHA了,好在經(jīng)過大規(guī)模的實踐應(yīng)用證明它是個靠譜的工具。
安裝:
作為前提條件,應(yīng)先配置MySQL復(fù)制,并設(shè)置SSH公鑰免密碼登錄。下面以CentOS為例來說明,好先安裝EPEL,不然YUM可能找不到某些軟件包。
MHA由Node和Manager組成,Node運行在每一臺MySQL服務(wù)器上,也就是說,不管是MySQL主服務(wù)器,還是MySQL從服務(wù)器,都要安裝Node,而Manager通常運行在獨立的服務(wù)器上,但如果硬件資源吃緊,也可以用一臺MySQL從服務(wù)器來兼職Manager的角色。
安裝Node:
shell> yum install perl-DBD-MySQL
shell> rpm -Uvh http://mysql-master-ha.googlecode.com/files/mha4mysql-node-0.52-0.noarch.rpm安裝Manager:
shell> yum install perl-DBD-MySQL
shell> yum install perl-Config-Tiny
shell> yum install perl-Log-Dispatch
shell> yum install perl-Parallel-ForkManager
shell> rpm -Uvh http://mysql-master-ha.googlecode.com/files/mha4mysql-node-0.52-0.noarch.rpm
shell> rpm -Uvh http://mysql-master-ha.googlecode.com/files/mha4mysql-manager-0.52-0.noarch.rpm配置:
配置全局設(shè)置:
shell> cat /etc/masterha_default.cnf
[server default]
user=...
password=...
ssh_user=...配置應(yīng)用設(shè)置:
shell> cat /etc/masterha_application.cnf
[server_1]
hostname=...
[server_2]
hostname=...注:MHA配置文件中參數(shù)的詳細(xì)介紹請參考官方文檔。
檢查
檢查MySQL復(fù)制:
shell> masterha_check_repl --conf=/etc/masterha_application.cnf檢查SSH公鑰免密碼登錄:
shell> masterha_check_ssh --conf=/etc/masterha_application.cnf實戰(zhàn)
首先啟動MHA進程:
shell> masterha_manager --conf=/etc/masterha_application.cnf注:視配置情況而定,可能會提示read_only,relay_log_purge等警告信息。
然后檢查MHA狀態(tài):
shell> masterha_check_status --conf=/etc/masterha_application.cnf注:如果正常,會顯示『PING_OK』,否則會顯示『NOT_RUNNING』。
到此為止,一個基本的MHA例子就能正常運轉(zhuǎn)了,不過一旦當(dāng)前的MySQL主服務(wù)器發(fā)生故障,MHA把某臺MySQL從服務(wù)器提升為新的MySQL主服務(wù)器后,如何通知應(yīng)用呢?這就需要在配置文件里加上如下兩個參數(shù):
master_ip_failover_script
master_ip_online_change_script
說到Failover,通常有兩種方式:一種是虛擬IP地址,一種是全局配置文件。MHA并沒有限定使用哪一種方式,而是讓用戶自己選擇,虛擬IP地址的方式會牽扯到其它的軟件,這里就不贅述了,以下簡單說說全局配置文件,以PHP為實現(xiàn)語言,代碼如下:
#!/usr/bin/env
$longopts = array(
'command:',
'ssh_user:',
'orig_master_host:',
'orig_master_ip:',
'orig_master_port:',
'new_master_host::',
'new_master_ip::',
'new_master_port::',
);
$options = getopt(null, $longopts);
if ($options['command'] == 'start') {
$params = array(
'ip' => $options['new_master_ip'],
'port' => $options['new_master_port'],
);
$string = '';
file_put_contents('config.php', $string, LOCK_EX);
}
exit(0);
?>注:用其它語言實現(xiàn)這個腳本也是OK的,最后別忘了給腳本加上可執(zhí)行屬性。
如果要測試效果的話,可以kill掉當(dāng)前的MySQL主服務(wù)器,稍等片刻,MHA就會把某臺MySQL從服務(wù)器提升為新的MySQL主服務(wù)器,并調(diào)用master_ip_failover_script腳本,如上所示,我們在master_ip_failover_script腳本里可以把新的MySQL主服務(wù)器的ip和port信息持久化到配置文件里,這樣應(yīng)用就可以使用新的配置了。
有時候需要手動切換MySQL主服務(wù)器,可以使用masterha_master_switch命令,不過它調(diào)用的不是master_ip_failover_script腳本,而是master_ip_online_change_script腳本,但調(diào)用參數(shù)類似,腳本可以互用。
shell> masterha_master_switch --conf=/etc/masterha_application.cnf --master_state=dead --dead_master_host=...
shell> masterha_master_switch --conf=/etc/masterha_application.cnf --master_state=alive --new_master_host=...注:針對原來的MySQL主服務(wù)器是否已經(jīng)宕機,執(zhí)行命令所需的參數(shù)有所不同。
需要說明的是,缺省情況下,如果MHA檢測到連續(xù)發(fā)生宕機,且兩次宕機時間間隔不足八小時的話,則不會進行Failover,之所以這樣限制是為了避免ping-pong效應(yīng)。不過為了自動化,我們往往希望能取消這種限制,此時可以用如下方式啟動Manager:
shell> nohup masterha_manager --conf=/etc/masterha_application.cnf --ignore_last_failover --remove_dead_master_conf &注:請確保Manager的運行用戶對masterha_application.cnf有寫權(quán)限
上述內(nèi)容就是MySQL中的MHA怎么用,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享名稱:MySQL中的MHA怎么用-創(chuàng)新互聯(lián)
瀏覽地址:http://www.ef60e0e.cn/article/dggoop.html