微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MHA基础知识

环境:
manager: 10.1.99.213(安装mha manager mha node包)安装依赖可以使用机器对应版本的epel源
master: 10.1.99.216(安装mysql 以及 mha node包)
node1 : 10.1.99.214(安装MysqL 以及 mha node包)
node2 : 10.1.99.215(安装MysqL 以及 mha node包)

依赖:
yum install perl-dbd-mysql  
yum install perl-Config-Tiny  
yum install perl-Log-dispatch  
yum install perl-Parallel-ForkManager

关于MHA failover的处理过程:
①保存宕机master的未来得及传输的binlog
②检查最新状态的slave,应用差异中继日志到各个slave
③应用master的binlog到各个slave
④提升一个slave为主
⑤其他slave指向新master

Manager工具包
masterha_check_ssh              检查MHA的SSH配置状况
masterha_check_repl             检查MysqL复制状况
masterha_manger                 启动MHA
masterha_check_status           检测当前MHA运行状态
masterha_master_monitor         检测master是否宕机
masterha_master_switch          控制故障转移(自动或者手动)
masterha_conf_host              添加删除配置的server信息


Node工具包(自动调用 无需手动执行):

save_binary_logs                保存和复制master的二进制日志 

apply_diff_relay_logs           识别差异的中继日志事件并将其差异的事件应用于其他的slave 

purge_relay_logs                清除中继日志(不会阻塞sql线程 )

配置过程(mha manager的配置文件在文尾app1.conf):
①主机间互相信任配置(sshkey-gen  以及 ssh-copy -i 等)
②master 以及node之间的主从配置 (都得设置复制账户repl(replication slave),以及管理账户mha(ALL),开启log-bin参数(故障切换提神master只能切换到开启该参数的机器),node得配置relay-log-purge=0,read_only=1等)
③manager 配置文件/etc/mha/app1.conf
④manager ssh检查(masterha_check_ssh --conf=/etc/mha/app1.cnf)
⑤manager repl检查(masterha_check_repl --conf=/etc/mha/app1.cnf)
⑥启动mha manager(masterha_manager --conf=/etc/mha/app1.cnf )不要后台启动,会退出 
⑦检查manager状态(masterha_check_status --conf=/etc/mha/app1.cnf)
关闭mha manager(masterha_stop --conf=/etc/mha/app1.cnf)

参数:
--ignore_last_failover:在缺省情况下,如果MHA检测到连续发生宕机,且两次宕机间隔不足8小时的话,则不会进行Failover,之所以这样限制是为了避免ping-pong效应。该参数代表忽略上次MHA触发切换产生的文件认情况下,MHA发生切换后会在日志目录,也就是上面我设置的/data产生app1.failover.complete文件,下次再次切换的时候如果发现该目录下存在该文件将不允许触发切换,除非在第一次切换后收到删除文件
--remove_dead_master_conf: 该参数代表当发生主从切换后,老的主库将会从配置文件中移除,一般情况下不需要开启

Failover之后的处理:
①启动宕机master
②自己作为从指向新的master(指向内容查看日志来确定 cat app1.log  | grep -i "change master")
③启动mha manager
④若想还是用原来的实例作master,则使用该命令masterha_master_switch手动切换

附件 app1.conf

点击(此处)折叠或打开

  1. ####################BEGIN#########
  2. [server default]
  3. manager_log=/tmp/app1.log
  4. manager_workdir=/tmp
  5. master_binlog_dir=/opt/MysqL
  6. user=mha
  7. password=taomee
  8. ping_interval=2
  9. repl_password=taomee
  10. repl_user=repl
  11. ssh_user=root
  12. master_ip_failover_script=/usr/bin/masterha_ip_failover
  13. [server1]
  14. hostname=10.1.99.216
  15. port=3306
  16. [server2]
  17. hostname=10.1.99.215
  18. port=3306
  19. [server3]
  20. hostname=10.1.99.214
  21. port=3306
  22. #######################END##########


