MYSQL高可用架构之MHA

本篇博客内容写的有点赘余,
读者可以参考https://blog.csdn.net/aaaaaab_/article/details/81583657
实验环境的搭建:

准备: 在主从复制(GTID)的基础上面去做一主两从,必须保证一主二从可以实时
同步数据。
主从复制可以参考
https://blog.csdn.net/aaaaaab_/article/details/81515111
server1:172.25.38.1(master)
server2:172.25.38.2(slave)
server3:172.25.38.3(slave)
server4:172.25.38.4(mha)

在server1:

[root@server1 MHA]# ls
master_ip_failover
master_ip_online_change
mha4mysql-manager-0.56-0.el6.noarch.rpm
mha4mysql-node-0.56-0.el6.noarch.rpm
perl-Config-Tiny-2.12-7.1.el6.noarch.rpm
perl-Email-Date-Format-1.002-5.el6.noarch.rpm
perl-Log-Dispatch-2.27-1.el6.noarch.rpm
perl-Mail-Sender-0.8.16-3.el6.noarch.rpm
perl-Mail-Sendmail-0.79-12.el6.noarch.rpm
perl-MIME-Lite-3.027-2.el6.noarch.rpm
perl-MIME-Types-1.28-2.el6.noarch.rpm
perl-Parallel-ForkManager-0.7.9-1.el6.noarch.rpm
send_report
[root@server1 MHA]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm -y

这里写图片描述

[root@server1 .ssh]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
72:c7:15:ad:e4:17:bb:b0:a3:0f:15:48:d8:45:f0:4d root@server1
The key's randomart image is:
+--[ RSA 2048]----+
|         oo++.E  |
|        ...o.+o  |
|          .o+..o |
|         . .+.o  |
|      . S o .+ . |
|       o . .o .  |
|          .. .   |
|          ..     |
|           ..    |
+-----------------+
[root@server1 .ssh]# ls
id_rsa  id_rsa.pub  known_hosts

这里写图片描述

[root@server1 .ssh]# ssh-copy-id root@172.25.38.3
root@172.25.38.3's password: 
Now try logging into the machine, with "ssh 'root@172.25.38.3'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@server1 .ssh]# ssh-copy-id root@172.25.38.2
Now try logging into the machine, with "ssh 'root@172.25.38.2'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

这里写图片描述
在server2:

[root@server2 MHA]# ls
master_ip_failover
master_ip_online_change
mha4mysql-manager-0.56-0.el6.noarch.rpm
mha4mysql-node-0.56-0.el6.noarch.rpm
perl-Config-Tiny-2.12-7.1.el6.noarch.rpm
perl-Email-Date-Format-1.002-5.el6.noarch.rpm
perl-Log-Dispatch-2.27-1.el6.noarch.rpm
perl-Mail-Sender-0.8.16-3.el6.noarch.rpm
perl-Mail-Sendmail-0.79-12.el6.noarch.rpm
perl-MIME-Lite-3.027-2.el6.noarch.rpm
perl-MIME-Types-1.28-2.el6.noarch.rpm
perl-Parallel-ForkManager-0.7.9-1.el6.noarch.rpm
send_report
[root@server2 MHA]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm -y

这里写图片描述

[root@server2 MHA]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
f9:df:dc:8e:9f:b1:69:50:77:88:a8:63:3b:f0:da:7f root@server2
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|           . . . |
|         .. . ..o|
|        S.    . o|
|      . +.   .   |
|       + o.   .. |
|       .+  .Eo +=|
|      ...o... =*+|
+-----------------+

这里写图片描述

[root@server2 MHA]# ssh-copy-id 172.25.38.1
[root@server2 MHA]# ssh-copy-id 172.25.38.3

这里写图片描述

[root@server2 MHA]# mysql -p
mysql> grant replication slave on *.* to yiyi@'172.25.38.%' identified by 'YIYI=bushe24';
Query OK, 0 rows affected, 1 warning (0.00 sec)

这里写图片描述
在server3重新配置slave数据库的初始化

[root@server3 ~]# ls
MHA
[root@server3 ~]# cd MHA/
[root@server3 MHA]# ls
master_ip_failover
master_ip_online_change
mha4mysql-manager-0.56-0.el6.noarch.rpm
mha4mysql-node-0.56-0.el6.noarch.rpm
perl-Config-Tiny-2.12-7.1.el6.noarch.rpm
perl-Email-Date-Format-1.002-5.el6.noarch.rpm
perl-Log-Dispatch-2.27-1.el6.noarch.rpm
perl-Mail-Sender-0.8.16-3.el6.noarch.rpm
perl-Mail-Sendmail-0.79-12.el6.noarch.rpm
perl-MIME-Lite-3.027-2.el6.noarch.rpm
perl-MIME-Types-1.28-2.el6.noarch.rpm
perl-Parallel-ForkManager-0.7.9-1.el6.noarch.rpm
send_report
[root@server3 MHA]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm -y

