亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MySQL MHA高可用環境部署

發布時間:2020-07-28 12:00:49 來源:網絡 閱讀:685 作者:凜冬一壺酒 欄目:MySQL數據庫

一,安裝MHA基本環境

安裝MHA節點

(1)基本環境說明


角色IP地址主機名     

=========================================     

主機192.168.1.121節點1     

從機192.168.1.122節點2     

從機192.168.1.123節點3     

監視主機192.168.1.125節點5 

(2)在node1,node2,node3,node5中操作:

#vi / etc / hosts

192.168.1.121 node1   

192.168.1.122 node2     

192.168.1.123 node3     

192.168.1.125 node5

安裝MHA節點節點軟件包:

#rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm#yum    

install perl-DBD-MySQL perl-CPAN -y     

#tar xf mha4mysql -node-0.56.tar.gz     

#cd mha4mysql-node-0.56     

#perl Makefile.PL        

#make && make install    

 

安裝MHA

在節點5管理節點上操作:注:MHA管理器主機也是需要安裝MHA節點,MHA管理器

#yum install perl-DBD-MySQL perl-CPAN perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-time-HiRes -y   

#tar xf mha4mysql-manager-0.56.tar.gz     

#cd mha4mysql-manager -0.56     

#perl Makefile.PL     

#make && make install

#說明:安裝的腳本程序都在/ usr / local / bin /目錄下。   

 

3.節點間配置SSH登錄無密碼驗證(MHA主機之間使用密鑰登錄)

在node5(Monitor)中:

#ssh-keygen -t rsa   

#ssh-copy-id -i /root/.ssh/id_rsa.pub root @ node1     

#ssh-copy-id -i /root/.ssh/id_rsa.pub root @ node2 

#ssh-     copy-id -i /root/.ssh/id_rsa.pub root @ node3

在node1(Master)中:

#ssh-keygen -t rsa   

#ssh-copy-id -i /root/.ssh/id_rsa.pub root @ node2     

#ssh-copy-id -i /root/.ssh/id_rsa.pub root @ node3 

#ssh-     copy-id -i /root/.ssh/id_rsa.pub root @ node5

在node2(slave)中:

#ssh-keygen -t rsa   

#ssh-copy-id -i /root/.ssh/id_rsa.pub root @ node1     

#ssh-copy-id -i /root/.ssh/id_rsa.pub root @ node3 

#ssh-     copy-id -i /root/.ssh/id_rsa.pub root @ node5

在node3(slave)中:

#ssh-keygen -t rsa   

#ssh-copy-id -i /root/.ssh/id_rsa.pub root @ node1     

#ssh-copy-id -i /root/.ssh/id_rsa.pub root @ node2 

#ssh-     copy-id -i /root/.ssh/id_rsa.pub root @ node5

二,搭建主從復制環境

主從復制環境配置過程

(1)mysql安裝過程略,但是三節點要創建如下鏈接

node1(主),node2(主備從),node3(從)

注意:創建如下鏈接:   

ln -s / usr / local / mysql / bin / * / usr / local / bin /

node1 my.cnf

server-id = 1   

binlog-format = ROW     

log-bin = master-bin     

log-bin-index = master-bin.index     

log-slave-updates = true     

relay_log_purge = 0

node2 my.cnf

server-id = 2   

binlog-format = ROW     

log-bin = master-bin     

log-bin-index = master-bin.index     

log-slave-updates = true     

relay_log_purge = 0

node3 my.cnf

binlog-format = ROW   

log-bin = mysql-bin     

relay-log = slave-relay-bin     

relay-log-index = slave-relay-bin.index     

log-slave-updates = true     

server-id = 11     

skip-name- resolve     

relay_log_purge = 0

(2)在node1(Master)上備份一份完整的數據:

#mysqldump -uroot -p123456 --master-data = 2 - 單事務-R - triggers -A> all.sql

其中--master-data = 2代表備份時刻記錄主的Binlog位置和位置。

(3)在node1(Master)上創建復制用戶:

mysql>授予復制從機*。*到'123456'確定的'repl'@'192.168.1.%';    

刷新權限;

(4)查看主庫備份時的binlog名稱和位置,MASTER_LOG_FILE和MASTER_LOG_POS:

#head -n 30 all.sql | grep'CHANGE MASTER TO'

- CHANGE MASTER TO MASTER_LOG_FILE ='master-bin.000004',MASTER_LOG_POS = 120;

