CentOS 7 MariaDB-MHA

關於MHA

   MHA(Master High Availability)是一款開源的mysql高可用程序,目前在mysql高可用方面是一個相對成熟的解決方案。MHA 搭建的前提是MySQL集羣中已經搭建了MySql Replication環境,有了Master/Slave節點。MHA的主要做用就是監測到Master節點故障時會提高主從複製環境中擁有最新數據的Slave節點成爲新的master節點。同時,在切換master期間,MHA會經過從其餘的Slave節點來獲取額外的信息來避免一致性的問題,整個的切換過程對於應用程序而言是徹底透明的。MHA還提供了master節點在線切換功能,即按需切換master/slave節點node

MHA Manager 和 MHA Node

MHA 服務有兩個角色,MHA Manager 和 MHA Node 
   MHA Manager: 一般單獨部署在一臺獨立機器上管理 master/slave 集羣,每一個master/slave 集羣能夠理解爲一個application。mysql

   MHA Node: 運行在每臺mysql 服務器(master/slave)上。它經過監控具有解析和清理logs功能來加快故障轉移。linux

總體上的架構以下圖所示sql

MHA 在自動切換的過程當中會從宕掉的MySql master節點中保存二進制日誌,以保證數據的完整性。可是若是master節點直接宕機了呢,或者網絡直接不能聯通了呢?MHA就沒有辦法獲取master的二進制日誌,也就沒有辦法保證數據的完整性了。這也就是爲何MHA應該與MySql主從複製結合起來。這樣的話,只要有一個slave節點從master節點複製到了最新的日誌,MHA就能夠將最近的二進制日誌應用到其餘的slave節點上,這樣就能夠最大限度上保證數據的完整性。vim

MHA 自動切換的原理能夠總結爲下面幾點.服務器

  • 從宕機崩潰的master保存二進制日誌事件(binlog events);
  • 識別含有最新更新的slave;
  • 應用差別的中繼日誌(relay log)到其餘的slave;
  • 應用從master保存的二進制日誌事件(binlog events);
  • 提高一個slave爲新的master;
  • 使其餘的slave鏈接新的master進行復制;

MHA 工具組件

MHA 提供了不少的程序組件,經過這些組件,咱們 能夠很方便的管理MHA集羣。網絡

Manager節點:架構

  • masterha_check_ssh:MHA依賴的環境監測工具;
  • masterha_check_ssh: MySql複製環境檢測工具;
  • masterha_manager: MHA 服務主程序;
  • masterha_check_status: MHA運行狀態探測工具;
  • masterha_master_monitor: MySql master節點可用性檢測工具;
  • masterha_switch: master 節點切換工具;
  • masterha_conf_host: 添加或刪除配置的節點;
  • masterha_stop: 關閉MHA服務的工具;

Node節點:app

  • save_binary_logs:保存和複製master節點的二進制日誌;
  • apply_diff_relay_logs: 識別差別的中繼日誌事件並應用於其餘的slave;
  • purge_relay_logs:清除中集日誌(不會阻塞SQL線程);

環境以下less

MHA manager    192.168.94.11  manager

master1      192.168.94.22  sqlm1

slave1      192.168.94.33  sqls1

slave2      192.168.94.44  sqls2

作好準備工做 , 準備好全部安裝包以及依賴關係

關閉防火牆、SElinux

下載mha-manager和mha-node

http://downloads.mariadb.com/MHA/

下載源碼、rpm包均可以  

安裝依賴包和mariadb

# 全部服務器相同操做
[root@manager ~]# yum -y install ntp perl cpan perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes mariadb mariadb-server

# manager生成密鑰對 , 把公鑰發到下面
[root@manager ~]# ssh-keygen -t rsa
[root@manager ~]# ssh-copy-id -i .ssh/id_rsa.pub 192.168.94.22
[root@manager ~]# ssh-copy-id -i .ssh/id_rsa.pub 192.168.94.33
[root@manager ~]# ssh-copy-id -i .ssh/id_rsa.pub 192.168.94.44
# 手動驗證 , 第一次鏈接須要敲yes
[root@manager ~]# ssh sqlm1
[root@manager ~]# ssh sqls1
[root@manager ~]# ssh sqls2
# sqlm1服務器生成祕鑰對 , 發給sqls一、2
# sqls1服務器生成密鑰對 , 發給sqlm一、sqls2
# sqls2服務器生成密鑰對 , 發給sqlm一、sqls1
# 無需驗證

