欧美一区2区三区4区公司二百,国产精品婷婷午夜在线观看,自拍偷拍亚洲精品,国产美女诱惑一区二区

MySQL之高可用組件MHA

1 簡介

MHA(Master High Availability)是由日本人yoshinorim開發的一款成熟且開源的MySQL高可用程序,它實現了MySQL主從環境下MASTER宕機后能夠自動進行單次故障轉移的功能,其本身由perl語言編寫,安裝方便,使用簡單

MHA Master可以單獨部署在一臺獨立的機器上管理多個master-slave集群,也可以部署在一臺slave節點

MHA Manager主要運行一些工具,比如masterha_manager工具實現自動監控MySQL Master和實現master故障切換,其它工具實現手動實現master故障切換、在線master轉移、連接檢查等

MHA Node部署在所有運行MySQL的服務器上,無論是master還是slave。主要作用有三個

Ⅰ、保存二進制日志 如果能夠訪問故障master,會拷貝master的二進制日志

II、應用差異中繼日志 從擁有最新數據的slave上生成差異中繼日志,然后應用差異日志

III、清除中繼日志 在不停止SQL線程的情況下刪除中繼日志

MHA工作原理

  1. 從宕機崩潰的Master保存二進制日志事件(binlog event);
  2. 識別含有最新更新的Slave;
  3. 應用差異的中繼日志(relay log)到其他Slave;
  4. 應用從Master保存的二進制日志事件;
  5. 提升一個Slave為新的Master;
  6. 使其他的Slave連接新的Master進行復制;

MHA數據補償

  • 當SSH能連接,從庫對比主庫GTID 或者position號,立即將二進制日志保存至各個從節點并且應用(save_binary_logs )
  • 當SSH不能連接, 對比從庫之間的relaylog的差異(apply_diff_relay_logs)

2 環境部署

節點 ip
主節點 10.243.95.3
從節點1 10.243.95.4
從節點2 10.243.95.5

數據庫復制一主兩從架構,MHA node部署在每個節點上,MHA manager部署在從節點2上

2.1 建立SSH互信

#各節點生成rsa密鑰
ssh-keygen -t rsa
#發送密鑰至三臺服務器實現互信
ssh-copy-id root@10.243.95.3
ssh-copy-id root@10.243.95.4
ssh-copy-id root@10.243.95.5

2.2 搭建主從復制

具體內容見上篇主從復制,注意mha57以下版本不支持GTID事務,需避雷,按需選擇下面的部署方法

2.3 安裝依賴包

2.3.1 MHA-0.57-el6版本

#1.所有節點安裝以下依賴包
yum -y install perl-DBD-MySQL perl-ExtUtils-MakeMaker perl-CPAN
yum install -y perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes
#2.所有節點安裝mha-node包
rpm -ivh ?mha4mysql-node-0.57-0.el6.noarch.rpm
#3.管理節點安裝mha-manage包
rpm -ivh ?mha4mysql-manager-0.57-0.el6.noarch.rpm

2.3.2 MHA-0.55-el6版本

#1.解壓mha包
unzip -u mha_pack.zip
cd mha_pack/mha_pack/rhel6/mha2
#2.安裝依賴
rpm -ivh perl-DBI-1.609-4.el6.x86_64.rpm
rpm -ivh perl-DBD-MySQL-4.013-3.el6.x86_64.rpm
rpm -ivh mha4mysql-node-0.54-0.el6.noarch.rpm
rpm -ivh perl-DBI-1.609-4.el6.x86_64.rpm
rpm -ivh perl-DBD-MySQL-4.013-3.el6.x86_64.rpm
rpm -ivh mha4mysql-node-0.54-0.el6.noarch.rpm
rpm -ivh perl-Config-Tiny-2.12-7.1.el6.noarch.rpm
rpm -ivh perl-TimeDate-1.16-11.1.el6.noarch.rpm
rpm -ivh perl-MailTools-2.04-4.el6.noarch.rpm
rpm -ivh perl-Email-Date-Format-1.002-5.el6.noarch.rpm
rpm -ivh perl-MIME-Types-1.28-2.el6.noarch.rpm
rpm -ivh perl-MIME-Lite-3.027-2.el6.noarch.rpm
rpm -ivh perl-Mail-Sender-0.8.16-3.el6.noarch.rpm
rpm -ivh perl-Mail-Sendmail-0.79-12.el6.noarch.rpm
rpm -ivh perl-Params-Validate-0.92-3.el6.x86_64.rpm
rpm -ivh perl-Log-Dispatch-2.27-1.el6.noarch.rpm
rpm -ivh perl-Parallel-ForkManager-0.7.9-1.el6.noarch.rpm
rpm -ivh perl-Time-HiRes-1.9721-127.el6.x86_64.rpm
rpm -ivh mha4mysql-manager-0.55-0.el6.noarch.rpm