(5)把備份復制到192.168.1.122和192.168.1.123

#scp all.sql   

192.168.1.122:/root/#scp all.sql 192.168.1.123:/root/

(6)分別在兩臺服務器上導入備份,執行復制相關命令

在node2,node3主機上操作:

#mysql -uroot -p123456 <all.sql

停止從站

CHANGE MASTER TO     

MASTER_HOST ='192.168.1.121',     

MASTER_USER ='repl',     

MASTER_PASSWORD ='123456',     

MASTER_LOG_FILE ='master-bin.000004',     

MASTER_LOG_POS = 120;

mysql> start slave;   

顯示從屬狀態\ G

創建MHA管理用戶,在主上創建。

將*。*的所有權限授予'123456'標識為'root'@'192.168.1.%'的權限;    

刷新權限;

 

三,配置Keepal VIP

vip配置可以采用兩種方式,一種通過keepalived的方式管理虛擬ip的浮動;一人是通過腳本方式,本文通過keepalived方式實現

1.在node1(Master)與node2(備選主節點)安裝keepalived。

#wget的http://www.keepalived.org/software/keepalived-1.2.12.tar.gz    

#焦油XF的keepalived-1.2.12.tar.gz     

#CD的keepalived-1.2.12     

#的./configure前綴= / usr / local / keepalived     

#make && make install     

#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/#cp     

/ usr / local / keepalived / etc / sysconfig / keepalived / etc / sysconfig /     

#mkdir / etc / keepalived     

#cp /usr/local/keepalived/etc/keepalived/keepalived.conf / etc / keepalived /     

#cp / usr / local / keepalived / sbin / keepalived / usr / sbin /

配置keepalived的配置文件,在node1(master)上配置操作如下:

注意:keepalived配置成備份 - 備份,即IP地址切換后,主起來后IP地址不切換,本文監控腳本由MHA提供,keepalived不提供對mysqld的監控。

#vi /etc/keepalived/keepalived.conf     

!保持配置文件

global_defs {   

     notification_email {     

     abc@163.com     

   }     

   notification_email_from dba@dbserver.com     

   smtp_server 127.0.0.1     

   smtp_connect_timeout 30     

   router_id MySQL-HA     

}

vrrp_instance VI_1 {   

    state BACKUP     

    interface eth0     

    virtual_router_id 51     

    priority 150     

    advert_int 1     

    nopreempt

    身份驗證{   

    auth_type PASS     

    auth_pass 1111     

    }

    virtual_ipaddress {   

        192.168.1.130     

    }     

}

配置keepalived的配置文件,在node2(備用節點)上配置操作如下:

#vi /etc/keepalived/keepalived.conf     

!保持配置文件

global_defs {   

     notification_email {     

     abc@163.com     

   }     

   notification_email_from dba@dbserver.com     

   smtp_server 127.0.0.1     

   smtp_connect_timeout 30     

   router_id MySQL-HA     

}

vrrp_instance VI_1 {   

    state BACKUP     

    interface eth0     

    virtual_router_id 51     

    priority 120     

    advert_int 1     

    nopreempt

    身份驗證{   

    auth_type PASS     

    auth_pass 1111     

    }

    virtual_ipaddress {   

        192.168.1.130     

    }     

}

4. node1,node2啟動keepalived服務

#service keepalived start   

#chkconfig keepalived on

5. node1查看VIP啟動情況

[root @ 

node1]#ip a   1:lo:<LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN     

    link / loopback 00:00:00:00:00:00 brd 00:00:00:00:00 :00     

    inet 127.0.0.1/8 scope host lo     

    inet6 :: 1/128 scope host     

       valid_lft forever preferred_lft forever     

2:eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000     

    link / ether 00:0c :29:4e:53:71 brd ff:ff:ff:ff:ff:ff     

    inet 192.168.1.121/24 brd 192.168.1.255范圍全局eth0     

    inet 192.168.1.130/32范圍全局eth0     

    inet6 fe80 :: 20c:29ff: fe4e:5371/64范圍鏈接     

       valid_lft永遠preferred_lft永遠

四,配置MHA

監控創建MHA的工作目錄,并且創建相關配置文件(在軟件包解壓后的目錄里面有樣例配置文件)。

#mkdir -p / etc / masterha   

#mkdir -p / var / log / masterha / app1     

