- MHA Manager(管理節點)
——可以單獨部署在一臺獨立的機器上,管理其他節點
——也可以試部署在一臺slave節點上
- MHA Node(數據節點)
——運行在每臺MySQL服務器上
MHA工作過程
由Manager定時探測集羣中的master節點
當master故障時,master自動將擁有最新數據的slave提升爲新的master
- 關鍵點
- 從宕機崩潰的master保存二進制日誌事件
- 識別含有最新更新的slave
- 應用差異的中繼日誌(relay log)到其他的slave
- 應用從master保存的二進制日誌事件
- 提升一個slave爲新的master
- 使其他的slave連接新的master進行復制
拓撲圖:
一、配置所有數據節點主機之間可以互相以ssh密鑰對方式認證登陸
二、配置manager56主機 無密碼ssh登錄 所有數據節點主機
三、配置主從同步,要求如下:
51 主庫 開半同步複製
52 從庫(備用主庫) 開半同步複製
53 從庫(備用主庫) 開半同步複製
54 從庫 不做備用主庫所以不用開半同步複製
55 從庫 不做備用主庫所以不用開半同步複製
不讓從庫把中繼日誌刪掉,默認會刪掉
mysql> set global relay_log_purge=off;
Query OK, 0 rows affected (0.13 sec)
配置數據庫服務器:(51~55)
yum -y install perl-DBD-mysql // 安裝依賴包
rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
配置管理服務器:(56)
yum -y install perl-DBD-mysql // 安裝依賴包
rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm //先裝Noed在安裝Manager
yum -y install perl-ExtUtils-* perl-CPAN-*
tar -zxvf mha4mysql-manager-0.56.tar.gz //源碼安裝Manager
cd mha4mysql-manager-0.56/
perl Makefile.PL
make
make install
[root@mha ~]# mkdir /etc/mha_manager/
[root@mha ~]# cp /opt/mha4mysql-manager-0.56/samples/conf/app1.cnf /etc/mha_manager/
[root@mha ~]# cp /opt/mha4mysql-manager-0.56/samples/conf/master_ip_failover /etc/mha_manager/
[root@mha ~]# vim /etc/mha_manager/app1.cnf //修改MHA配置文件
[server default]
manager_workdir=/etc/mha_manager //定義軟件路徑
manager_log=/etc/mha_manager/manager.log //指定log日誌位置
master_ip_failover_script=/etc/mha_manager/master_ip_failover //自動failover的切換腳本
ssh_user=root //ssh用戶名
ssh_port=22
repl_user=replication //主從同步用戶名
repl_password=123qqq...A // 主從密碼
user=root // 數據庫用戶名
password=123qqq...A // 數據庫密碼
[server1] //添加管理主機
hostname=192.168.4.50
candidate_master=1 //設置爲候選master
[server2]
hostname=192.168.4.51
candidate_master=1
[server3]
hostname=192.168.4.52
candidate_master=1
[server4]
hostname=192.168.4.53
no_master=1 //不競選master
[server5]
hostname=192.168.4.54
no_master=1
添加授權用戶: //用於MHA檢測集羣狀態
mysql> grant all on *.* to root@"%" identified by "123qqq...A";
Query OK, 0 rows affected (0.10 sec)
添加授權用戶: //用於主從授權
grant replication slave on *.* to replication@"%" identified by "123qqq...A";
Query OK, 0 rows affected (0.12 sec)
測試:
SSH測試:
[root@mha ~]# masterha_check_ssh --conf=/etc/mha_manager/app1.cnf
主從配置測試:
[root@mha ~]# masterha_check_repl --conf=/etc/mha_manager/app1.cnf
編輯VIP轉換腳本: vim /etc/mha_manager/master_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;
use MHA::DBHelper;
my (
$command, $ssh_user, $orig_master_host,
$orig_master_ip, $orig_master_port, $new_master_host,
$new_master_ip, $new_master_port, $new_master_user,
$new_master_password
);
my $vip = '192.168.4.100/24'; # Virtual IP
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
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,
'new_master_user=s' => \$new_master_user,
'new_master_password=s' => \$new_master_password,
);
exit &main();
sub main {
if ( $command eq "stop" || $command eq "stopssh" ) {
# $orig_master_host, $orig_master_ip, $orig_master_port are passed.
# If you manage master ip address at global catalog database,
# invalidate orig_master_ip here.
my $exit_code = 1;
eval {
# updating global catalog, etc
&stop_vip();
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) {
# all arguments are passed.
# If you manage master ip address at global catalog database,
# activate new_master_ip here.
# You can also grant write access (create user, set read_only=0, etc) here.
my $exit_code = 10;
eval {
my $new_master_handler = new MHA::DBHelper();
# args: hostname, port, user, password, raise_error_or_not
$new_master_handler->connect( $new_master_ip, $new_master_port,
$new_master_user, $new_master_password, 1 );
## Set read_only=0 on the new master
$new_master_handler->disable_log_bin_local();
print "Set read_only=0 on the new master.\n";
$new_master_handler->disable_read_only();
## Creating an app user on the new master
print "Creating app user on the new master..\n";
$new_master_handler->enable_log_bin_local();
$new_master_handler->disconnect();
## Update master ip on the catalog database, etc
&start_vip();
$exit_code = 0;
};
if ($@) {
warn $@;
# If you want to continue failover, exit 10.
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
# do nothing
exit 0;
}
else {
&usage();
exit 1;
}
}
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
sub stop_vip() {
return 0 unless ($ssh_user);
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
print
"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";
}
啓動MHA:
[root@mha ~] # masterha_manager --conf=/etc/mha_manager/app1.cnf --remove_dead_master_conf --ignore_last_failover
--remove_dead_master_conf //當主庫壞掉,會自動將這個數據庫踢出集羣,在配置文件app1.cnf中將主庫的配置刪除,當你修復好之後需要在配置文件app1.cnf中添加回去
--ignore_last_failover // 默認一臺壞掉另外一臺頂上去,在8個小時之內又壞了,就不會再切換主機了,所以需要把這個關掉
[root@mha ~] # masterha_check_status --conf=/etc/mha_manager/app1.cnf //查看服務器狀態
停止master50,主庫會動態遷移
將被刪除的50添加到集羣
mysql>change master to master_host="192.168.4.51",master_user="replication",master_password="123qqq...A",master_log_file="master51.000003",master_log_pos=154; // 將50設置爲現在的主庫51的從庫
mysql> start slave;
mysql> show slave status\G;
在56中將刪除的50信息添加回去
]# vim /etc/mha_manager/app1.cnf
[server1]
candidate_master=1
hostname=192.168.4.50