MHA集群搭建

Posted diantong

tags:

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

(1).简介

  MHA(Master High Availability)目前在mysql高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Facebook公司)开发,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件。在MySQL故障切换过程中,MHA能做到在0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程中,MHA能在最大程度上保证数据的一致性,以达到真正意义上的高可用。

  该软件由两部分组成:MHA Manager(管理节点)和MHA Node(数据节点)。MHA Manager可以单独部署在一台独立的机器上管理多个master-slave集群,也可以部署在一台slave节点上。MHA Node运行在每台MySQL服务器上,MHA Manager会定时探测集群中的master节点,当master出现故障时,它可以自动将最新数据的slave提升为新的master,然后将所有其他的slave重新指向新的master。整个故障转移过程对应用程序完全透明。

  在MHA自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据的不丢失,但这并不总是可行的。例如,如果主服务器硬件故障或无法通过ssh访问,MHA没法保存二进制日志,只进行故障转移而丢失了最新的数据。使用MySQL 5.5的半同步复制,可以大大降低数据丢失的风险。MHA可以与半同步复制结合起来。如果只有一个slave已经收到了最新的二进制日志,MHA可以将最新的二进制日志应用于其他所有的slave服务器上,因此可以保证所有节点的数据一致性。

  目前MHA主要支持一主多从的架构,要搭建MHA,要求一个复制集群中必须最少有三台数据库服务器,一主二从,即一台充当master,一台充当备用master,另外一台充当从库,因为至少需要三台服务器,出于机器成本的考虑,淘宝也在该基础上进行了改造,目前淘宝TMHA已经支持一主一从。MHA 适合任何存储引擎, 只要能主从复制的存储引擎它都支持,不限于支持事物的 innodb 引擎。另外对于想快速搭建的可以参考:MHA快速搭建(大佬的快速搭建指南)

  我们自己使用其实也可以使用一主一从,但是master主机宕机后无法切换,以及无法补全二进制日志。master的mysqld进程crash(停止工作)后,还是可以切换成功,以及补全二进制日志。

  官方介绍:https://code.google.com/p/mysql-master-ha/(需要梯子)

图01展示了如何通过MHA Manager管理多组主从复制。可以将MHA工作原理总结为如下:

 技术图片

                                 ( 图01 )

(1)从宕机崩溃的master保存二进制日志事件(binlog events);

(2)识别含有最新更新的slave;

(3)应用差异的中继日志(relay log)到其他的slave;

(4)应用从master保存的二进制日志事件(binlog events);

(5)提升一个slave为新的master;

(6)使其他的slave连接新的master进行复制;

  MHA软件由两部分组成,Manager工具包和Node工具包,具体的说明如下。

  Manager工具包主要包括以下几个工具:

masterha_check_ssh              检查MHA的SSH配置状况
masterha_check_repl             检查MySQL复制状况
masterha_manger                 启动MHA
masterha_check_status           检测当前MHA运行状态
masterha_master_monitor         检测master是否宕机
masterha_master_switch          控制故障转移(自动或者手动)
masterha_conf_host              添加或删除配置的server信息

  Node工具包(这些工具通常由MHA Manager的脚本触发,无需人为操作)主要包括以下几个工具:

save_binary_logs                保存和复制master的二进制日志
apply_diff_relay_logs           识别差异的中继日志事件并将其差异的事件应用于其他的slave
filter_mysqlbinlog              去除不必要的ROLLBACK事件(MHA已不再使用这个工具)
purge_relay_logs                清除中继日志(不会阻塞SQL线程)

  注意:为了尽可能的减少主库硬件损坏宕机造成的数据丢失,因此在配置MHA的同时建议配置成MySQL 5.5的半同步复制。关于半同步复制原理各位自己进行查阅。(不是必须)

(2).实验环境

youxi1  192.168.1.6  CentOS7.5  管理节点       mha4mysql-manager,mha4mysql-node

youxi2  192.168.1.7  CentOS7.5  master        Mysql5.7.26,mha4mysql-node

youxi3  192.168.1.8  CentOS7.5  slave1,备用master  Mysql5.7.26,mha4mysql-node

youxi4  192.168.1.9  CentOS7.5  slave2        Mysql5.7.26,mha4mysql-node

(3).所有的服务器都安装mha4mysql-node

  mha4mysql-node是所有服务器都需要安装的,首先安装该软件需要的依赖包