#cp mha4mysql-manager-0.56 / samples / conf / app1.cnf / etc / masterha /

修改app1.cnf配置文件,修改后的文件內容如下:

#cat /etc/masterha/app1.cnf

[server default]   

manager_workdir = / var / log / masterha / app1     

manager_log = / var / log / masterha / app1 / manager.log     

master_binlog_dir = / usr / local / mysql / data /     

master_ip_failover_script = / usr / local / bin / master_ip_failover     

master_ip_online_change_script = / usr / local / bin / master_ip_online_change     

password = 123456     

user = root     

ping_interval = 1     

remote_workdir = / tmp     

repl_password = 123456     

repl_user = repl     

report_script = / usr / local / bin / send_report     

ssh_user = root  

[server1]   

hostname = 192.168.1.121     

port = 3306

[server2]   

hostname = 192.168.1.122     

port = 3306     

candidate_master = 1     

check_repl_delay = 0

[server3]   

hostname = 192.168.1.123     

port = 3306

說明:     

master_ip_failover_script = / usr / local / bin / master_ip_failover            #MHA自動切換執行的腳本,需要修改     

master_ip_online_change_script = / usr / local / bin / master_ip_online_change #手動

切換  需要執行的腳本,需要修改     report_script = / usr / local / bin / send_report 切換                                時發送郵件進行報告,需要修改

2.設置中繼日志的清除方式(在每個從節點上):

(1)在節點2,節點3從節點上操作:

將relay_log_purge = 0加入my.cnf配置文件,前面已經配置。

(2)設定定期清理繼電器腳本(node2,node3上操作):

#貓purge_relay_log.sh     

#!/斌/ bash的     

用戶=根     

的passwd = 123456     

端口= 3306     

LOG_DIR = '/數據/ masterha /日志'     

WORK_DIR = '/數據'     

清除= '在/ usr / local / bin目錄/ purge_relay_logs'

如果[!-d $ log_dir]   

then     

   mkdir $ log_dir -p     

fi

$ purge --user = $ user --password = $ passwd --disable_relay_log_purge --port = $ port --workdir = $ work_dir >> $ log_dir / purge_relay_logs.log 2>&1

配置定時計劃任務

#crontab -e   0 4 * * * / bin / bash /root/purge_relay_log.sh

 

要求把keepalived服務引入MHA,我們只需要修改切換是觸發的腳本文件master_ip_failover即可,在該腳本中添加在master發生宕機時對keepalived的處理。

(1)編輯腳本/ usr / local / bin / master_ip_failover,修改后如下:

#vi / usr / local / bin / master_ip_failover     

#!/ usr / bin / env perl     

use strict;    

使用警告FATAL =>'all';

使用Getopt :: Long;

我的(   

    $命令,$ ssh_user,$ orig_master_host,$ orig_master_ip,     

    $ orig_master_port,$ new_master_host,$ new_master_ip,$ new_master_port     

);

我的$ vip ='192.168.1.130';   

我的$ ssh_start_vip =“/etc/init.d/keepalived start”;    

我的$ ssh_stop_vip =“/etc/init.d/keepalived stop”;

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 = '=> \ $ orig_master_port,     

    'new_master_host = s'=> \ $ new_master_host,     

    'new_master_ip = s'=> \ $ new_master_ip,     

    'new_master_port = i'=> \ $ new_master_port,     

);

exit&main();   

sub main {     

    print“\ n \ nIN SCRIPT TEST ==== $ ssh_stop_vip == $ ssh_start_vip === \ n \ n”;    

    if($ command eq“stop”|| $ command eq“stopssh”){     

        my $ exit_code = 1;    

        eval {     

            print“禁用舊主機上的VIP:$ orig_master_host \ n”;    

            &stop_vip();    

            $ exit_code = 0;    

        };    

        if($ @){     

            warn“Got Error:$ @ \ n”;    

            退出$ exit_code;    

        }     

        exit $ exit_code;    

    }     

    elsif($ command eq“start”){

        我的$ exit_code = 10;   

        eval {     

            print“啟用VIP - $ vip on the new master - $ new_master_host \ n”;    

            &start_vip();    

            $ exit_code = 0;    

        };    

        if($ @){     

            warn $ @;    

            退出$ exit_code;    

        }     

        exit $ exit_code;    

    } { 

    el } { 

        “ } ”{     “ }     ”\“”    

        退出0;    

    }     

    else {     

        &usage();    

        出口1;    

    }     

}     

