- 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