[[email protected] ~]# yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager

  如果报缺少libmysqlclient.so.18,安装mysql-community-libs-compat即可。

  接着上传事先下载好的mha4mysql-node的rpm包,使用rpm安装

[[email protected] ~]# rpm -ivh mha4mysql-node-0.57-0.el7.noarch.rpm 
准备中...                          ################################# [100%]
正在升级/安装...
   1:mha4mysql-node-0.57-0.el7        ################################# [100%]

  查看Node工具包:

[[email protected] ~]# ll /usr/bin/app*,filter*,purge*,save*
-rwxr-xr-x 1 root root 16381 5月  31 2015 /usr/bin/apply_diff_relay_logs
-rwxr-xr-x 1 root root  4807 5月  31 2015 /usr/bin/filter_mysqlbinlog
-rwxr-xr-x 1 root root  8261 5月  31 2015 /usr/bin/purge_relay_logs
-rwxr-xr-x 1 root root  7525 5月  31 2015 /usr/bin/save_binary_logs

(4).在管理节点youxi1上安装mha4mysql-manager

  因为依赖包在安装mha4mysql-node时已经安装了,所以可以直接安装manager。

  上传事先下载好的mha4mysql-manager的rpm包,使用rpm安装。

[[email protected] ~]# rpm -ivh mha4mysql-manager-0.57-0.el7.noarch.rpm 
准备中...                          ################################# [100%]
正在升级/安装...
   1:mha4mysql-manager-0.57-0.el7        ################################# [100%]

(5).ssh互信

  管理节点youxi1要能免密登录主从复制的所有服务器,而不需要反向免密登录。主从复制的所有服务器之间要能免密登录。

  所有节点生成密钥

[[email protected] ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory ‘/root/.ssh‘.
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:
SHA256:+lvjcHIMg1+3TSMXo44vQZSVzPJaios2nOQo74lmNQI [email protected]
The key‘s randomart image is:
+---[RSA 2048]----+
|           =..   |
|        . + +    |
|       o o +  o  |
|  E    .= o o. o |
|   .  .*S+.o+ +  |
|    o +o==o+ * . |
|     =..+ B.o .  |
|    o..o B.o     |
|   o..o o....    |
+----[SHA256]-----+

  接着互相发送密钥

//管理节点youxi1发送给youxi2、youxi3、youxi4
[[email protected] ~]# ssh-copy-id 192.168.1.7
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host ‘192.168.1.7 (192.168.1.7)‘ can‘t be established.
ECDSA key fingerprint is SHA256:j3ee8eoTo2XEv0QxCYmxphMipcNRxC+IONPmt1HwRLg.
ECDSA key fingerprint is MD5:25:e2:b4:08:f2:79:7d:6e:42:84:b5:78:3d:6a:81:20.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]‘s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh ‘192.168.1.7‘"
and check to make sure that only the key(s) you wanted were added.
[[email protected] ~]# ssh-copy-id 192.168.1.8
[[email protected] ~]# ssh-copy-id 192.168.1.9

//master服务器youxi2发送给youxi3、youxi4
[[email protected] ~]# ssh-copy-id 192.168.1.8
[[email protected] ~]# ssh-copy-id 192.168.1.9

//slave1服务器发youxi3送给youxi2、youxi4
[[email protected] ~]# ssh-copy-id 192.168.1.7
[[email protected] ~]# ssh-copy-id 192.168.1.9

//slave2服务器youxi4发送给youxi2、youxi3
[[email protected] bin]# ssh-copy-id 192.168.1.7
[[email protected] bin]# ssh-copy-id 192.168.1.8

  测试是否可以免密登录,测试完这一步就结束了

(6).搭建主从环境

1)配置master

  创建需要同步的数据库,并创建复制用的用户以及监控用的用户

[[email protected] ~]# mysql -uroot -p123456
mysql> create database test_db;
Query OK, 1 row affected (0.00 sec)

mysql> grant replication slave on *.* to ‘slave‘@‘192.168.1.%‘ identified by ‘123456‘;  //复制用的用户
Query OK, 0 rows affected, 1 warning (0.05 sec)

mysql> grant all privileges on *.* to ‘root‘@‘192.168.1.%‘ identified by ‘123456‘;  //监控用的用户
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;  //刷新权限表
Query OK, 0 rows affected (0.00 sec)

  修改配置文件,重启mysqld

