使用6台RHEL 7虚拟机,如图所示。准备集群环境,安装依赖包,授权用户,配置ssh密钥对认证登陆,所有节点之间互相以root秘钥对认证登录,管理主机以root密钥对认证登录所有数据节点主机,配置mha集群。
IP规划
一,所有主机装包(51~56)
yum -y install perl-*
yum -y install /root/mha-soft-student/perl-*.rpm
yum -y install /root/mha-soft-student/mha4mysql-node-0.56-0.el6.noarch.rpm
二,在管理主机上安装mha4mysql-manager(56主机)
[root@ip56 ~]# tar -xf /root/mha-soft-student/mha4mysql-manager-0.56.tar.gz
[root@ip56 ~]# cd mha4mysql-manager-0.56/
[root@ip56 mha4mysql-manager-0.56]# perl Makefile.PL
[root@ip56 mha4mysql-manager-0.56]# make && make install
manager节点提供的命令工具
命令 | 作用 |
---|---|
masterha_check_ssh | 检查MHA的SSH配置状况 |
masterha_check_perl | 检查MySQL复制状况 |
masterha_manager | 启动MHA |
masterha_check_status | 检测MHA运行状态 |
masterha_master_monitor | 检查master是否宕机 |
三,配置所有节点之间可以互相无密码登录(51~55),管理主机(56)可以无密码登录所有节点主机(51~55)
四,配置MHA集群
(1)配置主服务器51
1)修改配置文件
[root@ip51 ~]# vim /etc/my.cnf
[mysqld]
plugin-load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl-semi-sync-master-enabled = 1
rpl-semi-sync-slave-enabled = 1
server_id=51
log-bin=master51
binlog-format="mixed"
... ...
[root@ip51 ~]# systemctl restart mysqld
2)登录数据库,添加主从同步授权用户
[root@ip51 ~]# mysql -uroot -p123456
mysql> set global relay_log_purge=off; //不自动删除本机的中继日志文件
mysql> grant replication slave on *.* to plj@"%" identified by "123qqq...A";
1)修改配置文件
[root@ip52 ~]# vim /etc/my.cnf
[mysqld]
log-bin=master52
server_id=52
binlog-format=mixed
plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl_semi_sync_master_enabled=1
rpl_semi_sync_slave_enabled=1
relay_log_purge=off
... ....
[root@ip52 ~]# systemctl restart mysqld
2)登录数据库添加主库授权用户信息
[root@ip52 ~]# mysql -uroot -p123456
mysql> change master to master_host="192.168.4.51",master_user="plj",master_password="123qqq...A",master_log_file="master51.000003",master_log_pos=436;
mysql> start slave;
mysql> show slave status\G;//查看启用状态
1)修改配置文件
[root@ip53 ~]# vim /etc/my.cnf
[mysqld]
log-bin=master53
server_id=53
binlog-format=mixed
plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl_semi_sync_master_enabled=1
rpl_semi_sync_slave_enabled=1
relay_log_purge=off
... ....
[root@ip53 ~]# systemctl restart mysqld
2)登录数据库添加主库授权用户信息
[root@ip53 ~]# mysql -uroot -p123456
mysql> change master to master_host="192.168.4.51",master_user="plj",master_password="123qqq...A",master_log_file="master51.000003",master_log_pos=436;
mysql> start slave;
mysql> show slave status\G;//查看启用状态
1)修改配置文件
[root@ip54 ~]# vim /etc/my.cnf
[mysqld]
server_id=54
plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl_semi_sync_master_enabled=1
rpl_semi_sync_slave_enabled=1
relay_log_purge=off
... ...
[root@ip54 ~]# systemctl restart mysqld
2)登录数据库添加授权用户信息
[root@ip54 ~]# mysql -uroot -p123456
mysql> change master to master_host="192.168.4.51",master_user="plj",master_password="123qqq...A",master_log_file="master51.000003",master_log_pos=436;
mysql> start slave;
mysql> show slave status\G;
1)修改配置文件
[root@ip55 ~]# vim /etc/my.cnf
[mysqld]
server_id=55
plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl_semi_sync_master_enabled=1
rpl_semi_sync_slave_enabled=1
relay_log_purge=off
... ...
[root@ip54 ~]# systemctl restart mysqld
2)登录数据库添加授权用户信息
[root@ip55 ~]# mysql -uroot -p123456
mysql> change master to master_host="192.168.4.51",master_user="plj",master_password="123qqq...A",master_log_file="master51.000003",master_log_pos=436;
mysql> start slave;
mysql> show slave status\G;
(1)创建工作目录,撰写配置文件
[root@ip56 ~]# mkdir /etc/mha_manager
[root@ip56 ~]# vim /etc/mha_manager/app1.cnf
[server default]
manager_workdir=/etc/mha_manager
manager_log=/etc/mha_manager/manager.log
master_ip_failover_script=/etc/mha_manager/master_ip_failover //自动failover的切换脚本
ssh_user=root
ssh_port=22
repl_user=plj //主从同步用户名
repl_password=123qqq...A //主从同步密码
user=root //数据库用户名
password=123456 //密码
[server1]
hostname=192.168.4.51
port=3306
candidate_master=1 //设置为候选master
[server2]
hostname=192.168.4.52
port=3306
candidate_master=1 //设置为候选master
[server3]
hostname=192.168.4.53
port=3306
candidate_master=1 //设置为候选master
[server4]
hostname=192.168.4.54
port=3306
no_master=1 //不竞选master
[server5]
hostname=192.168.4.55
port=3306
no_master=1 //不竞选master
(2)撰写故障切换脚本
[root@ip56 ~]# 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";
}
[root@ip56 ~]# chmod +x /etc/mha_manager/master_ip_failover //脚本添加执行权限
(3)51主机上配置VIP
[root@ip51 ~]# ifconfig eth0:1 192.168.4.100/24
(4)52,53主机上创建配置文件里边的监视用用户(root),56用来监视数据库服务器状态,(主从同步用户)
[root@ip52 ~]# mysql -uroot -p123456
mysql> grant replication slave on *.* to plj@"%" identified by "123qqq...A";
[root@ip53 ~]# mysql -uroot -p123456
mysql> grant replication slave on *.* to plj@"%" identified by "123qqq...A";
(5)51主库上创建配置文件中的数据库用户
[root@ip51 ~]# mysql -uroot -p123456
mysql> grant all on *.* to root@"%" identified by "123456";
五、测试集群配置
[root@ip56 ~]# masterha_check_ssh --conf=/etc/mha_manager/app1.cnf
[root@ip56 ~]# masterha_check_repl --conf=/etc/mha_manager/app1.cnf
--remove_dead_master_conf
//删除宕机主库配置
--ignore_last_failover
//忽略xxx.health文件
[root@ip56 ~]# masterha_manager --conf=/etc/mha_manager/app1.cnf --remove_dead_master_conf --ignore_last_failover
2)查看状态
[root@ip56 ~]# masterha_check_status --conf=/etc/mha_manager/app1.cnf
3)停止服务
[root@ip56 ~]# masterha_stop --conf=/etc/mha_manager/app1.cnf
[root@ip51 ~]# mysql -uroot -p123456
mysql> create database db9;
mysql> use db9;
mysql> create table t1(name varchar(100));
mysql> grant select,insert on db9.* to yy99 identified by "123456";
( 2)在客户端50上,连接vip地址,使用该用户访问数据库
[root@ip50 ~]# mysql -h192.168.4.100 -uyy99 -p123456
mysql> insert into db9.t1 values("harry"),("bob");
(3)停止51的mysql服务,50客户端仍可以访问
[root@ip51 ~]# systemctl stop mysqld
停掉之后,监控管理器会退出监控状态,将主配置文件app1.cnf中的51主机的配置文件删除,并将51上的vip地址转配到下一个master主机上,其余从数据库服务器上的主服务器ip地址都会被改变
(4)恢复51主机后要重新修改监控管理器的主配置文件app1.cnf,添加51主机配置,并将51主机添加为从服务器
[root@ip56 ~]# vim /etc/mha_manager/app1.cnf
... ...
[server1]
candidate_master=1
hostname=192.168.4.51
port=3306
... ...
[root@ip51 ~]# systemctl restart mysqld
[root@ip51 ~]# mysql -uroot -p123456
mysql> change master to master_host="192.168.4.52",master_user="plj",master_password="123qqq...A",master_log_file="master52.000003";master_log_pos="154";
mysql> start slave;
mysql> show slave status\G;