MySQL MHA--故障切換模式(GTID模式和非GTID模式)


GTID和非GTID故障切換模式選擇

MySQL 5.6版本引入GTID來解決主從切換時BINLOG位置點難定位的問題,MHA從0.56版本開始支持基於GTID的復制,在切換時可以采用GTID模式和非GTID模式兩種模式進行切換,如何在發生故障切換時如何判斷采用哪種切換方式呢?

在MHA/MasterFailover.pm的do_master_failover方法中定義了"主庫宕機"情況下的故障切換流程,其中第一步就是檢查配置文件和確定故障切換模式

相關代碼:

    my ( $servers_config_ref, $binlog_server_ref ) = init_config(); $log->info("Starting master failover."); $log->info(); $log->info("* Phase 1: Configuration Check Phase..\n"); $log->info(); MHA::ServerManager::init_binlog_server( $binlog_server_ref, $log ); $dead_master = check_settings($servers_config_ref); if ( $_server_manager->is_gtid_auto_pos_enabled() ) { $log->info("Starting GTID based failover."); } else { $_server_manager->force_disable_log_bin_if_auto_pos_disabled(); $log->info("Starting Non-GTID based failover."); } $log->info();

在MHA\ServerManager.pm中判斷是否采用GTID模式切換的方法:

sub is_gtid_auto_pos_enabled($) { my $self = shift; return 1 if ( $self->{gtid_failover_mode} == 1 ); return 0; } sub force_disable_log_bin_if_auto_pos_disabled($) { my $self = shift; my $log  = $self->{logger}; if ( $self->{gtid_failover_mode} == 2 ) { my @slaves = $self->get_alive_slaves(); $log->info("Forcing disable_log_bin since GTID auto pos is disabled"); foreach my $slave (@slaves) { $slave->{disable_log_bin} = 1; } } }

$self->{gtid_failover_mode} = $self->get_gtid_status();

在MHA/ServerManager.pm判斷群集是否使用GTID復制:

sub get_gtid_status($) { my $self    = shift; my @servers = $self->get_alive_servers(); my @slaves  = $self->get_alive_slaves(); return 0 if ( $#servers < 0 );
  foreach (@servers) { return 0 unless ( $_->{has_gtid} ); } foreach (@slaves) { return 0 unless ( $_->{Executed_Gtid_Set} ); } foreach (@slaves) { return 1
      if ( defined( $_->{Auto_Position} ) && $_->{Auto_Position} == 1 ); return 1 if ( $_->{use_gtid_auto_pos} ); } return 2; }

其中has_gtid的計算為:

use constant Has_Gtid_SQL             => "SELECT \@\@global.gtid_mode As Value"; sub has_gtid($) { my $self  = shift; my $value = $self->get_variable(Has_Gtid_SQL); if ( defined($value) && $value eq "ON" ) { $self->{has_gtid} = 1; return 1; } return 0; }

計算use_gtid_auto_pos的代碼:

## MHA/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/; my %PARAM; for (@PARAM_ARRAY) { $PARAM{$_} = 1; } $value{use_gtid_auto_pos} = $param_arg->{use_gtid_auto_pos}; if ( !defined( $value{use_gtid_auto_pos} ) ) { $value{use_gtid_auto_pos} = $default->{use_gtid_auto_pos}; $value{use_gtid_auto_pos} = 1 if ( !defined( $value{use_gtid_auto_pos} ) ); }

如果配置文件中未指明參數use_gtid_auto_pos,則默認use_gtid_auto_pos=1。

計算Executed_Gtid_Set是查看show master status和show slave status兩個命令輸出結果中的Executed_Gtid_Set列信息。

計算Auto_Position是查看show slave status命令輸出結果中MASTER_AUTO_POSITION列的值。

 

使用非GTID模式進行切換的場景有:

1、如果群集中任意節點未開啟GTID(gtid_mode=0)或Executed_Gtid_Set集合為空。

2、群集中所有節點都未使用GTID復制模式(MASTER_AUTO_POSITION=0),且配置文件中設置use_gtid_auto_pos=0。

 

使用GTID模式進行切換的場景有:

1、如果群集中任意節點使用GTID復制,SHOW SLAVE STATUS輸入結果中MASTER_AUTO_POSITION=1。

2、如果所有節點都開啟GTID,且未配置參數use_gtid_auto_pos或配置參數use_gtid_auto_pos=1。

 

差異日志補償

1、原主庫日志補償

在GTID故障切換模式下,無論原主庫操作系統級別是否正常,都不會嘗試從原主庫上獲取差異BINLOG。

如上圖所示,在GTID故障切換模式下,不會進行Phase 3.2階段,即不會嘗試從原Master服務器中獲取最新BINLOG。

 

在非GTID故障切換模式下,如果原主庫操作系統級別正常,會嘗試對比原主庫BINLOG和最新從庫的RELAY LOG,如果存在差異,會備份主庫差異BINLOG並應用到從庫上。

如上圖所示,在非GTID故障切換模式下,會先進行Phase 3.1階段,從擁有最新BINLOG的從庫上獲取差異日志,再進行Phase 3.2階段,嘗試從原Master服務器上獲取最新BINLOG。

 

2、Binlog Server日志補償

在GTID故障切換模式下,會對比候選主庫的RELAY LOG和Binlog Server的BINLOG,如果Binlog Server含有最新日志,會根據Binlog Server進行日志補償。

在非GTID模式下,無論Binlog Server是否最新日志,都不會根據Binlog Server進行日志補償。

 

3、最新從庫日志補償

無論是GTID故障切換模式還是非GTID故障切換模式,都會挑選出“最新從庫”,並對比“最新從庫”RELAY LOG和“新主庫” RELAY LOG,如果存在差異,則保存“最新從庫”的日志並應用到“新主庫上”。

在GTID故障切換模式下,可以給masterha_master_switch傳入–wait_until_gtid_in_sync=1參數使其不等其它Slave完成數據同步,以加快切換速度。

 

GTID故障切換模式注意事項

如果群集因為某種原因導致主從節點上的Executed_Gtid_Set不同,如:

1、對從庫進行直接授權,導致從庫比主庫擁有更多BINLOG,但該Binlog因各種原因被Purged掉

2、群集做過版本升級,從未使用GTID的版本升級到GTID版本,從庫上曾一段時間內作為主庫提供服務,但該時間段日志被Purged掉

有上訴類似問題時,將從庫提升為主庫並使用master_auto_position=1來配置復制,復制會因為新主庫無法提供足夠BINLOG事件而失敗。

處理辦法:

1、通過RESET MASTER和SET GLOBAL gtid_purged=''使得所有節點擁有相同的GTID 集合

2、將所有復制修改為基於POS點搭建的復制。

 

GTID和非GTID故障切換模式對比

1、無論時GTID故障切換模式還是非GTID故障切換模式,都會從“最新從庫”獲取差異日志。

2、非GTID故障切換模式下,會嘗試從“原主庫”獲取差異日志,但不會從“BINLOG Server”獲取差異日志。

3、GTID故障切換模式下,會從“BINLOG Server”獲取差異日志,但不會從“原主庫”獲取差異日志。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM