LAMP-5:MHA高可用(mysql集群)

文章目录

  • 1.简介
  • 2. 搭建MHA
    • 2.1 搭建一个一主两从的群组
    • 2.2 配置server4管理节点
  • 3. MHA高可用
    • 3.1 手动迁移
    • 3.2 自动迁移

1.简介

  • MHA(Master High Availability)目前在 MySQL 高可用方面是一个相对成熟的解决方案,是一套优秀的作为 MySQL 高可用性环境下故障切换和主从提升的高可用软件。
  • MHA分为管理节点(manage)和数据节点(node)
  • 高可用体现在Mysql出现故障切换时,只需不到30秒即可完成,而且能巨大程度上保持数据的一致性
  • 在一台master出现故障时,manage可以迅速将一台slave升级成master,并将其他salve指向新的master
  • 本文实验是搭建一个单组复制组的集群架构,由server4作为MHA管理节点,server1作为复制组的master节点,server2,3作为slave节点。
    LAMP-5:MHA高可用(mysql集群)_第1张图片

2. 搭建MHA

2.1 搭建一个一主两从的群组

首先删data/,修改配置文件my.cnf,初始化:mysqld --initialize --user=mysql,启动mysql,mysql_secure_installation
server1:

[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0

server_id=1
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW

LAMP-5:MHA高可用(mysql集群)_第2张图片

mysql> grant replication slave on *.* to repl@'%' identified by 'lee';
Query OK, 0 rows affected, 1 warning (0.01 sec)

server2:

[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0

disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
server_id=2
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
mysql> CHANGE MASTER TO MASTER_HOST='172.25.38.1',MASTER_USER='repl',MASTER_PASSWORD='lee',MASTER_AUTO_POSITION=1;
Query OK, 0 rows affected, 2 warnings (0.03 sec)

mysql> start slave;
Query OK, 0 rows affected (0.04 sec)

LAMP-5:MHA高可用(mysql集群)_第3张图片

server3:

[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0

disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
server_id=3
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
mysql> CHANGE MASTER TO MASTER_HOST='172.25.38.1',MASTER_USER='repl',MASTER_PASSWORD='lee',MASTER_AUTO_POSITION=1;
Query OK, 0 rows affected, 2 warnings (0.03 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

LAMP-5:MHA高可用(mysql集群)_第4张图片

2.2 配置server4管理节点

准备文件:
LAMP-5:MHA高可用(mysql集群)_第5张图片
yum install -y *.rpm
在server1,2,3上;yum install -y mha4mysql-node-0.58-0.el7.centos.noarch.rpm
在server4上,获取模板文件:tar zxf mha4mysql-manager-0.58.tar.gz
建立目录:mkdir /etc/masterha
进入模板目录:cd mha4mysql-manager-0.58/samples/conf/
导入至新建目录:cat masterha_default.cnf app1.cnf > /etc/masterha/app.cnf
新建目录:mkdir /etc/masterha/app1
编辑app.cnf:

[server default]
user=root
password=lee
ssh_user=root
repl_user=repl
repl_password=lee
master_binlog_dir= /usr/local/mysql/data
remote_workdir=/tmp
secondary_check_script= masterha_secondary_check -s 172.25.38.1 -s 172.25.38.2
ping_interval=3
# master_ip_failover_script= /script/masterha/master_ip_failover
# shutdown_script= /script/masterha/power_manager
# report_script= /script/masterha/send_report
# master_ip_online_change_script= /script/masterha/master_ip_online_change
manager_workdir=/etc/masterha/app1
manager_log=/etc/masterha/app1/manager.log

[server1]
hostname=172.25.38.1

[server2]
hostname=172.25.38.2
candidate_master=1

[server3]
hostname=172.25.38.3
no_master=1                     

做节点之间互相的免密,完成后执行脚本检测:masterha_check_ssh --conf=/etc/masterha/app.cnf

进入server1,给予root用户远程权限:

mysql> grant all on *.* to root@'%' identified by 'lee';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

检测用户权限:masterha_check_repl --conf=/etc/masterha/app.cnf
LAMP-5:MHA高可用(mysql集群)_第6张图片

3. MHA高可用

MHA的故障切换过程,共包括以下的步骤:

  • 配置文件检查阶段,这个阶段会检查整个集群配置文件的信息
  • 宕机的master处理,这个阶段包括虚拟ip摘除操作,主机关机操作
  • 复制dead master和最新slave相差的relay log,并保存到MHA Manager具体的目录下
  • 识别含有最新更新的slave
  • 应用从master保存的二进制日志事件(binlog events)
  • 提升一个slave作为新的master进行复制
  • 使其他的slave连接新的master进行复制

3.1 手动迁移

手动迁移master,从server1迁移到server2上:

masterha_master_switch --conf=/etc/masterha/app.cnf --master_state=alive --new_master_host=172.25.38.2 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=10000

LAMP-5:MHA高可用(mysql集群)_第7张图片
此时,server1由master转为指向server2的slave:
LAMP-5:MHA高可用(mysql集群)_第8张图片
模拟server2的master dead,进行master转移
手动down掉作为master的server2:/etc/init.d/mysqld stop
在server4上执行master迁移:

masterha_master_switch --master_state=dead --conf=/etc/masterha/app.cnf --dead_master_host=172.25.38.2 --dead_master_port=3306 --new_master_host=172.25.38.1 --new_master_port=3306 --ignore_last_failover

LAMP-5:MHA高可用(mysql集群)_第9张图片
迁移成功,此时master节点又回到了server1,
LAMP-5:MHA高可用(mysql集群)_第10张图片
若要重新加回stop的server2,需要:/etc/init.d/mysqld start
再进入数据库指定master节点:CHANGE MASTER TO MASTER_HOST='172.25.38.1',MASTER_USER='repl',MASTER_PASSWORD='lee',MASTER_AUTO_POSITION=1;
打开slave:start slave;
LAMP-5:MHA高可用(mysql集群)_第11张图片

3.2 自动迁移

准备两个脚本,修改其中地址,并给予权限chmod +x *:

cat /usr/local/bin/master_ip_failover

#!/usr/bin/env perl
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 = '172.25.38.100/24';
my $ssh_start_vip = "/sbin/ip addr add $vip dev eth0";
my $ssh_stop_vip = "/sbin/ip addr del $vip dev eth0";

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();
            $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 $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";
}

cat master_ip_online_change

#!/usr/bin/env perl
use strict;  
use warnings FATAL =>'all';  
  
use Getopt::Long;  
  
my $vip = '172.25.38.100/24';
my $ssh_start_vip = "/sbin/ip addr add $vip dev eth0";  
my $ssh_stop_vip = "/sbin/ip addr del $vip dev eth0";  
my $exit_code = 0;  
  
my (  
  $command,              $orig_master_is_new_slave, $orig_master_host,  
  $orig_master_ip,       $orig_master_port,         $orig_master_user,  
  $orig_master_password, $orig_master_ssh_user,     $new_master_host,  
  $new_master_ip,        $new_master_port,          $new_master_user,  
  $new_master_password,  $new_master_ssh_user,  
);  
GetOptions(  
  'command=s'                => \$command,  
  'orig_master_is_new_slave' => \$orig_master_is_new_slave,  
  'orig_master_host=s'       => \$orig_master_host,  
  'orig_master_ip=s'         => \$orig_master_ip,  
  'orig_master_port=i'       => \$orig_master_port,  
  'orig_master_user=s'       => \$orig_master_user,  
  'orig_master_password=s'   => \$orig_master_password,  
  'orig_master_ssh_user=s'   => \$orig_master_ssh_user,  
  '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,  
  'new_master_ssh_user=s'    => \$new_master_ssh_user,  
);  
  
  
exit &main();  
  
sub main {  
  
#print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";  
  
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 {  
            print "\n\n\n***************************************************************\n";  
            print "Disabling the VIP - $vip on old master: $orig_master_host\n";  
            print "***************************************************************\n\n\n\n";  
&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 {  
            print "\n\n\n***************************************************************\n";  
            print "Enabling the VIP - $vip on new master: $new_master_host \n";  
            print "***************************************************************\n\n\n\n";  
&start_vip();  
            $exit_code = 0;  
        };  
        if ($@) {  
            warn $@;  
            exit $exit_code;  
        }  
        exit $exit_code;  
}  
elsif ( $command eq "status" ) {  
        print "Checking the Status of the script.. OK \n";  
        `ssh $orig_master_ssh_user\@$orig_master_host \" $ssh_start_vip \"`;  
        exit 0;  
}  
else {  
&usage();  
        exit 1;  
}  
}  
  
# A simple system call that enable the VIP on the new master  
sub start_vip() {  
`ssh $new_master_ssh_user\@$new_master_host \" $ssh_start_vip \"`;  
}  
# A simple system call that disable the VIP on the old_master  
sub stop_vip() {  
`ssh $orig_master_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";  
}

打开配置文件中的相关部分:
LAMP-5:MHA高可用(mysql集群)_第12张图片
在server2上添加一个网段;ip addr add 172.25.38.100/24 dev eth0
在server1上测试可以成功登陆:
LAMP-5:MHA高可用(mysql集群)_第13张图片

master节点设置为server1,不是的话执行:CHANGE MASTER TO MASTER_HOST='172.25.38.1',MASTER_USER='repl',MASTER_PASSWORD='lee',MASTER_AUTO_POSITION=1;

在管理节点manager上打开MHA并后台执行:masterha_manager --conf=/etc/masterha/app.cnf &
LAMP-5:MHA高可用(mysql集群)_第14张图片
此时,stop掉server1的master,MHA控制下的master将自动落到备份master也就是server2上;
将server启动,重新指定其指向的master为serer2,即可
LAMP-5:MHA高可用(mysql集群)_第15张图片

你可能感兴趣的:(Linux,LAMP)