MHA部署实现高可用

Posted security-guard

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MHA部署实现高可用相关的知识,希望对你有一定的参考价值。

一、启动MHA

[root@c733 ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf  --remove_dead_master_conf --ignore_last_failover < /dev/null >  /var/log/mha/app1/manager.log  2>&1 &

查看MHA状态

[root@c733 ~]# masterha_check_status --conf=/etc/mha/app1.cnf 

关闭MHA

[root@c733 ~]# masterha_stop --conf=/etc/mha/app1.cnf

从库重新加入主库

[root@c733 ~]# grep "CHANGE MASTER TO MASTER" /var/log/mha/app1/manager.log | tail -1

二、测试MHA故障转移

1、停掉c731主库192.168.37.31

[root@c731 ~]# systemctl stop mysql

查看c733的slave状态,发现master_host变成了192.168.37.32

[root@c733 ~]# mysql -uroot -p123456 -e start slave;
Warning: Using a password on the command line interface can be insecure.
[root@c731 ~]# mysql -uroot -p123456 -e show slave status G
Warning: Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.37.32
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 231
               Relay_Log_File: mysqld-relay-bin.000003
                Relay_Log_Pos: 401
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

查看c732的master状态

[root@c732 ~]# mysql -p123456 -e show master status;

技术图片

 

 三、MHA故障还原

[root@c733 ~]# grep "CHANGE MASTER TO MASTER" /var/log/mha/app1/manager.log | tail -1
Fri Feb 14 02:57:59 2020 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST=192.168.37.31, MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER=rep, MASTER_PASSWORD=xxx;

重启数据库,重新change_master

[root@c731 ~]# systemctl restart mysql

[root@c731 ~]# mysql -uroot -p123456 -e "CHANGE MASTER TO MASTER_HOST=‘192.168.37.32‘,MASTER_PORT=3306, MASTER_AUTO_POSITION=1,MASTER_USER=‘rep‘,MASTER_PASSWORD=‘123456‘;"
Warning: Using a password on the command line interface can be insecure.

[root@c731 ~]# mysql -uroot -p123456 -e start slave;
Warning: Using a password on the command line interface can be insecure.

[root@c731 ~]# mysql -uroot -p123456 -e show slave status G
Warning: Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.37.32
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 231
               Relay_Log_File: mysqld-relay-bin.000003
                Relay_Log_Pos: 401
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

此时,重新在将【server1】标签添加到MHA配置文件,并且重新启动MHA

[root@c733 bin]# sed -i -e /server2/i                                                                 [server1]                                                     
hostname=192.168.37.31 port=3306  /etc/mha/app1.cnf

 

[root@c733 ~]# ps -ef | grep mha
root      13478  11518  0 03:50 pts/1    00:00:00 grep --color=auto mha
[root@c733 ~]# cat /etc/mha/app1.cnf 
[server default]
manager_log=/var/log/mha/app1/manager.log
manager_workdir=/var/log/mha/app1
master_binlog_dir=/var/lib/mysql
master_ip_failover_script=/usr/bin/master_ip_failover
password=mha
ping_interval=2
repl_password=123456
repl_user=rep
ssh_user=root
user=mha

[server1]
hostname=192.168.37.32
port=3306

[server3]
hostname=192.168.37.33
ignore_fail=1
no_master=1
port=3306

发生故障时, MHA做了什么?
①当作为主库的c731.上的MySQL宕机以后, mha通过检测发现c731的mysq|宕机了, 那么会将binlog日志最全的从库
( c732 )立刻提升为主库,而其他的从库会指向新的主库进行再次同步。
②MHA会自己结束自己的进程,还会将/etc/mha/app1.cnf配置文件中,坏掉的那台机器剔除。

四、 配置vip飘逸

1、ip漂移的两种方式:

①通过keepalived的方式,管理虚拟ip的漂移

②通过mha自带脚本的方式,管理虚拟ip的漂移 ,用mha自带的vip漂移脚本,那个提升为主,就漂移到那个上面

要根据binlog最新的slave提升

2、MHA脚本方式

修改MHA配置文件

[root@c733 ~]# cat /etc/mha/app1.cnf 
[server default]
manager_log=/var/log/mha/app1/manager.log
manager_workdir=/var/log/mha/app1
master_binlog_dir=/var/lib/mysql
master_ip_failover_script=/usr/bin/master_ip_failover   //添加此处代码
password=mha
ping_interval=2
repl_password=123456
repl_user=rep
ssh_user=root
user=mha

编写漂移脚本

vim /usr/bin/master_ip_failover
技术图片
 1 [root@c733 bin]# cat master_ip_failover 
 2 #!/usr/bin/env perl
 3 
 4 use strict;
 5 use warnings FATAL => all;
 6 
 7 use Getopt::Long;
 8 
 9 my (
10     $command,          $ssh_user,        $orig_master_host, $orig_master_ip,
11     $orig_master_port, $new_master_host, $new_master_ip,    $new_master_port
12 );
13 
14 my $vip = 192.168.37.100/24;
15 my $key = 1;
16 my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
17 my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
18 
19 GetOptions(
20     command=s          => $command,
21     ssh_user=s         => $ssh_user,
22     orig_master_host=s => $orig_master_host,
23     orig_master_ip=s   => $orig_master_ip,
24     orig_master_port=i => $orig_master_port,
25     new_master_host=s  => $new_master_host,
26     new_master_ip=s    => $new_master_ip,
27     new_master_port=i  => $new_master_port,
28 );
29 
30 exit &main();
31 
32 sub main {
33 
34     print "

IN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===

";
35 
36     if ( $command eq "stop" || $command eq "stopssh" ) {
37 
38         my $exit_code = 1;
39         eval {
40             print "Disabling the VIP on old master: $orig_master_host 
";
41             &stop_vip();
42             $exit_code = 0;
43         };
44         if ($@) {
45             warn "Got Error: $@
";
46             exit $exit_code;
47         }
48         exit $exit_code;
49     }
50     elsif ( $command eq "start" ) {
51 
52         my $exit_code = 10;
53         eval {
54             print "Enabling the VIP - $vip on the new master - $new_master_host 
";
55             &start_vip();
56             $exit_code = 0;
57         };
58         if ($@) {
59             warn $@;
60             exit $exit_code;
61         }
62         exit $exit_code;
63     }
64     elsif ( $command eq "status" ) {
65         print "Checking the Status of the script.. OK 
";
66         exit 0;
67     }
68     else {
69         &usage();
70         exit 1;
71     }
72 }
73 
74 sub start_vip() {
75     `ssh $ssh_user@$new_master_host " $ssh_start_vip "`;
76 }
77 sub stop_vip() {
78      return 0  unless  ($ssh_user);
79     `ssh $ssh_user@$orig_master_host " $ssh_stop_vip "`;
80 }
81 
82 sub usage {
83     print
84     "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
";
85 }
View Code

注意修改脚本中的网卡名和ip地址

192.168.37.100    ens33

添加执行权限

chmod +x /usr/bin/master_ip_failover

3、重启MHA

[root@c733 ~]# masterha_stop --conf=/etc/mha/app1.cnf 
Stopped app1 successfully.
[1]+  退出 1                nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1
[root@c733 ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf  --remove_dead_master_conf --ignore_last_failover < /dev/null >  /var/log/mha/app1/manager.log  2>&1 &
[1] 12132
[root@c733 ~]# masterha_check_status --conf=/etc/mha/app1.cnf 
app1 (pid:12132) is running(0:PING_OK), master:192.168.37.32

五、模拟主库宕机vip漂移

1、关闭c732上的主库mysql

[root@c732 ~]# ifconfig ens33:1 192.168.37.100/24
[root@c732 ~]# ip a show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:84:52:12 brd ff:ff:ff:ff:ff:ff
    inet 192.168.37.32/24 brd 192.168.37.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.37.100/24 brd 192.168.37.255 scope global secondary ens33:1
       valid_lft forever preferred_lft forever
    inet6 fe80::ea02:b804:fa8f:a8bb/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::cefd:32cb:6b50:23c3/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::5aae:78b0:a3e4:e112/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

这里的vip漂移有几秒的延迟

[root@c732 ~]# systemctl stop mysql
[root@c732 ~]# ip a show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:84:52:12 brd ff:ff:ff:ff:ff:ff
    inet 192.168.37.32/24 brd 192.168.37.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::ea02:b804:fa8f:a8bb/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::cefd:32cb:6b50:23c3/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::5aae:78b0:a3e4:e112/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

2、现在查看c731上的mysql的master状态和vip

[root@c731 ~]# ip a show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:18:e4:7a brd ff:ff:ff:ff:ff:ff
    inet 192.168.37.31/24 brd 192.168.37.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.37.100/24 brd 192.168.37.255 scope global secondary ens33:1
       valid_lft forever preferred_lft forever
    inet6 fe80::ea02:b804:fa8f:a8bb/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::cefd:32cb:6b50:23c3/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@c731 ~]# mysql -p123456 -e show master statusG
Warning: Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
             File: mysql-bin.000003
         Position: 1256
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 986a9807-4e56-11ea-a705-000c29845212:1-6,
f27d0522-4e44-11ea-a692-000c2918e47a:1-4

 

以上是关于MHA部署实现高可用的主要内容,如果未能解决你的问题,请参考以下文章

MHA部署实现高可用

MySQL高可用之MHA—部署MHA

MHA高可用集群部署及故障切换

MHA的集群架构实现高可用

MHA高可用架构部署配置实例

MHA高可用群集基本部署(纯实战)