2.4 配置文件

新建mha配置文件:vi /etc/masterha/app1.cnf

[server default]
# mysql root user and password
user=root
password=123456

# mysql log locations
#master_binlog_dir=/mysql/data

# user to use SSH around the cluster
ssh_user=root

# repl user and password
repl_user=repl
repl_password=123456

# working directory on the manager
manager_log=/root/mha/log/manager.log
manager_workdir=/root/mha/log
#remote_workdir=/mysql/mha/log

# MHA ping interval
ping_interval=5

## scripts to support MHA functions
## secondary_check_script= /usr/bin/masterha_secondary_check -s remote_host1 -s remote_host2
master_ip_failover_script= /root/mha/scripts/master_ip_failover
## shutdown_script= /usr/local/masterha/scripts/power_manager
## report_script= /usr/local/masterha/scripts/send_report
master_ip_online_change_script= /root/mha/scripts/master_ip_online_change

# masters
[server1]
hostname=10.243.95.3
port=3306
candidate_master=1
master_binlog_dir=/opt/mysql/binlog
remote_workdir=/root/mha/log

[server2]
hostname=10.243.95.4
port=3306
candidate_master=1
check_repl_delay=0
master_binlog_dir=/opt/mysql/binlog
remote_workdir=/root/mha/log

[server3]
hostname=10.243.95.5
port=3306?
candidate_master=1
check_repl_delay=0
master_binlog_dir=/opt/mysql/binlog
remote_workdir=/root/mha/log

2.5 切換腳本

1)創建一些目錄

#建立mha的文件夾
mkdir -p /root/mha/conf
mkdir -p /root/mha/log?
mkdir -p /root/mha/scripts
chmod -R 777 /root/mha/scripts

