1. 程式人生 > 其它 >MHA高可用架構

MHA高可用架構

MHA架構圖

一:資源規劃:

主機名系統IP伺服器角色備註ssh互通
manage-mysql centos 7 192.168.205.188 MHA管理端 監控各節點 ssh免密互通
master-mysql centos 7 192.168.205.189 主資料庫 開啟bin-log,relay-log ssh免密互通
slave1-mysql centos 7 192.168.205.190 從資料庫 開啟bin-log,relay-log ssh免密互通
slave2-mysql centos 7 192.168.205.191 從資料庫 開啟bin-log,relay-log
ssh免密互通

二:搭建一主二從架構:(資料庫主從搭建略)

如果使用gtid複製需在配置檔案中開啟,在從伺服器上執行

change master to master_host='192.168.205.189',master_user='repl',master_password='111111',master_auto_position=1;

需要注意以下幾點

1. uuid每個節點需要唯一,不然主從複製會失敗

 cat /var/lib/mysql/auto.cnf 

2. 開啟gtid複製(更高效,本例中主從複製仍然採用binglog日誌同步)

3. 給每節點授權一個管理賬號(主節點授權即可,其他節點會自動同步)

grant all on . to 'mhaadmin'@'%' identified by '111111'; 

4. 在4個節點上安裝以下軟體包

yum install epel-release -y
yum -y install mha4mysql-node-0.54-0.el6.noarch.rpm

 在管理節點上安裝mha4mysql-manage

yum -y install mha4mysql-manager-0.54-0.el6.noarch.rpm

5. 定義 MHA 管理配置檔案

mkdir /etc/mha_master 
vim /etc/mha_master/mha.cnf
​[server default]
user=mhaadmin
password=111111
manager_workdir=/etc/mha_master/workspace
manager_log=/etc/mha_master/manager.log
remote_workdir=/mydata/mha_master/workspace
ssh_user=root
repl_user=repl
repl_password=111111
ping_interval=1
[server1]
hostname=192.168.205.189
ssh_port=22
candidate_master=1
[server2]
hostname=192.168.205.190
ssh_port=22
candidate_master=1
[server3]
hostname=192.168.205.191
ssh_port=22
candidate_master=1  

6. 檢測各節點間 ssh 互信通訊配置是否 ok(基本環境已經配置)

  在 Manager 機器上輸入下述命令來檢測:

 masterha_check_ssh -conf=/etc/mha_master/mha.cnf  

7. 檢查管理的MySQL複製叢集的連線配置引數是否OK(注:複製賬號repl需要在mysql各個節點上授權不然檢測會報錯)

grant replication slave on *.* to 'repl'@'%'  identified by '111111';
masterha_check_repl -conf=/etc/mha_master/mha.cnf

8. MHA資料庫切換指令碼

vim /usr/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
);
#定義VIP變數
my $vip = '192.168.205.200/24';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$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,
);
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";
}  

9. 授予指令碼執行許可權

chmod a+x /usr/bin/master_ip_failover

在配置檔案中加入指令碼配置檔案路徑

vim /etc/mha_master/mha.cnf

10. 啟動manager

nohup masterha_manager -conf=/etc/mha_master/mha.cnf 2>&1 > /etc/mha_master/manager.log &

檢視後臺執行任務

jobs -l

11. 檢視MHA狀態(目前192.168.205.190這個ip為主庫)

masterha_check_status  --conf=/etc/mha_master/mha.cnf 

第一次配置VIP時,需要手動新增主庫的虛擬IP,現在主庫是192.168.205.190這個主機,所以在這個主機上加第一個VIP地址

ifconfig ens33:1 192.168.205.200/24

三:現在我們人為重現主庫故障,停止192.168.205.190這個主機mysql服務

systemctl stop mysqld  

我們發現vip介面地址已經不在此節點上

現在我們看下manager日誌資訊,master已切換到189這個主機上

還有一個很重要的資訊,原有主機down掉後面恢復後變為從,需要手動去同步。裡面有條語句很重要需要在原有主庫恢復後執行

All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.205.189', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000005'
, MASTER_LOG_POS=234, MASTER_USER='repl', MASTER_PASSWORD='xxx';

備註:MASTER_PASSWORD='xxx'; 是複製賬號密碼MASTER_PASSWORD='111111'

看下ip地址vip已將切換到189這臺主機上

到191這臺主機節點檢視mysql相關複製資訊發現master已經切換成功

登入190這臺主機,啟動mysql服務並連線mysql執行以下語句

CHANGE MASTER TO MASTER_HOST='192.168.205.189', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000005'
, MASTER_LOG_POS=234, MASTER_USER='repl', MASTER_PASSWORD='111111';
start slave;

檢視同步情況

show slave status\G

  

到此所有功能已恢復,最後我們需要重新啟動manager指令碼監控即可。

nohup masterha_manager -conf=/etc/mha_master/mha.cnf 2>&1 > /etc/mha_master/manager.log &