sub start_vip(){     

    `ssh $ ssh_user \ @ $ new_master_host \“$ ssh_start_vip \”`;    

}     

#一個簡單的系統調用,禁用在old_master 

子上的VIP     stop_vip(){     

    `ssh $ ssh_user \ @ $ orig_master_host \“$ ssh_stop_vip \”`;    

}}

sub usage {   

    print     

    “用法: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“;    

}

(2)編輯腳本master_ip_online_change,修改后如下:

#!/ usr / bin / env perl

#版權所有(C)2011 DeNA有限公司   

##     

這個程序是免費的軟件; 您可以

根據

#自由軟件基金會發布的GNU通用公共許可證的條款重新分配和/或修改     #    許可證的版本2或     

#(根據您的選擇)任何更高版本。    

##     

這個程序是分發的,希望它是有用的,     

但沒有任何的保證; 甚至沒有

#適銷性或適用于特定用途的默示保證     。有關

詳細信息,請參閱     #GNU通用公共許可證。    

##     

您應該已經收到了GNU通用公共許可證

#的副本     以及該程序; 如果不,    

##注意:這是一個示例腳本,不完整。根據您的環境修改腳本。

使用嚴格   

使用警告FATAL =>'all';

使用Getopt :: Long;   

使用MHA :: DBHelper;    

使用MHA :: NodeUtil;    

使用Time :: HiRes qw(sleep gettimeofday tv_interval);    

使用Data :: Dumper;

我的$ _tstart;   

我的$ _running_interval = 0.1;    

我的(     

  $命令,$ orig_master_is_new_slave,$ orig_master_host,     

  $ orig_master_ip,$ orig_master_port,$ orig_master_user,     

  $ orig_master_password,$ orig_master_ssh_user,$ new_master_host,     

  $ new_master_ip,$ new_master_port,$ new_master_user,     

  $ new_master_password,$ new_master_ssh_user     

)    

我的$ vip ='192.168.1.130/24';    

我的$ key ='1';    

我的$ ssh_start_vip =“/ sbin / ifconfig eth0:$ key $ vip”;    

我的$ ssh_stop_vip =“/ sbin / ifconfig eth0:$ key down”;    

我的$ orig_master_ssh_port = 22;    

我的$ new_master_ssh_port = 22;    

exit&main();

sub current_time_us {   

  my($ sec,$ microsec)= gettimeofday();    

  我的$ curdate = localtime($ sec);    

  返回$ 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_connection_id = shift;    

  我的$ running_time_threshold = shift;    

  我的$ type = shift;    

  $ running_time_threshold = 0,除非($ running_time_threshold);    

  $ type = 0,除非($ type);    

  我的@threads;

  我的$ sth = $ dbh-> prepare(“SHOW PROCESSLIST”);   

  $ sth-> execute();

  while(my $ ref = $ sth-> fetchrow_hashref()){   

    my $ id = $ ref - > {Id};    

    我的$ user = $ ref - > {User};    

    我的$ host = $ ref - > {Host};    

    我的$ command = $ ref - > {Command};    

    我的$ state = $ ref - > {State};    

    我的$ query_time = $ ref - > {Time};    

    我的$ 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”);    

    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);    

    }

    推送@threads,$ ref;   

  }     

  return @threads;    

}