[[email protected] ~]# vim /etc/my.cnf
server-id=1
log-bin=mysql-bin-master
binlog-do-db=test_db
[[email protected] ~]# systemctl restart mysqld

  查看master状态

[[email protected] ~]# mysql -uroot -p123456
mysql> show master status\\G
*************************** 1. row ***************************
             File: mysql-bin-master.000001
         Position: 154
     Binlog_Do_DB: test_db
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

  注意:如果防火墙是打开的,记得添加mysql的端口号。

[[email protected] ~]# firewall-cmd --permanent --zone=public --add-port=3306/tcp
success
[[email protected] ~]# firewall-cmd --reload
success
[[email protected] ~]# firewall-cmd --zone=public --list-ports
3306/tcp

2)配置slave1,备用master

  这是一台slave,也是一台master,所以既需要配置slave的信息,也要配置master的信息。

  创建同步数据库,并创建复制用的用户以及监控用的用户

[[email protected] ~]# mysql -uroot -p123456
mysql> create database test_db;
Query OK, 1 row affected (0.00 sec)

mysql> grant replication slave on *.* to ‘slave‘@‘192.168.1.%‘ identified by ‘123456‘;  //复制用的用户
Query OK, 0 rows affected, 1 warning (0.05 sec)

mysql> grant all privileges on *.* to ‘root‘@‘192.168.1.%‘ identified by ‘123456‘;  //监控用的用户
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;  //刷新权限表
Query OK, 0 rows affected (0.01 sec)

  修改配置文件,重启master

[[email protected] ~]# vim /etc/my.cnf
server-id=2
log-bin=mysql-bin-slave1  //一定要存在二进制日志,因为随时有可能升为master
binlog-do-db=test_db
log-slave-updates=1  //一定要同步到二进制日志文件
[[email protected] ~]# systemctl restart mysqld

  修改master信息,并查看slave状态

[[email protected] ~]# mysql -uroot -p123456
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host=‘192.168.1.7‘,
    -> master_user=‘slave‘,
    -> master_password=‘123456‘,
    -> master_log_file=‘mysql-bin-master.000001‘,
    -> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.08 sec)

mysql> start slave;
Query OK, 0 rows affected (0.02 sec)

mysql> show slave status\\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.7
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin-master.000001
Read_Master_Log_Pos: 154
Relay_Log_File: youxi2-relay-bin.000002
Relay_Log_Pos: 327
Relay_Master_Log_File: mysql-bin-master.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

  设置为只读,并关闭中转日志自动清理

mysql> set global read_only=1;  //从表设置为只读,备用master的只读不能写入配置文件,因为有可能会升为master
Query OK, 0 rows affected (0.00 sec)

mysql> set global relay_log_purge=off;  //从表关闭中转日志自动清理,因为MHA从库恢复过程依赖中转日志相关信息
Query OK, 0 rows affected (0.00 sec)

  注意:如果防火墙是打开的,记得添加mysql的端口号。

[[email protected] ~]# firewall-cmd --permanent --zone=public --add-port=3306/tcp
success
[[email protected] ~]# firewall-cmd --reload
success
[[email protected] ~]# firewall-cmd --zone=public --list-ports
3306/tcp

3)配置slave2

  创建同步数据库,并创建监控用的用户

[[email protected] ~]# mysql -uroot -p123456
mysql> create database test_db;
Query OK, 1 row affected (0.00 sec)

mysql> grant all privileges on *.* to ‘root‘@‘192.168.1.%‘ identified by ‘123456‘;  //监控用的用户
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;  //刷新权限表
Query OK, 0 rows affected (0.01 sec)

  修改配置文件,重启mysqld

[[email protected] ~]# vim /etc/my.cnf
server-id=3
[[email protected] ~]# systemctl restart mysqld

  创建同步数据库,修改master信息,查看slave状态

[[email protected] ~]# mysql -uroot -p123456
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host=‘192.168.1.7‘,
    -> master_user=‘slave‘,
    -> master_password=‘123456‘,
    -> master_log_file=‘mysql-bin-master.000001‘,
    -> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.09 sec)

mysql> start slave;
Query OK, 0 rows affected (0.03 sec)

mysql> show slave status\\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.7
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin-master.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: youxi3-relay-bin.000002
                Relay_Log_Pos: 327
        Relay_Master_Log_File: mysql-bin-master.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

  设置为只读,并关闭中转日志自动清理