这里写图片描述

[root@server3 ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
2b:8b:36:e7:dc:aa:b0:65:10:13:9b:62:b4:16:c2:3f root@server3
The key's randomart image is:
+--[ RSA 2048]----+
|oo.              |
|o.o+             |
|.+*              |
|o. E             |
|  . .   S        |
|   .     .       |
|  . o . .        |
|   =oo.+         |
|  ..o==..        |
+-----------------+

这里写图片描述

[root@server3 ~]# ssh-copy-id 172.25.38.1
[root@server3 ~]# ssh-copy-id 172.25.38.2

这里写图片描述

mysql> grant replication slave on *.* to yiyi@'172.25.38.%' identified by 'YIYI=bushe24';
Query OK, 0 rows affected, 1 warning (0.00 sec)

这里写图片描述
在server4(mha主机):

[root@server4 ~]# ls
MHA
[root@server4 ~]# cd MHA/
[root@server4 MHA]# ls
master_ip_failover
master_ip_online_change
mha4mysql-manager-0.56-0.el6.noarch.rpm
mha4mysql-node-0.56-0.el6.noarch.rpm
perl-Config-Tiny-2.12-7.1.el6.noarch.rpm
perl-Email-Date-Format-1.002-5.el6.noarch.rpm
perl-Log-Dispatch-2.27-1.el6.noarch.rpm
perl-Mail-Sender-0.8.16-3.el6.noarch.rpm
perl-Mail-Sendmail-0.79-12.el6.noarch.rpm
perl-MIME-Lite-3.027-2.el6.noarch.rpm
perl-MIME-Types-1.28-2.el6.noarch.rpm
perl-Parallel-ForkManager-0.7.9-1.el6.noarch.rpm
send_report
[root@server4 MHA]# yum install * -y

这里写图片描述

mkdir /etc/mha                 ##创建工作目录workdir
mkdir /etc/mha                       ##创建配置文件所在目录
cd /etc/mha/     
vim app.conf
[root@server4 mha]# cat app.conf 
[server default]

manager_workdir=/usr/local/bingrep master_ip_failover /etc/masterha/app1.cnf

manager_log=/usr/local/bin/mha.log

master_binlog_dir=/var/lib/mysql

#master_ip_failover_script=/usr/local/bin/master_ip_failover

master_ip_online_change_script=/usr/local/bin/master_ip_online_change

user=root

password=Aa@+123456

ping_interval=1

remote_workdir=/tmp

repl_user=yiyi

repl_password=YIYI+bushe24

report_script=/usr/local/send_report

#secondary_check_script=/usr/local/bin/masterha_secondary_check -s server03 -s server02

#shutdown_script=""

ssh_user=root



[server1]

hostname=172.25.38.1

port=3306



[server2]

hostname=172.25.38.2

port=3306

candidate_master=1

check_repl_delay=0



[server3]

hostname=172.25.38.3

port=3306

#no_master=1

这里写图片描述

[root@server4 mha]# masterha_check_ssh --conf=/etc/mha/app.conf 
Thu Aug  9 15:40:10 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Aug  9 15:40:10 2018 - [info] Reading application default configuration from /etc/mha/app.conf..
Thu Aug  9 15:40:10 2018 - [info] Reading server configuration from /etc/mha/app.conf..
Thu Aug  9 15:40:10 2018 - [info] Starting SSH connection tests..
Thu Aug  9 15:40:11 2018 - [debug] 
Thu Aug  9 15:40:10 2018 - [debug]  Connecting via SSH from root@172.25.38.1(172.25.38.1:22) to root@172.25.38.2(172.25.38.2:22)..
Thu Aug  9 15:40:11 2018 - [debug]   ok.
Thu Aug  9 15:40:11 2018 - [debug]  Connecting via SSH from root@172.25.38.1(172.25.38.1:22) to root@172.25.38.3(172.25.38.3:22)..
Thu Aug  9 15:40:11 2018 - [debug]   ok.
Thu Aug  9 15:40:12 2018 - [debug] 
Thu Aug  9 15:40:11 2018 - [debug]  Connecting via SSH from root@172.25.38.2(172.25.38.2:22) to root@172.25.38.1(172.25.38.1:22)..
Thu Aug  9 15:40:11 2018 - [debug]   ok.
Thu Aug  9 15:40:11 2018 - [debug]  Connecting via SSH from root@172.25.38.2(172.25.38.2:22) to root@172.25.38.3(172.25.38.3:22)..
Thu Aug  9 15:40:12 2018 - [debug]   ok.
Thu Aug  9 15:40:12 2018 - [debug] 
Thu Aug  9 15:40:11 2018 - [debug]  Connecting via SSH from root@172.25.38.3(172.25.38.3:22) to root@172.25.38.1(172.25.38.1:22)..
Thu Aug  9 15:40:12 2018 - [debug]   ok.
Thu Aug  9 15:40:12 2018 - [debug]  Connecting via SSH from root@172.25.38.3(172.25.38.3:22) to root@172.25.38.2(172.25.38.2:22)..
Thu Aug  9 15:40:12 2018 - [debug]   ok.
Thu Aug  9 15:40:12 2018 - [info] All SSH connection tests passed successfully

这里写图片描述
通过脚本检测状态:

[root@server4 mha]# touch /usr/local/bin/master_ip_failover
[root@server4 mha]# cd /usr/local/bin/
[root@server4 bin]# touch /usr/local/bin/master_ip_online_change
[root@server4 bin]# vim master_ip_failover 
[root@server4 bin]# vim master_ip_online_change 
[root@server4 bin]# chmod +x master_ip_failover 
[root@server4 bin]# chmod +x master_ip_online_change 

这里写图片描述
保证三台虚拟机的/etc/my.cnf一致server-id区分即可,在每个虚拟机重启服务即可

[root@server1 ~]# scp /etc/my.cnf server2:/etc/my.cnf
The authenticity of host 'server2 (172.25.38.2)' can't be established.
RSA key fingerprint is 2f:cc:a7:97:6a:d8:41:5b:90:f3:91:b6:e8:87:4c:b3.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'server2' (RSA) to the list of known hosts.
my.cnf                                                               100% 1034     1.0KB/s   00:00    
[root@server1 ~]# scp /etc/my.cnf server3:/etc/my.cnf
my.cnf                                                               100% 1034     1.0KB/s   00:00    

这里写图片描述
开启MHA Manager监控

[root@server4 bin]# masterha_check_repl --conf=/etc/mha/app.conf
Thu Aug  9 23:48:32 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Aug  9 23:48:32 2018 - [info] Reading application default configuration from /etc/mha/app.conf..
Thu Aug  9 23:48:32 2018 - [info] Reading server configuration from /etc/mha/app.conf..
Thu Aug  9 23:48:32 2018 - [info] MHA::MasterMonitor version 0.56.
Thu Aug  9 23:48:32 2018 - [info] GTID failover mode = 1
Thu Aug  9 23:48:32 2018 - [info] Dead Servers:
Thu Aug  9 23:48:32 2018 - [info] Alive Servers:
Thu Aug  9 23:48:32 2018 - [info]   172.25.254.1(172.25.254.1:3306)
Thu Aug  9 23:48:32 2018 - [info]   172.25.254.2(172.25.254.2:3306)
Thu Aug  9 23:48:32 2018 - [info]   172.25.254.3(172.25.254.3:3306)
Thu Aug  9 23:48:32 2018 - [info] Alive Slaves:
Thu Aug  9 23:48:32 2018 - [info]   172.25.254.2(172.25.254.2:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Thu Aug  9 23:48:32 2018 - [info]     GTID ON
Thu Aug  9 23:48:32 2018 - [info]     Replicating from 172.25.254.1(172.25.254.1:3306)
Thu Aug  9 23:48:32 2018 - [info]     Primary candidate for the new Master (candidate_master is set)
Thu Aug  9 23:48:32 2018 - [info]   172.25.254.3(172.25.254.3:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Thu Aug  9 23:48:32 2018 - [info]     GTID ON
Thu Aug  9 23:48:32 2018 - [info]     Replicating from 172.25.254.1(172.25.254.1:3306)
Thu Aug  9 23:48:32 2018 - [info] Current Alive Master: 172.25.254.1(172.25.254.1:3306)
Thu Aug  9 23:48:32 2018 - [info] Checking slave configurations..
Thu Aug  9 23:48:32 2018 - [info]  read_only=1 is not set on slave 172.25.254.2(172.25.254.2:3306).
Thu Aug  9 23:48:32 2018 - [info]  read_only=1 is not set on slave 172.25.254.3(172.25.254.3:3306).
Thu Aug  9 23:48:32 2018 - [info] Checking replication filtering settings..
Thu Aug  9 23:48:32 2018 - [info]  binlog_do_db= , binlog_ignore_db= 
Thu Aug  9 23:48:32 2018 - [info]  Replication filtering check ok.
Thu Aug  9 23:48:32 2018 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Thu Aug  9 23:48:32 2018 - [info] Checking SSH publickey authentication settings on the current master..
Thu Aug  9 23:48:32 2018 - [info] HealthCheck: SSH to 172.25.254.1 is reachable.
Thu Aug  9 23:48:32 2018 - [info] 
172.25.254.1(172.25.254.1:3306) (current master)
 +--172.25.254.2(172.25.254.2:3306)
 +--172.25.254.3(172.25.254.3:3306)

Thu Aug  9 23:48:32 2018 - [info] Checking replication health on 172.25.254.2..
Thu Aug  9 23:48:32 2018 - [info]  ok.
Thu Aug  9 23:48:32 2018 - [info] Checking replication health on 172.25.254.3..
Thu Aug  9 23:48:32 2018 - [info]  ok.
Thu Aug  9 23:48:32 2018 - [warning] master_ip_failover_script is not defined.
Thu Aug  9 23:48:32 2018 - [warning] shutdown_script is not defined.
Thu Aug  9 23:48:32 2018 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

这里写图片描述

[root@server4 bin]# cat /etc/mha/app.conf 
[server default]
manager_workdir=/etc/mha
manager_log=/usr/local/bin/mha.log
master_binlog_dir=/var/lib/mysql
#master_ip_failover_script=/usr/local/bin/master_ip_failover
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
user=root
password=Aa@+123456
ping_interval=1
remote_workdir=/tmp
repl_user=repl
repl_password=Yakexi+007
report_script=/usr/local/send_report
#secondary_check_script=/usr/local/bin/masterha_secondary_check -s server03 -s server02
#shutdown_script=""
ssh_user=root
[server1]
hostname=172.25.254.1
port=3306
[server2]
hostname=172.25.254.2
port=3306
candidate_master=1
check_repl_delay=0
[server3]
hostname=172.25.254.3
port=3306
#no_master=1

这里写图片描述
通过master_check_status脚本查看Manager的状态

[root@server4 bin]# masterha_check_status --conf=/etc/mha/app.conf
app is stopped(2:NOT_RUNNING).
解决报错打开监控即可
[root@server4 bin]# nohup masterha_manager --conf=/etc/mha/app.conf --remove_dead_master_conf --ignore_last_failover < /dev/null > /usr/local/mha/mha.log 2>&1 & 
[1] 1646
[root@server4 bin]# masterha_check_status --conf=/etc/mha/app.confapp (pid:1646) is running(0:PING_OK), master:172.25.254.1

这里写图片描述
查看日志会出来成功:

[root@server4 bin]# tail -10 /usr/local/bin/mha.log
172.25.254.1(172.25.254.1:3306) (current master)
 +--172.25.254.2(172.25.254.2:3306)
 +--172.25.254.3(172.25.254.3:3306)

Thu Aug  9 23:52:03 2018 - [warning] master_ip_failover_script is not defined.
Thu Aug  9 23:52:03 2018 - [warning] shutdown_script is not defined.
Thu Aug  9 23:52:03 2018 - [info] Set master ping interval 1 seconds.
Thu Aug  9 23:52:03 2018 - [warning] secondary_check_script is not defined. It is highly recommended setting it to check master reachability from two or more routes.
Thu Aug  9 23:52:03 2018 - [info] Starting ping health check on 172.25.254.1(172.25.254.1:3306)..
Thu Aug  9 23:52:03 2018 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..

这里写图片描述

[root@server4 ~]# vim /etc/mha/app.conf    
master_ip_failover_script=/usr/local/bin/master_ip_failover
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
#将注释的两行打开,脚本拷到指定位置

分享两个脚本都需要更改虚拟IP:

[root@server4 bin]# cat master_ip_failover 
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;

my (
    $command,          $ssh_user,        $orig_master_host, $orig_master_ip,
    $orig_master_port, $new_master_host, $new_master_ip,    $new_master_port
);

my $vip = '172.25.254.100/24';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";

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,
    '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 "Disabling the VIP on old master: $orig_master_host \n";
            &stop_vip();
            $exit_code = 0;
        };
        if ($@) {
            warn "Got Error: $@\n";
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "start" ) {

        my $exit_code = 10;
        eval {
            print "Enabling the VIP - $vip on the new master - $new_master_host \n";
            &start_vip();
            $exit_code = 0;
        };
        if ($@) {
            warn $@;
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "status" ) {
        print "Checking the Status of the script.. OK \n";
        exit 0;
    }
    else {
        &usage();
        exit 1;
    }
}

sub start_vip() {
    `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
sub stop_vip() {
     return 0  unless  ($ssh_user);
    `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}

sub usage {
    print
    "Usage: 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";
}
[root@server4 bin]# cat master_ip_online_change 
#!/usr/bin/env perl
use strict;  
use warnings FATAL =>'all';  

use Getopt::Long;  

my $vip = '172.25.254.100/24';  # Virtual IP  
my $key = "1";  
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";  
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";  
my $exit_code = 0;  

my (  
  $command,              $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,  
);  
GetOptions(  
  'command=s'                => \$command,  
  'orig_master_is_new_slave' => \$orig_master_is_new_slave,  
  '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,  
  'orig_master_ssh_user=s'   => \$orig_master_ssh_user,  
  '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,  
  'new_master_ssh_user=s'    => \$new_master_ssh_user,  
);  


exit &main();  

sub main {  

#print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";  

if ( $command eq "stop" || $command eq "stopssh" ) {  

        # $orig_master_host, $orig_master_ip, $orig_master_port are passed.  
        # If you manage master ip address at global catalog database,  
        # invalidate orig_master_ip here.  
        my $exit_code = 1;  
        eval {  
            print "\n\n\n***************************************************************\n";  
            print "Disabling the VIP - $vip on old master: $orig_master_host\n";  
            print "***************************************************************\n\n\n\n";  
&stop_vip();  
            $exit_code = 0;  
        };  
        if ($@) {  
            warn "Got Error: $@\n";  
            exit $exit_code;  
        }  
        exit $exit_code;  
}  
elsif ( $command eq "start" ) {  

        # all arguments are passed.  
        # If you manage master ip address at global catalog database,  
        # activate new_master_ip here.  
        # You can also grant write access (create user, set read_only=0, etc) here.  
my $exit_code = 10;  
        eval {  
            print "\n\n\n***************************************************************\n";  
            print "Enabling the VIP - $vip on new master: $new_master_host \n";  
            print "***************************************************************\n\n\n\n";  
&start_vip();  
            $exit_code = 0;  
        };  
        if ($@) {  
            warn $@;  
            exit $exit_code;  
        }  
        exit $exit_code;  
}  
elsif ( $command eq "status" ) {  
        print "Checking the Status of the script.. OK \n";  
        `ssh $orig_master_ssh_user\@$orig_master_host \" $ssh_start_vip \"`;  
        exit 0;  
}  
else {  
&usage();  
        exit 1;  
}  
}  

# A simple system call that enable the VIP on the new master  
sub start_vip() {  
`ssh $new_master_ssh_user\@$new_master_host \" $ssh_start_vip \"`;  
}  
# A simple system call that disable the VIP on the old_master  
sub stop_vip() {  
`ssh $orig_master_ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;  
}  

sub usage {  
print  
"Usage: 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";  
}

进行测试:

[root@server1 etc]# /etc/init.d/mysqld stop  将主端关闭mysql
Stopping mysqld:                                           [  OK  ]
[root@server1 etc]# 

server2会出来顶替成为主端:

[root@server2 MHA]# mysql -pAa@+123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 46
Server version: 5.7.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show master status;
+------------------+----------+--------------+------------------+----------------------------------------------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                                                                |
+------------------+----------+--------------+------------------+----------------------------------------------------------------------------------+
| mysql-bin.000001 |      445 |              |                  | bac7ebff-9bde-11e8-8767-5254004b3455:1-2,
c152f584-9bde-11e8-a530-525400e41981:1 |
+------------------+----------+--------------+------------------+----------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> show slave status\G
Empty set (0.00 sec)

这里写图片描述
将server1重新打开还是主端:

[root@server1 etc]# /etc/init.d/mysqld start
Starting mysqld:                                           [  OK  ]
[root@server1 etc]# mysql -pAa@+123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000003 |      194 |              |                  | bac7ebff-9bde-11e8-8767-5254004b3455:1-2 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)

mysql> show slave status\G
Empty set (0.00 sec)

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值