cat masterha_ip_failover


点击(此处)折叠或打开

  1. #!/usr/bin/env perl

  2. # copyright (C) 2011 DeNA Co.,Ltd.
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or fitness FOR A PARTIculaR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

  18. ## Note: This is a sample script and is not complete. Modify the script based on your environment.

  19. use strict;
  20. use warnings FATAL => 'all';

  21. use Getopt::Long;

  22. my (
  23.     $command, $ssh_user, $orig_master_host, $orig_master_ip,
  24.     $orig_master_port, $new_master_host, $new_master_ip, $new_master_port
  25. );

  26. my $vip = '10.1.99.233/24';
  27. my $key = '0';
  28. my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
  29. my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
  30. my $ssh_Bcast_arp = "/usr/bin/arping -c 3 -A 10.1.99.233"; #ARP回复模式,更新邻居。要是不加则服务器自动等到vip缓存失效,期间VIP会有一定时间的不可用。

  31. Getoptions(
  32.     'command=s' => \$command,
  33.     'ssh_user=s' => \$ssh_user,
  34.     'orig_master_host=s' => \$orig_master_host,
  35.     'orig_master_ip=s' => \$orig_master_ip,
  36.     'orig_master_port=i' => \$orig_master_port,
  37.     'new_master_host=s' => \$new_master_host,
  38.     'new_master_ip=s' => \$new_master_ip,
  39.     'new_master_port=i' => \$new_master_port,
  40. );

  41. exit &main();

  42. sub main {

  43.     print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";

  44.     if ( $command eq "stop" || $command eq "stopssh" ) {

  45.         my $exit_code = 1;
  46.         eval {
  47.             print "disabling the VIP on old master: $orig_master_host \n";
  48.             &stop_vip();
  49.             $exit_code = 0;
  50.         };
  51.         if ($@) {
  52.             warn "Got Error: $@\n";
  53.             exit $exit_code;
  54.         }
  55.         exit $exit_code;
  56.     }
  57.     elsif ( $command eq "start" ) {

  58.         my $exit_code = 10;
  59.         eval {
  60.             print "Enabling the VIP - $vip on the new master - $new_master_host \n";
  61.             &start_vip();
  62.            # &start_arp();
  63.             $exit_code = 0;
  64.         };
  65.         if ($@) {
  66.             warn $@;
  67.             exit $exit_code;
  68.         }
  69.         exit $exit_code;
  70.     }
  71.     elsif ( $command eq "status" ) {
  72.         print "Checking the Status of the script.. OK \n";
  73.         exit 0;
  74.     }

  75.     else {
  76.         &usage();
  77.         exit 1;
  78.     }
  79. }

  80. sub start_vip() {
  81.     `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
  82. }
  83. sub start_arp() {
  84.     `ssh $ssh_user\@$new_master_host \" $ssh_Bcast_arp \"`;
  85. }
  86. sub stop_vip() {
  87.     `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
  88. }

  89. sub usage {
  90.   print
  91. "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
  92. }

手动切换报错:
masterha_master_switch --master_state=alive --conf=/etc/mha/app1.cnf
Fri Oct 14 18:22:58 2016 - [info] Checking MHA is not monitoring or doing failover..
Fri Oct 14 18:22:58 2016 - [error][/usr/share/perl5/vendor_perl/MHA/MasterRotate.pm, ln142] Getting advisory lock Failed on the current master. MHA Monitor runs on the current master. Stop MHA Manager/Monitor and try again.
Fri Oct 14 18:22:58 2016 - [error][/usr/share/perl5/vendor_perl/MHA/ManagerUtil.pm, ln177] Got ERROR:  at /usr/bin/masterha_master_switch line 53.


在进行MHA手动切换时,需要将负责自动切换的监控工具masterha_manager关闭,才可完成手动切换。

参考:MysqL MHA 搭建&测试

注意:slave要设置 read_only=1 relay_log_purge=1
定时任务删除relay_log


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