sub main {   

  if($ command eq“stop”){     

    ##正當地殺死當前主機上的連接     

    #1.在新主機

    #2 上設置read_only = 1。DROP     USER使得沒有應用用戶可以建立新的連接     

    #3。在當前主機

    #4 上設置read_only = 1。殺死當前查詢     

    #*任何數據庫訪問失敗都會導致腳本死機     。    

    我的$ exit_code = 1;    

    eval {     

      ##在新主機上設置read_only = 1(以避免意外)     

      我的$ 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);    

      打印current_time_us()。“設置read_only在新的主人..”;    

      $ new_master_handler-> enable_read_only();    

      if($ new_master_handler-> is_read_only()){     

        print“ok。\ n”;    

      }     

      else {     

        die“Failed!\ n”;    

      }     

      $ new_master_handler-> disconnect();

      #連接到原始主機,如果發生任何數據庫錯誤,則會死亡   

      $ orig_master_handler = new MHA :: DBHelper();    

      $ orig_master_handler-> connect($ orig_master_ip,$ orig_master_port,     

        $ orig_master_user,$ orig_master_password,1);

      ##刪除應用程序用戶,以便沒有人可以連接。事先禁用每會話binlog   

      $ orig_master_handler-> disable_log_bin_local();    

      打印current_time_us()。“在原始主機上吸引應用用戶.. \ n”;    

      #FIXME_xxx_drop_app_user($ orig_master_handler);

      等待N * 100毫秒,以便當前的連接可以退出   

      我的$ time_until_read_only = 15;    

      $ _tstart = [gettimeofday];    

      我的@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等待所有正在運行的%d線程斷開連接..(最大%d毫秒)\ 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});    

      }}

      ##在當前主設備上設置read_only = 1,以便沒有人(SUPER除外)可以寫入   

      print_time_us()。“在原始主機上設置read_only = 1”。    

      $ orig_master_handler-> enable_read_only();    

      if($ orig_master_handler-> is_read_only()){     

        print“ok。\ n”;    

      }     

      else {     

        die“Failed!\ n”;    

      }}

      等待M * 100毫秒,以便當前的更新查詢可以完成   

      我的$ 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等待所有運行的%d查詢斷開連接..(最大%d毫秒)\ 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();

      ## After finishing the script, MHA executes FLUSH TABLES WITH READ LOCK   

      eval {    

      `ssh -p$orig_master_ssh_port $orig_master_ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;    

        };    

        if ($@) {    

            warn $@;    

        }    

      $exit_code = 0;    

    };    

    if ($@) {    

      warn "Got Error: $@\n";    

      exit $exit_code;    

    }    

    exit $exit_code;    

  }    

  elsif ( $command eq "start" ) {    

    ## Activating master ip on the new master    

    # 1. Create app user with write privileges    

    # 2. Moving backup script if needed    

    # 3. 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 {    

      my $new_master_handler = new MHA::DBHelper();

      # args: hostname, port, user, password, raise_error_or_not   

      $new_master_handler->connect( $new_master_ip, $new_master_port,    

        $new_master_user, $new_master_password, 1 );

      ## Set read_only=0 on the new master   

      $new_master_handler->disable_log_bin_local();    

      print current_time_us() . " Set read_only=0 on the new master.\n";    

      $new_master_handler->disable_read_only();

      ## Creating an app user on the new master   

      print current_time_us() . " Creating app user on the new master..\n";    

      #FIXME_xxx_create_app_user($new_master_handler);    

      $new_master_handler->enable_log_bin_local();    

      $new_master_handler->disconnect();

      ## Update master ip on the catalog database, etc   

      `ssh -p$new_master_ssh_port $new_master_ssh_user\@$new_master_host \" $ssh_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;    

  }    

}

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;    

}

(3) 編輯腳本send_report,修改后如下:

#!/usr/bin/perl

use strict;   

use warnings FATAL => 'all';    

use Mail::Sender;    

use Getopt::Long;

#new_master_host and new_slave_hosts are set only when recovering master succeeded   

my ( $dead_master_host, $new_master_host, $new_slave_hosts, $subject, $body );    

my $smtp='smtp.163.com';    

my $mail_from='xxxx';    

my $mail_user='xxxxx';    

my $mail_pass='xxxxx';    

my $mail_to=['xxxx','xxxx'];    

GetOptions(    

  'orig_master_host=s' => \$dead_master_host,    

  'new_master_host=s'  => \$new_master_host,    

  'new_slave_hosts=s'  => \$new_slave_hosts,    

  'subject=s'          => \$subject,    

  'body=s'             => \$body,    

);

mailToContacts($smtp,$mail_from,$mail_user,$mail_pass,$mail_to,$subject,$body);

sub mailToContacts {   

    my ( $smtp, $mail_from, $user, $passwd, $mail_to, $subject, $msg ) = @_;    

    open my $DEBUG, "> /tmp/monitormail.log"    

        or die "Can't open the debug      file:$!\n";    

    my $sender = new Mail::Sender {    

        ctype       => 'text/plain; charset=utf-8',    

        encoding    => 'utf-8',    

        smtp        => $smtp,    

        from        => $mail_from,    

        auth        => 'LOGIN',    

        TLS_allowed => '0',    

        authid      => $user,    

        authpwd     => $passwd,    

        to          => $mail_to,    

        subject     => $subject,    

        debug       => $DEBUG    

    };

    $sender->MailMsg(   

        {   msg   => $msg,    

            debug => $DEBUG    

        }    

    ) or print $Mail::Sender::Error;    

    return 1;    

}