mysql> set global read_only=1;  //从库设置为只读,非备用master可以写进配置文件
Query OK, 0 rows affected (0.00 sec)

mysql> set global relay_log_purge=off;  //从表关闭中转日志自动清理,因为MHA从库恢复过程依赖中转日志相关信息
Query OK, 0 rows affected (0.00 sec)

(7).配置MHA

  创建工作目录

[[email protected] ~]# mkdir /etc/masterha
[[email protected] ~]# mkdir -p /var/log/masterha/app1

  在/usr/local/bin目录下创建脚本master_ip_failover(故障切换脚本,切换master的IP地址),并授权777

[[email protected] ~]# vim /usr/local/bin/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 = ‘10.10.10.111/24‘;  # Virtual IP  
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig ens160:$key $vip"; # Network name 
my $ssh_stop_vip = "/sbin/ifconfig ens160:$key down";
my $exit_code = 0;

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

        # $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 ([email protected]) 
            warn "Got Error: [email protected]\\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 ([email protected]) 
            warn [email protected];
            exit $exit_code;
        
        exit $exit_code;

elsif ( $command eq "status" ) 
        print "Checking the Status of the script.. OK \\n";
        `ssh $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 $ssh_user\\@$new_master_host \\" $ssh_start_vip \\"`;

# 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_failover –command=start|stop|stopssh|status –orig_master_host=host –orig_master_ip=ip –orig_master_port=po  
rt –new_master_host=host –new_master_ip=ip –new_master_port=port\\n";

[[email protected] ~]# chmod 777 /usr/local/bin/master_ip_failover

  在/etc/masterha下创建配置文件app1.cnf

[[email protected] ~]# vim /etc/masterha/app1.cnf
[server default]
manager_workdir=/var/log/masterha/app1  //manager工作目录
manager_log=/var/log/masterha/app1/manager.log  //manager的日志
master_binlog_dir=/var/lib/mysql  //master服务器保存二进制日志文件的位置
master_ip_failover_script=/usr/local/bin/master_ip_failover  //自动故障切换时master服务器IP地址切换脚本,如果没有则不写此项
master_ip_online_change_script=/usr/local/bin/master_ip_online_change  //手动切换时master服务器IP地址切换脚本,如果没有则不写此项(一般安装MHA时自带)
user=root  //监控用户
password=123456  //监控用户的密码
ping_interval=1  //监控主库时发送ping包的时间间隔,默认为3秒,如果连续3次没有回应将自动进行故障切换
remote_workdir=/tmp  //Mysql在发生切换时二进制日志文件的保存位置
repl_user=slave  //复制用户
repl_password=123456  //复制用户的密码
report_script=/usr/local/send_report  //发生切换后发送的报警脚本
shutdown_script=""  //故障发生后关闭故障主机的脚本,主要作用是关闭主机防止发生脑裂
ssh_user=root  //ssh登录用户

[server1]
hostname=192.168.1.7
port=3306

[server2]
hostname=192.168.1.8
port=3306
candidate_master=1  //候选master,主从切换后升为master,即使复制事件不是最新的
check_repl_delay=0  //默认情况下,如果一个slave的中转日志落后master二进制日志100M,那么MHA就不会选择该slave作为新的master。但设置此项后MHA会忽略复制延迟,对候选master非常有用。

[server3]
hostname=192.168.1.9
port=3306

  检查管理节点到所有Node节点的ssh连接状态

