修改MySQL高可用模块接收自定义VIP参数
admin
2023-05-18 03:21:49
0

    但凡是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}";


相关内容

热门资讯

伊朗宣布5月19日起恢复股票市... 当地时间16日,伊朗证券交易组织表示,伊朗股票市场将于5月19日起恢复交易。伊朗证券交易组织负责交易...
联合国秘书长欢迎黎以延长停火 新华社联合国5月16日电(记者尚绪谦)联合国秘书长发言人办公室16日说,秘书长古特雷斯欢迎黎巴嫩与以...
如何让“彩礼”回归于“礼”?一... 黄河水浇灌出的宁夏红寺堡是中国最大的生态移民安置区,宁南八县的乡音在此交汇,对美好生活的期盼与发展中...
原创 外... 外星翻译官的翻车现场:地球语言真的太难了! 银河系翻译官协会(假想机构)最近发布了一份年度报告,总...
首飞成功!已突破大重量载荷发射... 科技日报记者 付毅飞 实习生 张城辉 记者从蓝箭航天获悉,5月14日11时0分,朱雀二号改进型遥五运...
京东携手MAXHUB共推AI智... PChome消息,近日,京东与智能会议平板领域领军品牌MAXHUB正式签署战略合作协议,标志着双方战...
29个智能气象站启动业务试运行 本报北京5月15日电(记者李红梅)记者从中国气象局获悉:北京密云等29个智能气象站已于近日启动业务试...
广西一载15人车辆坠河,10人... 记者从广西环江毛南族自治县相关方面获悉,据初步了解,5月16日21时30分许,一辆汽车在环江县洛阳镇...
iOS 26.5更新苹果地图两... 苹果地图是iOS 26.5此次更新中获得新功能的应用之一。以下是地图用户需要了解的最新变化。 推荐...
每经热评 | 黄仁勋喝蜜雪冰城... 每经评论员 朱成祥 黄仁勋近期到访北京,被拍到在胡同喝豆汁、吃炸酱面、举着蜜雪冰城饮料。他表情轻松,...