# Do whatever you want here

exit 0;

 

五、MHA的日常管理

1. 檢查SSH配置(node5 Monitor 監控節點上操作),如下:

# masterha_check_ssh --conf=/etc/masterha/app1.cnf

Sun May  1 22:05:12 2016 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.   

Sun May  1 22:05:12 2016 - [info] Reading application default configuration from /etc/masterha/app1.cnf..    

Sun May  1 22:05:12 2016 - [info] Reading server configuration from /etc/masterha/app1.cnf..    

Sun May  1 22:05:12 2016 - [info] Starting SSH connection tests..    

Sun May  1 22:05:14 2016 - [debug]     

Sun May  1 22:05:12 2016 - [debug]  Connecting via SSH from root@192.168.1.121(192.168.1.121:22) to root@192.168.1.122(192.168.1.122:22)..    

Sun May  1 22:05:13 2016 - [debug]   ok.    

Sun May  1 22:05:13 2016 - [debug]  Connecting via SSH from root@192.168.1.121(192.168.1.121:22) to root@192.168.1.123(192.168.1.123:22)..    

Sun May  1 22:05:13 2016 - [debug]   ok.    

Sun May  1 22:05:14 2016 - [debug]     

Sun May  1 22:05:13 2016 - [debug]  Connecting via SSH from root@192.168.1.122(192.168.1.122:22) to root@192.168.1.121(192.168.1.121:22)..    

Sun May  1 22:05:13 2016 - [debug]   ok.    

Sun May  1 22:05:13 2016 - [debug]  Connecting via SSH from root@192.168.1.122(192.168.1.122:22) to root@192.168.1.123(192.168.1.123:22)..    

Sun May  1 22:05:14 2016 - [debug]   ok.    

Sun May  1 22:05:14 2016 - [debug]     

Sun May  1 22:05:13 2016 - [debug]  Connecting via SSH from root@192.168.1.123(192.168.1.123:22) to root@192.168.1.121(192.168.1.121:22)..    

Sun May  1 22:05:14 2016 - [debug]   ok.    

Sun May  1 22:05:14 2016 - [debug]  Connecting via SSH from root@192.168.1.123(192.168.1.123:22) to root@192.168.1.122(192.168.1.122:22)..    

Sun May  1 22:05:14 2016 - [debug]   ok.    

Sun May  1 22:05:14 2016 - [info] All SSH connection tests passed successfully.

2. 檢查整個復制環境狀況(node5 監控節點上操作),如下:

# masterha_check_repl --conf=/etc/masterha/app1.cnf

Sun May  1 22:46:44 2016 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.   

Sun May  1 22:46:44 2016 - [info] Reading application default configuration from /etc/masterha/app1.cnf..    

Sun May  1 22:46:44 2016 - [info] Reading server configuration from /etc/masterha/app1.cnf..    

Sun May  1 22:46:44 2016 - [info] MHA::MasterMonitor version 0.56.    

Sun May  1 22:46:45 2016 - [info] GTID failover mode = 0    

Sun May  1 22:46:45 2016 - [info] Dead Servers:    

Sun May  1 22:46:45 2016 - [info] Alive Servers:    

Sun May  1 22:46:45 2016 - [info]   192.168.1.121(192.168.1.121:3306)    

Sun May  1 22:46:45 2016 - [info]   192.168.1.122(192.168.1.122:3306)    

Sun May  1 22:46:45 2016 - [info]   192.168.1.123(192.168.1.123:3306)    

Sun May  1 22:46:45 2016 - [info] Alive Slaves:    

Sun May  1 22:46:45 2016 - [info]   192.168.1.122(192.168.1.122:3306)  Version=5.6.29-log (oldest major version between slaves) log-bin:enabled    

Sun May  1 22:46:45 2016 - [info]     Replicating from 192.168.1.121(192.168.1.121:3306)    

Sun May  1 22:46:45 2016 - [info]     Primary candidate for the new Master (candidate_master is set)    

Sun May  1 22:46:45 2016 - [info]   192.168.1.123(192.168.1.123:3306)  Version=5.6.29-log (oldest major version between slaves) log-bin:enabled    

Sun May  1 22:46:45 2016 - [info]     Replicating from 192.168.1.121(192.168.1.121:3306)    