2)新建腳本

  • 在線切換腳本
  • vi /root/mha/scripts/master_ip_online_change
  • #!/usr/bin/env perl

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

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

    use strict;
    use warnings FATAL => 'all';

    use Getopt::Long;
    use MHA::DBHelper;
    use MHA::NodeUtil;
    use Time::HiRes qw( sleep gettimeofday tv_interval );
    use Data::Dumper;

    my $_tstart;
    my $_running_interval = 0.1;
    my (
    ? $command, $ssh_user, ? ? ? ? $orig_master_host, $orig_master_ip,
    ? $orig_master_port, $orig_master_user, $orig_master_password,
    ? $new_master_host, ?$new_master_ip, ? ?$new_master_port,
    ? $new_master_user, ?$new_master_password
    );

    my $vip = '192.168.90.30'; ?# writer Virtual IP
    my $gw = `route|grep default|awk '{print \$2}'`;
    chomp($gw);
    my $interface = 'eth0'; ?# network interface
    my $ssh_start_vip = "ip addr add $vip/32 dev $interface";
    my $ssh_ping = "arping -U -I $interface -s $vip $gw -c 5";
    my $ssh_stop_vip = "ip addr del $vip/32 dev $interface";

    GetOptions(
    ? 'command=s' ? ? ? ? ? ? ?=> \$command,
    ? 'ssh_user=s' ? ? ? ? ? ? ? ?=> \$ssh_user,
    ? 'orig_master_host=s' ? ? => \$orig_master_host,
    ? 'orig_master_ip=s' ? ? ? => \$orig_master_ip,
    ? 'orig_master_port=i' ? ? => \$orig_master_port,
    ? 'orig_master_user=s' ? ? => \$orig_master_user,
    ? 'orig_master_password=s' => \$orig_master_password,
    ? 'new_master_host=s' ? ? ?=> \$new_master_host,
    ? 'new_master_ip=s' ? ? ? ?=> \$new_master_ip,
    ? 'new_master_port=i' ? ? ?=> \$new_master_port,
    ? 'new_master_user=s' ? ? ?=> \$new_master_user,
    ? 'new_master_password=s' ?=> \$new_master_password,
    );

    $ssh_user = 'root' unless ($ssh_user);

    exit &main();

    sub current_time_us {
    ? my ( $sec, $microsec ) = gettimeofday();
    ? my $curdate = localtime($sec);
    ? return $curdate . " " . sprintf( "%06d", $microsec );
    }

    sub sleep_until {
    ? my $elapsed = tv_interval($_tstart);
    ? if ( $_running_interval > $elapsed ) {
    ? ? sleep( $_running_interval - $elapsed );
    ? }
    }

    sub get_threads_util {
    ? my $dbh ? ? ? ? ? ? ? ? ? ?= shift;
    ? my $my_connection_id ? ? ? = shift;
    ? my $running_time_threshold = shift;
    ? my $type ? ? ? ? ? ? ? ? ? = shift;
    ? $running_time_threshold = 0 unless ($running_time_threshold);
    ? $type ? ? ? ? ? ? ? ? ? = 0 unless ($type);
    ? my @threads;

    ? my $sth = $dbh->prepare("SHOW PROCESSLIST");
    ? $sth->execute();

    ? while ( my $ref = $sth->fetchrow_hashref() ) {
    ? ? my $id ? ? ? ? = $ref->{Id};
    ? ? my $user ? ? ? = $ref->{User};
    ? ? my $host ? ? ? = $ref->{Host};
    ? ? my $command ? ?= $ref->{Command};
    ? ? my $state ? ? ?= $ref->{State};
    ? ? my $query_time = $ref->{Time};
    ? ? my $info ? ? ? = $ref->{Info};
    ? ? $info =~ s/^\s*(.*?)\s*$/$1/ if defined($info);
    ? ? next if ( $my_connection_id == $id );
    ? ? next if ( defined($query_time) && $query_time < $running_time_threshold );
    ? ? next if ( defined($command) ? ?&& $command eq "Binlog Dump" );
    ? ? next if ( defined($user) ? ? ? && $user eq "system user" );
    ? ? next
    ? ? ? if ( defined($command)
    ? ? ? && $command eq "Sleep"
    ? ? ? && defined($query_time)
    ? ? ? && $query_time >= 1 );

    ? ? if ( $type >= 1 ) {
    ? ? ? next if ( defined($command) && $command eq "Sleep" );
    ? ? ? next if ( defined($command) && $command eq "Connect" );
    ? ? }

    ? ? if ( $type >= 2 ) {
    ? ? ? next if ( defined($info) && $info =~ m/^select/i );
    ? ? ? next if ( defined($info) && $info =~ m/^show/i );
    ? ? }

    ? ? push @threads, $ref;
    ? }
    ? return @threads;
    }

    sub main {
    ? if ( $command eq "stop" ) {
    ? ? ## Gracefully killing connections on the current master
    ? ? # 1. Set read_only= 1 on the new master
    ? ? # 2. Set read_only= 1 on the current master
    ? ? # 3. Kill current queries
    ? ? # 4. Deegister original master's ip to the catalog database
    ? ? # * Any database access failure will result in script die.
    ? ? my $exit_code = 1;
    ? ? eval {
    ? ? ? ## Setting read_only=1 on the new master (to avoid accident)
    ? ? ? my $new_master_handler = new MHA::DBHelper();

    ? ? ? # args: hostname, port, user, password, raise_error(die_on_error)_or_not
    ? ? ? $new_master_handler->connect( $new_master_ip, $new_master_port,
    ? ? ? ? $new_master_user, $new_master_password, 1 );
    ? ? ? print current_time_us() . " Set read_only on the new master.. ";
    ? ? ? $new_master_handler->enable_read_only();
    ? ? ? if ( $new_master_handler->is_read_only() ) {
    ? ? ? ? print "ok.\n";
    ? ? ? }
    ? ? ? else {
    ? ? ? ? die "Failed!\n";
    ? ? ? }
    ? ? ? $new_master_handler->disconnect();

    ? ? ? # Connecting to the orig master, die if any database error happens
    ? ? ? my $orig_master_handler = new MHA::DBHelper();
    ? ? ? $orig_master_handler->connect( $orig_master_ip, $orig_master_port,
    ? ? ? ? $orig_master_user, $orig_master_password, 1 );

    ? ? ? ## Drop application user so that nobody can connect. Disabling per-session binlog beforehand
    ? ? ? $orig_master_handler->disable_log_bin_local();

    ? ? ? ## Waiting for N * 100 milliseconds so that current connections can exit
    ? ? ? my $time_until_read_only = 15;
    ? ? ? $_tstart = [gettimeofday];
    ? ? ? my @threads = get_threads_util( $orig_master_handler->{dbh},
    ? ? ? ? $orig_master_handler->{connection_id} );
    ? ? ? while ( $time_until_read_only > 0 && $#threads >= 0 ) {
    ? ? ? ? if ( $time_until_read_only % 5 == 0 ) {
    ? ? ? ? ? printf
    "%s Waiting all running %d threads are disconnected.. (max %d milliseconds)\n",
    ? ? ? ? ? ? current_time_us(), $#threads + 1, $time_until_read_only * 100;
    ? ? ? ? ? if ( $#threads < 5 ) {
    ? ? ? ? ? ? print Data::Dumper->new( [$_] )->Indent(0)->Terse(1)->Dump . "\n"
    ? ? ? ? ? ? ? foreach (@threads);
    ? ? ? ? ? }
    ? ? ? ? }
    ? ? ? ? sleep_until();
    ? ? ? ? $_tstart = [gettimeofday];
    ? ? ? ? $time_until_read_only--;
    ? ? ? ? @threads = get_threads_util( $orig_master_handler->{dbh},
    ? ? ? ? ? $orig_master_handler->{connection_id} );
    ? ? ? }

    ? ? ? ## Setting read_only=1 on the current master so that nobody(except SUPER) can write
    ? ? ? print current_time_us() . " Set read_only=1 on the orig master.. ";
    ? ? ? $orig_master_handler->enable_read_only();
    ? ? ? if ( $orig_master_handler->is_read_only() ) {
    ? ? ? ? print "ok.\n";
    ? ? ? }
    ? ? ? else {
    ? ? ? ? die "Failed!\n";
    ? ? ? }

    ? ? ? ## Waiting for M * 100 milliseconds so that current update queries can complete
    ? ? ? my $time_until_kill_threads = 5;
    ? ? ? @threads = get_threads_util( $orig_master_handler->{dbh},
    ? ? ? ? $orig_master_handler->{connection_id} );
    ? ? ? while ( $time_until_kill_threads > 0 && $#threads >= 0 ) {
    ? ? ? ? if ( $time_until_kill_threads % 5 == 0 ) {
    ? ? ? ? ? printf
    "%s Waiting all running %d queries are disconnected.. (max %d milliseconds)\n",
    ? ? ? ? ? ? current_time_us(), $#threads + 1, $time_until_kill_threads * 100;
    ? ? ? ? ? if ( $#threads < 5 ) {
    ? ? ? ? ? ? print Data::Dumper->new( [$_] )->Indent(0)->Terse(1)->Dump . "\n"
    ? ? ? ? ? ? ? foreach (@threads);
    ? ? ? ? ? }
    ? ? ? ? }
    ? ? ? ? sleep_until();
    ? ? ? ? $_tstart = [gettimeofday];
    ? ? ? ? $time_until_kill_threads--;
    ? ? ? ? @threads = get_threads_util( $orig_master_handler->{dbh},
    ? ? ? ? ? $orig_master_handler->{connection_id} );
    ? ? ? }

    ? ? ? ## Terminating all threads
    ? ? ? print current_time_us() . " Killing all application threads..\n";
    ? ? ? $orig_master_handler->kill_threads(@threads) if ( $#threads >= 0 );
    ? ? ? print current_time_us() . " done.\n";
    ? ? ? $orig_master_handler->enable_log_bin_local();
    ? ? ? $orig_master_handler->disconnect();

    ? ? ? ## Deregister original master's VIP
    ? ? ? print "Disabling the VIP on old master: $orig_master_host \n";
    ? ? ? &stop_vip();
    ? ? ? $exit_code = 10;
    ? ? ??
    ? ? ? ## After finishing the script, MHA executes FLUSH TABLES WITH READ LOCK
    ? ? ? $exit_code = 0;
    ? ? };
    ? ? if ($@) {
    ? ? ? warn "Got Error: $@\n";
    ? ? ? exit $exit_code;
    ? ? }
    ? ? exit $exit_code;
    ? }
    ? elsif ( $command eq "start" ) {
    ? ? # 1. Register new master's ip to the catalog database

    # We don't return error even though activating updatable accounts/ip failed so that we don't interrupt slaves' recovery.
    # If exit code is 0 or 10, MHA does not abort
    ? ? my $exit_code = 10;
    ? ? eval {
    ? ? ? ## Update master ip on the catalog database, etc
    ? ? ? print "Enabling the VIP - $vip on the new master - $new_master_host \n";
    ? ? ? &start_vip();
    ? ? ??
    ? ? ? $exit_code = 0;
    ? ? };
    ? ? if ($@) {
    ? ? ? warn "Got Error: $@\n";
    ? ? ? exit $exit_code;
    ? ? }
    ? ? exit $exit_code;
    ? }
    ? elsif ( $command eq "status" ) {

    ? ? # do nothing
    ? ? exit 0;
    ? }
    ? else {
    ? ? &usage();
    ? ? exit 1;
    ? }
    }

    # A simple system call that enable the VIP on the new master?
    sub start_vip() {
    ? ? `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
    ? ? `ssh $ssh_user\@$new_master_host \" $ssh_ping \"`;
    }

    # A simple system call that disable the VIP on the old_master
    sub stop_vip() {
    ? ? `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
    }

    sub usage {
    ? print
    "Usage: master_ip_online_change --command=start|stop|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";
    ? die;
    }

    注:若提示用戶無repl權限且確認已添加,檢查mysql.user表下的repl用戶是否存在其它host存在repl權限為N的行,因為mha檢查到任意行存在N即返回失敗

    2.6.3 故障自動切換驗證

    1、開啟三個數據庫節點實例,確保主從復制狀態正常(執行2.6.2操作)

    2、后臺打開監控程序,并持續監控日志

  • 4、完成切換

    MySQL之高可用組件MHA

    故障切換內容:

    • 檢查到master不通,嘗試繼續ping檢查,達到閾值5觸發自動切換
    • 若配置文件未指定候選節點,根據日志量選舉新的master主節點
    • 若主節點未宕機,移除原master節點的vip
    • 其他節點與新的master建立復制關系,主從復制同步中
    • 新master節點啟動vip,完成切換
    2.6.4 手動切換

    1)主節點已宕機

  • 2)主節點未宕機

文章鏈接: http://www.qzkangyuan.com/9824.html

文章標題:MySQL之高可用組件MHA

文章版權:夢飛科技所發布的內容,部分為原創文章,轉載請注明來源,網絡轉載文章如有侵權請聯系我們!

聲明:本站所有文章,如無特殊說明或標注,均為本站原創發布。任何個人或組織,在未征得本站同意時,禁止復制、盜用、采集、發布本站內容到任何網站、書籍等各類媒體平臺。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。

給TA打賞
共{{data.count}}人
人已打賞
投稿分享

怎樣防止租用服務器數據丟失問題

2022-9-8 12:30:57

建站教程投稿分享

MySQL配置Keepalived讀寫分離

2022-9-8 13:03:26

0 條回復 A文章作者 M管理員
    暫無討論,說說你的看法吧
?
個人中心
購物車
優惠劵
今日簽到
有新私信 私信列表
搜索
主站蜘蛛池模板: 汉寿县| 永城市| 阳谷县| 东安县| 馆陶县| 武胜县| 东兰县| 湄潭县| 雅安市| 淮南市| 南投市| 安塞县| 金坛市| 读书| 西华县| 日土县| 漳州市| 广河县| 祁门县| 叶城县| 登封市| 金川县| 南开区| 镇江市| 铜梁县| 丽江市| 汉沽区| 永宁县| 汽车| 长白| 汕尾市| 广安市| 甘洛县| 年辖:市辖区| 贵州省| 兰考县| 阿图什市| 志丹县| 诏安县| 喀什市| 西乌珠穆沁旗|