[[email protected] ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf  //检查通过
Fri Jun 21 23:50:21 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Jun 21 23:50:21 2019 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Fri Jun 21 23:50:21 2019 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Fri Jun 21 23:50:21 2019 - [info] Starting SSH connection tests..
Fri Jun 21 23:50:22 2019 - [debug] 
Fri Jun 21 23:50:21 2019 - [debug]  Connecting via SSH from [email protected](192.168.1.7:22) to [email protected](192.168.1.8:22)..
Fri Jun 21 23:50:22 2019 - [debug]   ok.
Fri Jun 21 23:50:22 2019 - [debug]  Connecting via SSH from [email protected](192.168.1.7:22) to [email protected](192.168.1.9:22)..
Fri Jun 21 23:50:22 2019 - [debug]   ok.
Fri Jun 21 23:50:23 2019 - [debug] 
Fri Jun 21 23:50:22 2019 - [debug]  Connecting via SSH from [email protected](192.168.1.8:22) to [email protected](192.168.1.7:22)..
Fri Jun 21 23:50:22 2019 - [debug]   ok.
Fri Jun 21 23:50:22 2019 - [debug]  Connecting via SSH from [email protected](192.168.1.8:22) to [email protected](192.168.1.9:22)..
Fri Jun 21 23:50:23 2019 - [debug]   ok.
Fri Jun 21 23:50:24 2019 - [debug] 
Fri Jun 21 23:50:22 2019 - [debug]  Connecting via SSH from [email protected](192.168.1.9:22) to [email protected](192.168.1.7:22)..
Fri Jun 21 23:50:23 2019 - [debug]   ok.
Fri Jun 21 23:50:23 2019 - [debug]  Connecting via SSH from [email protected](192.168.1.9:22) to [email protected](192.168.1.8:22)..
Fri Jun 21 23:50:23 2019 - [debug]   ok.
Fri Jun 21 23:50:24 2019 - [info] All SSH connection tests passed successfully.

  检查复制环境

[[email protected] ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf

MySQL Replication Health is OK.  //最后提示OK就通过了

  检查管理节点的状态

[[email protected] ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 is stopped(2:NOT_RUNNING).

  开启管理节点监控

/*--remove_dead_master_conf表示发生主从切换后,老的master服务器配置检查配置文件中删除
 *--ignore_last_failover在缺省情况下,如果MHA检测到连续宕机,切间隔不足8小时,则不会进行故障切换,避免ping-pong效应。设置该参数表示忽略上次切换
 *--manger_log日志存放文职*/
[[email protected] ~]# 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 &          
[1] 9562
[[email protected] ~]# masterha_check_status --conf=/etc/masterha/app1.cnf  //查看一下状态
app1 (pid:9562) is running(0:PING_OK), master:192.168.1.7

  关闭管理节点监控

[[email protected] ~]# masterha_stop --conf=/etc/masterha/app1.cnf     
Stopped app1 successfully.
[1]+  退出 1                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
[[email protected] ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 is stopped(2:NOT_RUNNING).

(8).测试

  开启管理节点监控

[[email protected] ~]# 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 &
[1] 11267

  关闭master服务器的mysqld

[[email protected] ~]# systemctl stop mysqld

  回到管理节点查看日志文件,有以下一段

[[email protected] ~]# vim /var/log/masterha/app1/manager.log
----- Failover Report -----

app1: MySQL Master failover 192.168.1.7(192.168.1.7:3306) to 192.168.1.8(192.168.1.8:3306) succeeded

Master 192.168.1.7(192.168.1.7:3306) is down!

Check MHA Manager logs at youxi1:/var/log/masterha/app1/manager.log for details.

Started automated(non-interactive) failover.
Invalidated master IP address on 192.168.1.7(192.168.1.7:3306)
The latest slave 192.168.1.8(192.168.1.8:3306) has all relay logs for recovery.
Selected 192.168.1.8(192.168.1.8:3306) as a new master.
192.168.1.8(192.168.1.8:3306): OK: Applying all logs succeeded.
192.168.1.8(192.168.1.8:3306): OK: Activated master IP address.
192.168.1.9(192.168.1.9:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
192.168.1.9(192.168.1.9:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.1.8(192.168.1.8:3306)

  另外再去看配置文件,会发现[server1]模块被删除了,user=root也没了。

[[email protected] ~]# cat /etc/masterha/app1.cnf 
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
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
password=123456
ping_interval=1
remote_workdir=/tmp
repl_user=slave
repl_password=123456
report_script=/usr/local/send_report
shutdown_script=""
ssh_user=root
user=root

[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.1.8
port=3306

[server3]
hostname=192.168.1.9
port=3306

  还可以前往slave1,备用master服务器上查看master状态

[[email protected] ~]# mysql -uroot -p123456
mysql> show master status\\G
*************************** 1. row ***************************
             File: mysql-bin-slave1.000001
         Position: 154
     Binlog_Do_DB: test_db
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

参考:MySQL高可用架构之MHA

以上是关于MHA集群搭建的主要内容,如果未能解决你的问题,请参考以下文章

Step By Step 搭建 MySql MHA 集群

搭建MHA实现MySQL集群高可用

mysql MHA集群的搭建

8.MHA高可用集群搭建

8.MHA高可用集群搭建

MHA集群搭建