Sun May  1 22:46:45 2016 - [info] Current Alive Master: 192.168.1.121(192.168.1.121:3306)    

Sun May  1 22:46:45 2016 - [info] Checking slave configurations..    

Sun May  1 22:46:45 2016 - [info]  read_only=1 is not set on slave 192.168.1.122(192.168.1.122:3306).    

Sun May  1 22:46:45 2016 - [warning]  relay_log_purge=0 is not set on slave 192.168.1.122(192.168.1.122:3306).    

Sun May  1 22:46:45 2016 - [info]  read_only=1 is not set on slave 192.168.1.123(192.168.1.123:3306).    

Sun May  1 22:46:45 2016 - [warning]  relay_log_purge=0 is not set on slave 192.168.1.123(192.168.1.123:3306).    

Sun May  1 22:46:45 2016 - [info] Checking replication filtering settings..    

Sun May  1 22:46:45 2016 - [info]  binlog_do_db= , binlog_ignore_db=     

Sun May  1 22:46:45 2016 - [info]  Replication filtering check ok.    

Sun May  1 22:46:45 2016 - [info] GTID (with auto-pos) is not supported    

Sun May  1 22:46:45 2016 - [info] Starting SSH connection tests..    

Sun May  1 22:46:46 2016 - [info] All SSH connection tests passed successfully.    

Sun May  1 22:46:46 2016 - [info] Checking MHA Node version..    

Sun May  1 22:46:47 2016 - [info]  Version check ok.    

Sun May  1 22:46:47 2016 - [info] Checking SSH publickey authentication settings on the current master..    

Sun May  1 22:46:47 2016 - [info] HealthCheck: SSH to 192.168.1.121 is reachable.    

Sun May  1 22:46:47 2016 - [info] Master MHA Node version is 0.56.    

Sun May  1 22:46:47 2016 - [info] Checking recovery script configurations on 192.168.1.121(192.168.1.121:3306)..    

Sun May  1 22:46:47 2016 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/usr/local/mysql/data/ --output_file=/tmp/save_binary_logs_test --manager_version=0.56 --start_file=master-bin.000008     

Sun May  1 22:46:47 2016 - [info]   Connecting to root@192.168.1.121(192.168.1.121:22)..     

  Creating /tmp if not exists..    ok.    

  Checking output directory is accessible or not..    

   ok.    

  Binlog found at /usr/local/mysql/data/, up to master-bin.000008    

Sun May  1 22:46:48 2016 - [info] Binlog setting check done.    

Sun May  1 22:46:48 2016 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..    

Sun May  1 22:46:48 2016 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='root' --slave_host=192.168.1.122 --slave_ip=192.168.1.122 --slave_port=3306 --workdir=/tmp --target_version=5.6.29-log --manager_version=0.56 --relay_log_info=/usr/local/mysql/data/relay-log.info  --relay_dir=/usr/local/mysql/data/  --slave_pass=xxx    

Sun May  1 22:46:48 2016 - [info]   Connecting to root@192.168.1.122(192.168.1.122:22)..     

  Checking slave recovery environment settings..    

    Opening /usr/local/mysql/data/relay-log.info ... ok.    

    Relay log found at /usr/local/mysql/data, up to node2-relay-bin.000002    

    Temporary relay log file is /usr/local/mysql/data/node2-relay-bin.000002    

    Testing mysql connection and privileges..Warning: Using a password on the command line interface can be insecure.    

done.    

    Testing mysqlbinlog output.. done.    

    Cleaning up test file(s).. done.    

Sun May  1 22:46:48 2016 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='root' --slave_host=192.168.1.123 --slave_ip=192.168.1.123 --slave_port=3306 --workdir=/tmp --target_version=5.6.29-log --manager_version=0.56 --relay_log_info=/usr/local/mysql/data/relay-log.info  --relay_dir=/usr/local/mysql/data/  --slave_pass=xxx    

Sun May  1 22:46:48 2016 - [info]   Connecting to root@192.168.1.123(192.168.1.123:22)..     

  Checking slave recovery environment settings..    

    Opening /usr/local/mysql/data/relay-log.info ... ok.    

    Relay log found at /usr/local/mysql/data, up to slave-relay-bin.000012    

    Temporary relay log file is /usr/local/mysql/data/slave-relay-bin.000012    

    Testing mysql connection and privileges..Warning: Using a password on the command line interface can be insecure.    

done.    

    Testing mysqlbinlog output.. done.    

    Cleaning up test file(s).. done.    

