共計 4826 個字符,預(yù)計需要花費(fèi) 13 分鐘才能閱讀完成。
這篇文章主要為大家展示了“mysql 中 MHA 如何搭建”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓丸趣 TV 小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“mysql 中 MHA 如何搭建”這篇文章吧。
操作系統(tǒng):
debian5.0.2
數(shù)據(jù)庫版本:
mysql5.0.51a
結(jié)構(gòu):
manager : 192.168.1.136(M1)
master : 192.168.1.20:3306(N1)
slave1 : 192.168.1.20:3307(N2)
slave2 : 192.168.1.20:3308(N3)
slave3 : 192.168.1.20:3309(N4)
軟件包準(zhǔn)備:
mha4mysql-manager_0.55-1_all.deb(還有對應(yīng)的二進(jìn)制包,源碼包)
mha4mysql-node_0.54-1_all.deb
準(zhǔn)備就緒:
①安裝依賴包
apt-get install libdbd-mysql-perl
apt-get install libconfig-tiny-perl
apt-get install liblog-dispatch-perl
apt-get install libparallel-forkmanager-perl
centos 版(yum install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager)
②安裝 mha manager 包和 node 包
M1:dpkg -i mha4mysql-manager_0.55-1_all.deb
N1:dpkg -i mha4mysql-node_0.54-1_all.deb
N2:dpkg -i mha4mysql-node_0.54-1_all.deb
N3:dpkg -i mha4mysql-node_0.54-1_all.deb
N4:dpkg -i mha4mysql-node_0.54-1_all.deb
③主機(jī)信任 (由于公司環(huán)境的原因,這里使用的是 mysql 用戶)
M1:
#su mysql
#ssh-keygen -t rsa
#cat ~/.ssh/id_rsa.pub
將公鑰內(nèi)容添加到 N1,N2,N3,N4 的對應(yīng)用戶 (這里是 mysql 用戶) 下的./ssh/authorized_keys 下即可實現(xiàn) M1 到 N1,N2,N3,N4 的免密登錄。
另外,這里踩了個坑,就是免密登錄的 server 端 (N1,N2,N3,N4) 即 /etc/ssh/sshd_config 文件必須開啟公鑰登錄
免密登錄的 client 端 (M1) 即 /etc/ssh/ssh_config 文件不能關(guān)閉公鑰登錄(這個參數(shù)設(shè)置之后在公司環(huán)境下貌似還是不能免密登錄,最后用了默認(rèn)配置就可以了)
N1,N2,N3,N4 節(jié)點之間也互相信任
2016-12-4 22:44:12 ADD
ssh 客戶端得注釋以下參數(shù) 才能免密登錄
PreferredAuthentications password
③新建 mysql mha 管理賬戶,mysql repl 復(fù)制用戶
④新建 mha 工作目錄
M1:mkdir -p /opt/mha_manager/log chown -R mysql /opt/mha_manager
N1~N4:mkdir -p /opt/mha_node chown -R mysql /opt/mha_node
⑤配置 manager
M1:
#mkdir -p /etc/mha
#cat /etc/mha/app1.conf
manager_workdir=/opt/mha_manager_work
manager_log=/opt/mha_manager_work/log/app1.log
remote_workdir=/opt/mha_node
對以上的配置文件更改屬主為 ssh 信任賬戶(這里是 mysql)
另外(源碼安裝的 mysql 注意):
mha 遠(yuǎn)程登錄操作 mysql 的時候默認(rèn)用不到 /usr/local/mysql/bin 下面的 mysql 以及 mysqlbinlog 會報相應(yīng)的錯誤信息,
這個時候我們需要做如下操作
#ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog
#ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
④開啟 manager
這里要注意的是,不要后臺啟動,即不要以 masterha_manager –conf=/etc/mha/app1.conf 形式啟動,不然會報錯退出。我們前臺啟動,然后 ctrl+z , bg 放入后臺。
至此,整個搭建過程完畢。
注意事項:
①slave 最好設(shè)置 read_only=1 relay_log_purge=0, 利用定時任務(wù)刪除 relay_log;
②mha 進(jìn)行 failover 之后會在 app1.log 中記錄相應(yīng)的 chang master to master_xxx 等信息,因此我們可以通過這條信息手動將
所宕 master
連接到新主;
③可以使用 (masterha_master_switch –master_state=alive –conf=/etc/mha/app1.cnf) 進(jìn)行手動切換主,然后將新主手動添加作為老主的從(步驟 2),注意 mha manager 必須處于關(guān)閉狀態(tài)(masterha_check_status –conf=xxx);
④我們可以在 app1.conf 配置主庫宕機(jī)后 vip 的飄逸腳本(
master_ip_failover_script=/usr/bin/masterha_ip_failover
),但是第一次必須得自己手動在 master 添加 vip.
附 masterha_ip_failover 腳本
點擊 (此處) 折疊或打開
#!/usr/bin/env perl
# Copyright (C) 2011 DeNA Co.,Ltd.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
## Note: This is a sample script and is not complete. Modify the script based on your environment.
use strict;
use warnings FATAL = all
use Getopt::Long;
my (
$command, $ssh_user, $orig_master_host, $orig_master_ip,
$orig_master_port, $new_master_host, $new_master_ip, $new_master_port
);
my $vip = 192.168.1.111/24
my $key = 0
my $ssh_start_vip = sudo /sbin/ifconfig eth2:$key $vip
my $ssh_stop_vip = sudo /sbin/ifconfig eth2:$key down
#my $ssh_Bcast_arp = /usr/bin/arping -c 3 -A 10.1.99.233 #ARP 回復(fù)模式,更新鄰居。要是不加則服務(wù)器會自動等到 vip 緩存失效,期間 VIP 會有一定時間的不可用。
GetOptions(
command=s = \$command,
ssh_user=s = \$ssh_user,
orig_master_host=s = \$orig_master_host,
orig_master_ip=s = \$orig_master_ip,
orig_master_port=i = \$orig_master_port,
new_master_host=s = \$new_master_host,
new_master_ip=s = \$new_master_ip,
new_master_port=i = \$new_master_port,
);
exit main();
sub main {
print \n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n
if ($command eq stop || $command eq stopssh) {
my $exit_code = 1;
eval {
print Disabling the VIP on old master: $orig_master_host \n
stop_vip();
$exit_code = 0;
};
if ($@) {
warn Got Error: $@\n
exit $exit_code;
}
exit $exit_code;
}
elsif ($command eq start) {
my $exit_code = 10;
eval {
print Enabling the VIP – $vip on the new master – $new_master_host \n
start_vip();
# start_arp();
$exit_code = 0;
};
if ($@) {
warn $@;
exit $exit_code;
}
exit $exit_code;
}
elsif ($command eq status) {
print Checking the Status of the script.. OK \n
exit 0;
}
else {
usage();
exit 1;
}
}
sub start_vip() {
`ssh -p56000 $ssh_user\@$new_master_host \ $ssh_start_vip \
}
#sub start_arp() {
# `ssh $ssh_user\@$new_master_host \ $ssh_Bcast_arp \
#}
sub stop_vip() {
`ssh -p56000 $ssh_user\@$orig_master_host \ $ssh_stop_vip \
}
sub usage {
Usage: master_ip_failover –command=start|stop|stopssh|status –orig_master_host=host –orig_master_ip=ip –orig_master_port=port –new_master_host=host –new_master_ip=ip –new_master_port=port\n
}
以上是“mysql 中 MHA 如何搭建”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注丸趣 TV 行業(yè)資訊頻道!