MHA master_ip_failover

服务器主备切换脚本示例

点击(此处)折叠或打开

  1. #!/usr/bin/env perl



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

  3.     use strict;
  4.     use warnings FATAL => 'all';

  5.     use Getopt::Long;
  6.     use MHA::DBHelper;


  7.     my (
  8.       $command, $ssh_user, $orig_master_host,
  9.       $orig_master_ip, $orig_master_port, $new_master_host,
  10.       $new_master_ip, $new_master_port
  11.     );

  12.     my $vip = '10.0.0.10/24'; #virtual ip
  13.     my $key = "2";
  14.     my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
  15.     my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";


  16.     GetOptions(
  17.       'command=s' => \$command,
  18.       'ssh_user=s' => \$ssh_user,
  19.       'orig_master_host=s' => \$orig_master_host,
  20.       'orig_master_ip=s' => \$orig_master_ip,
  21.       'orig_master_port=i' => \$orig_master_port,
  22.       'new_master_host=s' => \$new_master_host,
  23.       'new_master_ip=s' => \$new_master_ip,
  24.       'new_master_port=i' => \$new_master_port,
  25.       );
  26.     exit &main();

  27.     sub main {
  28.       if ( $command eq "stop" || $command eq "stopssh" ) {

  29.         # $orig_master_host, $orig_master_ip, $orig_master_port are passed.
  30.         # If you manage master ip address at global catalog database,
  31.         # invalidate orig_master_ip here.
  32.         my $exit_code = 1;
  33.         eval {
  34.           print "Disabling the VIP on old master: $orig_master_host \n";
  35.               &stop_vip();
  36.           $exit_code = 0;
  37.               # updating global catalog, etc

  38.         };
  39.         if ($@) {
  40.           warn "Got Error: $@\n";
  41.           exit $exit_code;
  42.         }
  43.         exit $exit_code;
  44.       }
  45.       elsif ( $command eq "start" ) {

  46.         # all arguments are passed.
  47.         # If you manage master ip address at global catalog database,
  48.         # activate new_master_ip here.
  49.         # You can also grant write access (create user, set read_only=0, etc) here.
  50.         my $exit_code = 10;
  51.         eval {
  52.           print "Enabling the VIP - $vip on old master: $new_master_host \n";
  53.               &start_vip();
  54.           $exit_code = 0;
  55.         };
  56.         if ($@) {
  57.           warn $@;

  58.           # If you want to continue failover, exit 10.
  59.           exit $exit_code;
  60.         }
  61.         exit $exit_code;
  62.       }
  63.       elsif ( $command eq "status" ) {

  64.         # do nothing
  65.         exit 0;
  66.       }
  67.       else {
  68.         &usage();
  69.         exit 1;
  70.       }
  71.     }

  72.     # Enable the VIP on the new_master
  73.     sub start_vip() {
  74.         `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
  75.     }

  76.     # Disable the VIP on the old_master

  77.     sub stop_vip() {
  78.     my $ssh_user = "root";
  79.         `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
  80.     }

  81.     sub usage {
  82.       print
  83.     "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";
  84.     }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26735168/viewspace-2096305/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26735168/viewspace-2096305/

[root@db-master mha]# masterha_check_ssh --conf=/etc/mha/app1.cnf [root@db-master mha]# [root@db-master mha]# # 2. 验证复制状态(修复认证插件后执行) [root@db-master mha]# masterha_check_repl --conf=/etc/mha/app1.cnf [root@db-master mha]# # 停止现有进程 [root@db-master mha]# pkill -f masterha_manager [root@db-master mha]# [root@db-master mha]# # 前台启动(查看实时错误) [root@db-master mha]# masterha_manager --conf=/etc/mha/app1.cnf --verbose [root@db-master mha]# masterha_check_repl --conf=/etc/mha/app1.cnf > /tmp/check.log 2>&1 [root@db-master mha]# cat /tmp/check.log [root@db-master mha]# nohup masterha_manager --conf=/etc/mha/app1.cnf \ > --remove_dead_master_conf \ > --ignore_last_failover \ > > /var/log/mha/app1/manager.log 2>&1 & [1] 10863 [root@db-master mha]# vi /etc/mha/app1.cnf [1]+ 完成 nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover > /var/log/mha/app1/manager.log 2>&1 [root@db-master mha]# cat /etc/mha/app1.cnf [server default] user=mha password=Strong@123 manager_workdir=/var/log/mha/app1 manager_log=/var/log/mha/app1/manager.log log_level=DEBUG remote_workdir=/tmp ssh_user=root repl_user=repl repl_password=Strong@123 ping_interval=1 ping_type=SELECT master_ip_failover=/usr/local/bin/mha/master_ip_failover master_ip_online_change=/usr/local/bin/mha/master_ip_online_change report_script=/usr/local/bin/mha/send_report shutdown_script="" secondary_check_script="" [server1] hostname=10.1.1.60 master_binlog_dir=/var/lib/mysql [server2] hostname=10.1.1.61 master_binlog_dir=/var/lib/mysql [server3] hostname=10.1.1.62 master_binlog_dir=/var/lib/mysql
最新发布
07-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值