配置mariadb配置文件

# 全部服務器作相同操做 同步時間
[root@manager ~]# ntpdate cn.pool.ntp.org
[root@manager ~]# vim /etc/my.cnf
server-id=22
log-bin=mysql-bin
log-slave-updates=true 
relay_log_purge=0 

[root@sqls1 ~]# vim /etc/my.cnf
server-id=33
log-bin=mysql-bin
relay-log=slave-relay-bin 
log-slave-updates=true 
relay_log_purge=0 

[root@sqls2 ~]# vim /etc/my.cnf
server-id=44
log-bin=mysql-bin
relay-log=slave-relay-bin 
log-slave-updates=true 
relay_log_purge=0 

受權主從複製用戶

[root@sqlm1 ~]# systemctl start mariadb
[root@sqlm1 ~]# mysql
MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.94.%' identified by '123123';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> show master status\G
File: mysql-bin.000003
        Position: 473

[root@sqls1 ~]# systemctl start mariadb
[root@sqls1 ~]# mysql
MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.94.%' identified by '123123';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> change master to master_host='192.168.94.22',master_user='repl',master_password='123123',master_log_file='mysql-bin.000003',master_log_pos=473;
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G

[root@sqls2 ~]# systemctl start mariadb
[root@sqls2 ~]# mysql
MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.94.%' identified by '123123';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> change master to master_host='192.168.94.22',master_user='repl',master_password='123123',master_log_file='mysql-bin.000003',master_log_pos=473;
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G

下載安裝mha-manager 、mha-node

# manager須要兩個都安裝 , 其餘只須要安裝node
wget https://downloads.mariadb.com/MHA/mha4mysql-node-0.54-0.el6.noarch.rpm
wget https://downloads.mariadb.com/MHA/mha4mysql-manager-0.55-0.el6.noarch.rpm
rpm -ivh mha4mysql-node-0.54-0.el6.noarch.rpm
rpm -ivh mha4mysql-manager-0.55-0.el6.noarch.rpm

在master上配置MHA文件

[root@manager ~]# mkdir -p /masterha/app1
[root@manager ~]# mkdir /etc/masterha
[root@manager ~]# vim /etc/masterha/default.cnf
[server default]
user
=mha password=123123 manager_workdir=/masterha/app1 manager_log=/masterha/app1/manager.log remote_workdir=/masterha/app1 ssh_user=root repl_user=repl repl_password=123123 ping_interval=1 master_ip_failover_script="/usr/bin/masterha_ip_failover" [server1] hostname=sqlm1 master_binlog_dir=/var/lib/mysql candidate_master=1 [server2] hostname=sqls1 master_binlog_dir=/var/lib/mysql candidate_master=1 [server3] hostname=sqls2 master_binlog_dir=/var/lib/mysql
[root@manager ~]# vim /usr/bin/masterha_ip_failover
[root@manager ~]# chmod +x /usr/bin/masterha_ip_failover

masterha_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 = '192.168.94.111/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";
                
            }
        }
}
&stop_vip();
&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";
    
}

受權mha用戶給本地 

# 全部node作相同操做
MariaDB [(none)]> grant all privileges on *.* to 'mha'@'sqlm1' identified by '123123';
MariaDB [(none)]> grant all privileges on *.* to 'mha'@'sqls1' identified by '123123';
MariaDB [(none)]> grant all privileges on *.* to 'mha'@'sqls2' identified by '123123';
MariaDB [(none)]> flush privileges;

masterha_check_ssh工具驗證的ssh信任登陸是否成功

[root@manager ~]# masterha_check_ssh --conf=/etc/masterha/default.cnf

 

masterha_check_repl工具驗證的MySQL複製是否成功

[root@manager ~]# masterha_check_repl --conf=/etc/masterha/default.cnf

測試

[root@manager ~]# masterha_manager --conf=/etc/masterha/default.cnf 
tail -f /masterha/app1/manager.log 

[root@sqlm1 ~]# systemctl stop mariadb

相關文章
相關標籤/搜索