码迷,mamicode.com
首页 > 数据库 > 详细

修改MySQL高可用模块接收自定义VIP参数

时间:2017-06-16 23:17:18      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:mysql   高可用   mha   

    但凡是MySQL DBA肯定都听说过MHA个高可用方案,而且很多公司都是通过对MHA做二次开发来实现MySQL高可用的。如果MHA不结合VIP的话,每次主库切换都需要程序修改连数据库的配置,这样比较麻烦。而采用MHA+VIP的方式时可以在主库切换的过程中让VIP漂移到新主库,省去了改数据库配置这一过程。

    公司以前是每一组主从复制集群都配置一个manager结点,然后将vip和网络接口等信息都写死在master_ip_failover和master_ip_online_change脚本中。当主从集群数量太多的情况下要维护的manager结点很多,管理起来很麻烦。

如何实现用一个Manager结点管理多个支持VIP的mysql主从集群呢?有两种实现方式:

1,每一组主从复制集群维护两个切换脚本,将VIP和网络接口信息写死在脚本里。

2,修改MHA的相关模块,使其能识别我们自定义的参数,我们只需要在每一组主从复制集群的配置文件中给参数传值。

    很明显,第1种方式太low了,需要维护大量的切换脚本。那我们需要修改哪些模块呢?

    看一下masterha_check_repl这段脚本,可以看到检测主从复制状态的时候会调用MasterMonitor模块。

$exit_code = MHA::MasterMonitor::main( "--interactive=0", "--check_only",
  "--check_repl_health", @ARGV );
if ( $exit_code == 0 ) {
  print "\nMySQL Replication Health is OK.\n";
}
else {
  print "\nMySQL Replication Health is NOT OK!\n";
}


通过masterha_master_switch这段脚本可以看到在线切换是调用的MasterRotate模块,故障切换是调用的MasterFailover模块。

if ( $master_state eq "dead" ) {
  $exit_code = MHA::MasterFailover::main(@ARGV);
}
elsif ( $master_state eq "alive" ) {
  $exit_code = MHA::MasterRotate::main(@ARGV);
}
else {
  pod2usage(1);
}


MasterMonitor.pm,MasterRotate.pm,MasterFailover.pm这三个模块都是调用Config.pm模块来读取参数配置,所以我们只需要修改这几个模块即可。


为了不平的网络环境,我在配置文件加了这三个配置项:

app_vip :主库的VIP

netmask :  VIP的网络位

interface :VIP要绑定的网上


对应调整的代码如下:

Config.pm:

申明变量:

my @PARAM_ARRAY =
  qw/ hostname ip port ssh_host ssh_ip ssh_port ssh_connection_timeout ssh_options node_label candidate_master no_master ignore_fail skip_init_ssh_check skip_reset_slave user password repl_user repl_password disable_log_bin master_pid_file handle_raw_binlog ssh_user remote_workdir master_binlog_dir log_level manager_workdir manager_log check_repl_delay check_repl_filter latest_priority multi_tier_slave ping_interval ping_type secondary_check_script master_ip_failover_script master_ip_online_change_script shutdown_script report_script init_conf_load_script client_bindir client_libdir use_gtid_auto_pos app_vip netmask interface/;


给变量赋值:

  $value{app_vip} = $param_arg->{app_vip};
  if ( !defined( $value{app_vip} ) ) {
    $value{app_vip} = $default->{app_vip};
  }
  $value{netmask} = $param_arg->{netmask};
  if ( !defined( $value{netmask} ) ) {
    $value{netmask} = $default->{netmask};
  }
  $value{interface} = $param_arg->{interface};
  if ( !defined( $value{interface} ) ) {
    $value{interface} = $default->{interface};
  }


MasterMonitor.pm :

修改复制检测时的命令:

"$current_master->{master_ip_failover_script} --command=status --ssh_user=$current_master->{ssh_user} --orig_master_host=$current_master->{hostname} --orig_master_ip=$current_master->{ip} --orig_master_port=$current_master->{port}  --app_vip=$current_master->{app_vip} --netmask=$current_master->{netmask} --interface=$current_master->{interface}";


MasterMonitor.pm :

修改停原主库写入的命令:

"$orig_master->{master_ip_online_change_script} --command=stop --orig_master_host=$orig_master->{hostname} --orig_master_ip=$orig_master->{ip} --orig_master_port=$orig_master->{port} --orig_master_user=$orig_master->{escaped_user} --orig_master_password=$orig_master->{escaped_password} --new_master_host=$new_master->{hostname} --new_master_ip=$new_master->{ip} --new_master_port=$new_master->{port} --new_master_user=$new_master->{escaped_user} --new_master_password=$new_master->{escaped_password} --app_vip=$orig_master->{app_vip} --netmask=$orig_master->{netmask} --interface=$orig_master->{interface}";


修改允许在新主库写入的命令:

"$new_master->{master_ip_online_change_script} --command=start --orig_master_host=$orig_master->{hostname} --orig_master_ip=$orig_master->{ip} --orig_master_port=$orig_master->{port} --orig_master_user=$orig_master->{escaped_user} --orig_master_password=$orig_master->{escaped_password} --new_master_host=$new_master->{hostname} --new_master_ip=$new_master->{ip} --new_master_port=$new_master->{port} --new_master_user=$new_master->{escaped_user} --new_master_password=$new_master->{escaped_password} --app_vip=$orig_master->{app_vip} --netmask=$orig_master->{netmask} --interface=$orig_master->{interface}";


MasterFailover.pm:

修改禁用原从库的vip命令:

 "$dead_master->{master_ip_failover_script} --orig_master_host=$dead_master->{hostname} --orig_master_ip=$dead_master->{ip} --orig_master_port=$dead_master->{port} --app_vip=$dead_master->{app_vip}  --netmask=$dead_master     ->{netmask} --interface=$dead_master->{interface}";


修改启用原从库vip的命令:

 "$new_master->{master_ip_failover_script} --command=start --ssh_user=$new_master->{ssh_user} --orig_master_host=$dead_master->{hostname} --orig_master_ip=$dead_master->{ip} --orig_master_port=$dead_master->{port} --new_m     aster_host=$new_master->{hostname} --new_master_ip=$new_master->{ip} --new_master_port=$new_master->{port} --new_master_user=$new_master->{escaped_user} --new_master_password=$new_master->{escaped_password} --app_vip=$de     ad_master->{app_vip} --netmask=$dead_master->{netmask} --interface=$dead_master->{interface}";


本文出自 “一直在路上” 博客,请务必保留此出处http://chenql.blog.51cto.com/8732050/1939105

修改MySQL高可用模块接收自定义VIP参数

标签:mysql   高可用   mha   

原文地址:http://chenql.blog.51cto.com/8732050/1939105

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!