Sun May  1 22:46:48 2016 - [info] Slaves settings check done.    

Sun May  1 22:46:48 2016 - [info]     

192.168.1.121(192.168.1.121:3306) (current master)    

+--192.168.1.122(192.168.1.122:3306)    

+--192.168.1.123(192.168.1.123:3306)

Sun May  1 22:46:48 2016 - [info] Checking replication health on 192.168.1.122..   

Sun May  1 22:46:48 2016 - [info]  ok.    

Sun May  1 22:46:48 2016 - [info] Checking replication health on 192.168.1.123..    

Sun May  1 22:46:48 2016 - [info]  ok.    

Sun May  1 22:46:48 2016 - [info] Checking master_ip_failover_script status:    

Sun May  1 22:46:48 2016 - [info]   /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.1.121 --orig_master_ip=192.168.1.121 --orig_master_port=3306

IN SCRIPT TEST====/etc/init.d/keepalived stop==/etc/init.d/keepalived start===

Checking the Status of the script.. OK    

Sun May  1 22:46:48 2016 - [info]  OK.    

Sun May  1 22:46:48 2016 - [warning] shutdown_script is not defined.    

Sun May  1 22:46:48 2016 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.   

[root@node5 masterha]#

3. 開啟MHA Manager監控(node5操作)如下:

# mkdir -p  /var/log/masterha/app1/   

# nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 & 

參數說明:   

--remove_dead_master_conf     #該參數代表當發生主從切換后,老的主庫的ip將會從配置文件中移除。    

--manger_log                  #日志存放位置    

--ignore_last_failover        #在缺省情況下,如果MHA檢測到連續發生宕機,會生成app1.failover.complete文件,會造成MHA管理進程無法啟動。

4. 查看MHA Manager監控是否正常:

# masterha_check_status --conf=/etc/masterha/app1.cnf   

app1 (pid:2480) is running(0:PING_OK), master:192.168.1.121

5. 查看啟動日志(node5操作)如下:

# tail -n20 /var/log/masterha/app1/manager.log

6. 關閉MHA Manage監控:

(1) 關閉

# masterha_stop --conf=/etc/masterha/app1.cnf

(2) 啟動

# nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1

六、MHA Failover切換

1. 自動Failover切換

(1) 模擬master mysql關閉   

(2) VIP將會切換到node2    

(3) /etc/masterha/app1.cnf中將原主服務器配置文件清掉。    

(4) masterha_manager監控進程會自動退出關閉,并在/var/log/masterha/app1下生成app1.failover.complete文件,manager.log會記錄全過程,從服務器會自動從新的主服務器復制。    

(5) 原主服務器mysqld啟動的,需要清掉/var/log/masterha/app1下生成app1.failover.complete文件,添加node1配置文件到/etc/masterha/app1.cnf,通過manager.log中的記錄的故障點,重新同步主服務器,成為從節點。

2. 手動Failover切換

(1) 先停MHA Manager進程。   

masterha_stop --conf=/etc/masterha/app1.cnf    

(2) 停掉master mysqld    

(3) 手動切換,在Manager主機上操作如下:    

# masterha_master_switch --master_state=dead --conf=/etc/masterha/app1.cnf --dead_master_host=192.168.1.122 --dead_master_port=3306 --new_master_host=192.168.1.121 --new_master_port=3306 --ignore_last_failover

通過觀察日志可以觀察切換全過程。   

(4) 如上節方式恢復節點為從服務器。

3.  正常運行情況下切換(Master正在運行)

等補充。

4. 小結

通過對MMM,MHA的環境搭建測試,MHA由于采用復制架構,原理簡單,在一些對數據要求比較高的環境,為了保證可靠性,最好與半同步結合使用。


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

教育| 肇东市| 前郭尔| 孟津县| 福建省| 岱山县| 嘉善县| 仪征市| 安国市| 九寨沟县| 汝阳县| 卢龙县| 扎鲁特旗| 宜宾县| 长春市| 射洪县| 德钦县| 双鸭山市| 赤水市| 恩施市| 资源县| 忻州市| 江西省| 自贡市| 林芝县| 云南省| 阳东县| 镇远县| 沙洋县| 常德市| 获嘉县| 双鸭山市| 五原县| 黄大仙区| 彝良县| 仁寿县| 石狮市| 栾城县| 浦县| 方山县| 永福县|