mysql高可用框架-MHA

MHA高可用架構

  • 用一個管理節點監控後端數據庫主庫可用性
  • 提供VIP漂移接口,不提供具體方法
  • 提供補全從庫日誌的腳本
 
MHA
  • 監控主庫,提供自動主從切換;
  • 提供VIP漂移接口
  • 提供補全從庫日誌的腳本
 
MHA的安裝步驟

  1. 規劃
  2. 配置服務器間域名和ssh互信訪問
  3. 在manager節點安裝MHA node 和manager組件及依賴包
  4. 在數據庫服務器安裝MHA node組件及依賴包
  5. 配置VIP管理腳本master_ip_failover和master_ip_online_change
  6. 配置MHA配置文件mha_manager.cnf
  7. 數據庫配置主從,添加mha鏈接用戶 ...etc
  8. 啓動MHA開始監控數據庫主從集羣
 
演示

演示MHA部署-環境介紹
 
 
環境請自行提取下載
http://pan.baidu.com/s/1qXDOeba
 
操做步驟
 
操做步驟:
- 0.確保主從複製配置完成
- 此列中主從複製帳號repl
-  
- ##
-  
- 虛擬ip已經,部署在主庫
- 主庫,爲mha提供mha帳戶
-
-   
-
-
- 1.確保hosts和hostname配置正確
- ##
-
- root@debtest1:~/.ssh# cat /etc/hosts
- 127.0.0.1    localhost
- 192.168.0.113    debtest1.sam.test    debtest1
- 192.168.0.114    debtest2.sam.test    debtest2
- 192.168.0.115    debtest3.sam.test    debtest3
-
- # The following lines are desirable for IPv6 capable hosts
- ::1    localhost ip6-localhost ip6-loopback
- ff02::1 ip6-allnodes
- ff02::2 ip6-allrouters
- root@debtest1:~/.ssh# cat /etc/hostname
- debtest1
-
- ###############################################################################
-
- 2.使用ssh-keygen工具生成統一公私鑰對,並同步到全部三臺機器.測試公私鑰驗證訪問
- ##
-
- ssh-keygen -t rsa
-
- cd ~/.ssh
- cat id_rsa.pub authorized_keys
-
-
- scp ./* root@debtest2:/root/.ssh/
- scp ./* root@debtest3:/root/.ssh/
-
-
- ssh debtest1
- ssh debtest2
- ssh debtest3
-
-
- ###############################################################################
-
- 3.規劃節點用戶和ip配置
- ##
-
-
- 192.168.0.113    debtest1 --> master
- 192.168.0.114    debtest2 --> mysql1(master)
- 192.168.0.115    debtest3 --> mysql2(slave)
- 192.168.0.119    vip
-
- 定好虛擬ip後別忘了在當前的主庫上添加虛擬ip
- ##
- ip addr add 192.168.0.119/32 dev eth1
-
- #刪除虛擬ip的命令
- ##ip addr del 192.168.0.119/32 dev eth1
-
- ###############################################################################
-
- 4.在所有節點上安裝mha node包和其依賴包
- ##
-
-
- debtest1,debtest1,debtest1
-
- apt-get install libdbd-mysql-perl
-
- dpkg -i mha4mysql-node_0.53_all.deb
-
- ###############################################################################
-
- 5.僅須要在manager節點上安裝mha manager包及其依賴包
- ##
-
- debtest1
-
- apt-get install libdbd-mysql-perl
- apt-get install libconfig-tiny-perl
- apt-get install liblog-dispatch-perl
- apt-get install libparallel-forkmanager-perl
-
- dpkg -i mha4mysql-manager_0.53_all.deb
-
-
- ###############################################################################
-
-
- 6.創建配置文件目錄,編輯mha必要的三個文件,一個配置文件,2個虛擬ip管理腳本且內容能夠一致
- ##
-
- master_ip_online_change 
- master_ip_failover
- mha_manager.cnf
-
-
- ###############################################################################
-
- 7.能夠嘗試驗證一下配置是否成功
- ##
-
- masterha_check_ssh --conf=./mha_manager.cnf
- masterha_check_repl --conf=./mha_manager.cnf
-
- ###############################################################################
-
- 8.在manager節點啓動mha服務,而後觀察日誌,並嘗試關閉當前主庫,注意觀察日誌,主要看失效發現,日誌檢測,ip漂移和角色切換過程
- ##
-
- nohup /usr/bin/masterha_manager --conf=/root/mha_base/mha_manager.cnf --ignore_last_failover  < /dev/null > /root/mha_base/manager.log 2>&1 &
 
 

 

MHA配置文件node

MHA配置文件
 
[server default]
manager_workdir=/root/mha_base
manager_log=/root/mha_base/manager.log
remote_workdir=/root/mha_base
 
ssh_user=root
ssh_port=22
user=mha
password=mha
repl_user=repl
repl_password=repl
multi_tier_slave=1
ping_interval=1
ping_type=CONNECT
master_ip_failover_script=/root/mha_base/master_ip_failover
master_ip_online_change_script=/root/mha_base/master_ip_online_change
secondary_check_script=/usr/bin/masterha_secondary_check -s 192.168.0.113 -s 192.168.0.115 --user=root --port=22 --master_host=debtest2 --master_ip=192.168.0.114 --master_port=3306
 
[server1]
candidate_master=0
ignore_fail=1
check_repl_delay = 1
hostname=debtest2
ip=192.168.0.114
port=3306
ssh_port=22
master_binlog_dir=/var/log/mysql/
 
[server2]
candidate_master=0
ignore_fail=1
check_repl_delay = 1
hostname=debtest3
ip=192.168.0.115
port=3306
ssh_port=22
master_binlog_dir=/var/log/mysql/

 

 
 
 
VIP漂移腳本
#!/usr/bin/env perl
 
## 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
);
 
my $vip = '192.168.0.119/24'; #virtual ip
my $ssh_start_vip = "ip addr add $vip dev eth1";
my $ssh_stop_vip = "ip addr del $vip dev eth1";
 
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 {
  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 "Disabling the VIP on old master: $orig_master_host \n";
          &stop_vip();
      $exit_code = 0;
          # updating global catalog, etc
 
    };
    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 "Enabling the VIP - $vip on old master: $new_master_host \n";
          &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;
  }
}
 
# Enable the VIP on the new_master
sub start_vip() {
    `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
 
# Disable the VIP on the old_master
 
sub stop_vip() {
my $ssh_user = "root";
    `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";
}
 
相關文章
相關標籤/搜索