mysql5.7 MHA搭建(复制+binlog开启)

版权声明:zhiyu https://blog.csdn.net/ichglauben/article/details/81944239

MHA介绍

MHA,即MasterHigh Availability Manager and Toolsfor MySQL,是日本的一位MySQL专家采用Perl语言编写的一个脚本管理工具,该工具仅适用于MySQLReplication 环境,目的在于维持Master主库的高可用性。

MHA(Master High Availability)是自动的master故障转移和Slave提升的软件包.它是基于标准的MySQL复制(异步/半同步).

MHA组成部分:

MHA由两部分组成:
- 1.Manager
- 2. Node

MHA部署解读:

MHA Manager可以单独部署在一台独立机器上管理多个master-slave集群,也可以部署在一台slave上.MHA Manager探测集群的node节点,当发现master出现故障的时候,它可以自动将具有最新数据的slave提升为新的master,然后将所有其它的slave导向新的master上.整个故障转移过程对应用程序是透明的。
MHA node运行在每台MySQL服务器上(master/slave/manager),它通过监控具备解析和清理logs功能的脚本来加快故障转移的。

MHA优缺点介绍:

  • 优点
    1. 故障切换时,可以自行判断哪个从库与主库的数据最接近,就切换到上面,可以减少数据的丢失,保证数据的一致性
    2. 支持 binlog server,可提高 binlog 传送效率,进一步减少数据丢失风险。
    3. 可以配置 mysql 5.7 的增强半同步,来保证数据的时时同步
  • 缺点
    1. 自动切换的脚本太简单了,而且比较老化,建议后期逐渐完善。
    2. 搭建 MHA 架构,需要开启 linux 系统互信协议,所以对于系统安全性来说,是个不小的考验。

原理介绍

MHA的目的在于维持MySQL Replication中Master库的高可用性,其最大特点是可以修复多个Slave之间的差异日志,最终使所有Slave保持数据一致,然后从中选择一个充当新的Master,并将其它Slave指向它。
当master出现故障时,可以通过对比slave之间I/O thread 读取主库binlog的position号,选取最接近的slave做为备选主库(备胎)。其它的从库可以通过与备选主库对比生成差异的中继日志。在备选主库上应用从原来master保存的binlog,同时将备选主库提升为master。最后在其它slave上应用相应的差异中继日志并开始从新的master开始复制。

MHA工具包功能介绍:

工具下载地址(node+manager057版本)
- 1.Manager工具:

#masterha_check_ssh : 检查MHA的SSH配置。
#masterha_check_repl : 检查MySQL复制。
#masterha_manager : 启动MHA。
#masterha_check_status : 检测当前MHA运行状态。
#masterha_master_monitor : 监测master是否宕机。
#masterha_master_switch : 控制故障转移(自动或手动)。
#masterha_conf_host : 添加或删除配置的server信息。
  • 2.Node工具:
#save_binary_logs : 保存和复制master的二进制日志。
#apply_diff_relay_logs : 识别差异的中继日志事件并应用于其它slave。
#filter_mysqlbinlog : 去除不必要的ROLLBACK事件(MHA已不再使用这个工具)。
#purge_relay_logs : 清除中继日志(不会阻塞SQL线程)。

演示图:

这里写图片描述

  • 环境介绍
192.168.1.238 master node
192.168.1.241 slave1 node
192.168.1.240 slave2 node(manager , node)
192.168.1.117 vip node

first step:生成SSH无密钥证书

1)主从库生成密钥

cd /root/.ssh
ssh-keygen -t dsa -P '' -f id_dsa 
cat id_dsa.pub >> authorized_keys

管理节点生成密钥

ssh-keygen -t dsa -P '' -f id_dsa 
cat id_dsa.pub >> authorized_keys

主库接收密钥过程:

scp 192.168.1.241:/root/.ssh/id_dsa.pub ./id_dsa.pub.241
scp 192.168.1.240:/root/.ssh/id_dsa.pub ./id_dsa.pub.240

合并密钥:

扫描二维码关注公众号,回复: 3122210 查看本文章
cat id_dsa.pub.241 >> authorized_keys
cat id_dsa.pub.240 >> authorized_keys

在主库上传送合成密钥:

scp authorized_keys 192.168.1.241:/root/.ssh/
scp authorized_keys 192.168.1.240:/root/.ssh/

在三台服务器上,编辑/etc/hosts文件,分别加入三台主机hostname

vim /etc/hosts

192.168.1.238  node1
192.168.1.241  node2
192.168.1.240  node3

验证主机名登陆,密钥验证:
分别在三台机器上执行:

在192.168.1.238执行

ssh node2
ssh node3

在192.168.1.241执行

ssh node1
ssh node3

在192.168.1.240执行

ssh node1
ssh node2

搭建主从环境(一主两从架构,我这里是mysql 5.7版本)

  • 在所有节点上都要执行
创建主从账号:

grant replication slave,REPLICATION CLIENT on *.* to 'repl'@'192.168.1.%' identified by 'repl';

flush privileges;

创建管理账号:

grant all privileges on *.* to 'zs'@'192.168.1.%' identified by 'rootroot';

flush privileges;

开启binlog 搭建复制环境(所有的节点)

  • 开启binlog文件
log_bin=/mysql/log/3306/binlog/itpuxdb-binlog
log_bin_index=/mysql/log/3306/binlog/itpuxdb-binlog.index
binlog_format='row'
binlog_rows_query_log_events=on

mkdir -p /mysql/log/3306/binlog/
chown -R mysql:mysql /mysql/log/3306/binlog/

service mysql restart

复制环境

  • 配置主备库:
    配置master的my.cnf

  • 主库

innodb_flush_log_at_trx_commit=1
sync_binlog=1
  • 备库配置:
log_slave_updates=1
read_only=1

即使开启了以上选项 备库仍然有可能在崩溃后被中断 因为master.info和中继日志文件都不是崩溃安全的 最好设置以下选项:
sync_master_info = 1
sync_relay_log = 1
sync_relay_log_info = 1

relay_log:指定中继日志的位置和命名
log_slave_updates:允许备库将其重放的事件也记录到自身的二进制日志中
read_only:可以阻止大部分用户更改非临时表 除了复制SQL线程和其他拥有超级权限的用户外

-主库查看(记录情况)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |     2005 | orders       | mysql            |                   |
+------------------+----------+--------------+------------------+-------------------+
  • 启动备库 执行:
mysql> change master to master_host='192.168.1.238',
                master_user='repl',
                master_password='repl',
                master_port=3306,
                master_log_file='itpuxdb-binlog.000002',#注意配置好名称会出错
                master_log_pos=154,
                master_connect_retry=10;

启动进程

      mysql> start slave;
      ---------------检查主从复制状态------------------
      mysql> show slave status\G
  • 注意报错 UUID不同 (每个节点 克隆导致 需要修改)
vi /mysql/data/3306/data/auto.cnf

mysql> show slave status\G(检查 IO线程 SQL线程running )

安装Node节点(57)

  • 在Master(238)上面安装数据节点:
    首先要先安装mysql依赖的perl环境
yum install perl-DBD-MySQL
  • 解压数据节点的包
tar -zxvf mha4mysql-node-0.57.tar.gz
  • 安装perl-cpan软件包
cd mha4mysql-node-0.57
yum -y install perl-CPAN*
perl Makefile.PL
make && make install

在从库(239)上面安装数据节点:

同主库一样的安装操作;

在manager管理节点(240)上面安装管理节点:
首先先要安装环境需要的介质包 建议配置EPEL源

yum install -y perl-DBD-MySQL
rpm -ivh perl-Params-Validate-0.92-3.el6.x86_64.rpm
rpm -ivh perl-Config-Tiny-2.12-1.el6.rfx.noarch.rpm
rpm -ivh perl-Log-Dispatch-2.26-1.el6.rf.noarch.rpm
rpm -ivh perl-Parallel-ForkManager-0.7.5-2.2.el6.rf.noarch.rpm
  • 再安装数据节点:
tar -zxvf mha4mysql-node-0.57.tar.gz 
cd mha4mysql-node-0.57

yum -y install perl-CPAN*

perl Makefile.PL
make && make install

最后安装管理节点:

tar -zxvf mha4mysql-manager-0.57.tar.gz

perl Makefile.PL
make &&  make install
  • 下面来进行管理节点MHA配置:
    创建mha家目录,编辑启动配置文件(自己配置binlog目录 要加master_binlog_dir=/mysql/log/3306/binlog/)
mkdir -p /usr/local/mha
mkdir -p /etc/mhacd /etc/mha/
touch /usr/local/mha/manager.log
vim /etc/mha/mha.conf
[server default]
user=zs
password=rootroot
manager_workdir=/usr/local/mha
manager_log=/usr/local/mha/manager.log
remote_workdir=/usr/local/mha
ssh_user=root
repl_user=repl
repl_password=repl
ping_interval=1
master_ip_failover_script=/usr/local/scripts/master_ip_failover
master_ip_online_change_script=/usr/local/scripts/master_ip_online_change
[server1]
hostname=192.168.1.238
ssh_port=22
candidate_master=1
port=3306

[server2]
hostname=192.168.1.241
ssh_port=22
candidate_master=1
port=3306


[server3]
hostname=192.168.1.240
ssh_port=22
no_master=1
port=3306

防止报错(所有节点执行)

which mysql
which mysqlbinlog
--------------------------------
ln -s /mysql/app/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog
ln -s /mysql/app/mysql/bin/mysql /usr/bin/mysql

测试:检查SSH连接

masterha_check_ssh --conf=/etc/mha/mha.conf
  • 结果必须是:
    All SSH connection tests passed successfully.

创建failover,online 脚本的目录:

mkdir -p /usr/local/scripts
cd /usr/local/scripts
vim 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 = '192.168.1.117/24';  
my $key = '0';
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"; }

 SELECT user,host,password, Repl_slave_priv AS Value FROM mysql.user WHERE user = 'repl';
  • 编辑online_change的脚本:
vim master_ip_online_change #!/usr/bin/env perl 
use strict; 
use warnings FATAL =>'all'; 
use Getopt::Long; 
my $vip = '192.168.1.117/24'; # Virtual IP 
my $key = "0"; 
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=po
rt –new_master_host=host –new_master_ip=ip –new_master_port=port\n";  }

创建完两个脚本,记得赋予执行权限

masterha_check_repl

masterha_check_repl --conf=/etc/mha/mha.conf

检查结果必须是

MySQL Replication Health is OK.


测试报错–

[root@mysqldb mha]# masterha_check_repl --conf=/etc/mha/mha.conf 
Wed Aug 22 14:52:05 2018 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='zs' --slave_host=192.168.1.241 --slave_ip=192.168.1.241 --slave_port=3306 --workdir=/usr/local/mha --target_version=5.7.20-log --manager_version=0.57 --relay_log_info=/mysql/data/3306/data/relay-log.info  --relay_dir=/mysql/data/3306/data/  --slave_pass=xxx
Wed Aug 22 14:52:05 2018 - [info]   Connecting to [email protected](192.168.1.241:22).. 
Can't exec "mysqlbinlog": No such file or directory at /usr/local/share/perl5/MHA/BinlogManager.pm line 106.
mysqlbinlog version command failed with rc 1:0, please verify PATH, LD_LIBRARY_PATH, and client options
 at /usr/local/bin/apply_diff_relay_logs line 493
Wed Aug 22 14:52:05 2018 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln208] Slaves settings check failed!
Wed Aug 22 14:52:05 2018 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln416] Slave configuration failed.
Wed Aug 22 14:52:05 2018 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln427] Error happened on checking configurations.  at /usr/local/bin/masterha_check_repl line 48
Wed Aug 22 14:52:05 2018 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln525] Error happened on monitoring servers.
Wed Aug 22 14:52:05 2018 - [info] Got exit code 1 (Not master dead).

解决
所有节点执行

[root@mysqldb mha4mysql-node-0.57]# which mysqlbinlog
/mysql/app/mysql/bin/mysqlbinlog
[root@mysqldb mha4mysql-node-0.57]# ln -s /mysql/app/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog

报错二:

 Testing mysql connection and privileges..sh: mysql: command not found
mysql command failed with rc 127:0!
 at /usr/local/bin/apply_diff_relay_logs line 375
        main::check() called at /usr/local/bin/apply_diff_relay_logs line 497
        eval {...} called at /usr/local/bin/apply_diff_relay_logs line 475
        main::main() called at /usr/local/bin/apply_diff_relay_logs line 120
Wed Aug 22 15:02:31 2018 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln208] Slaves settings check failed!
Wed Aug 22 15:02:31 2018 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln416] Slave configuration failed.
Wed Aug 22 15:02:31 2018 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln427] Error happened on checking configurations.  at /usr/local/bin/masterha_check_repl line 48
Wed Aug 22 15:02:31 2018 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln525] Error happened on monitoring servers.
Wed Aug 22 15:02:31 2018 - [info] Got exit code 1 (Not master dead).

MySQL Replication Health is NOT OK!

解决BUG

**所有节点都执行**************
[root@mysqldb mha]# which mysql
/mysql/app/mysql/bin/mysql
ln -s /mysql/app/mysql/bin/mysql /usr/bin/mysql

大功告成~

[root@mysqldb mha]# masterha_check_repl --conf=/etc/mha/mha.conf 
Wed Aug 22 15:05:10 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Aug 22 15:05:10 2018 - [info] Reading application default configuration from /etc/mha/mha.conf..
Wed Aug 22 15:05:10 2018 - [info] Reading server configuration from /etc/mha/mha.conf..
Wed Aug 22 15:05:10 2018 - [info] MHA::MasterMonitor version 0.57.
Wed Aug 22 15:05:11 2018 - [info] GTID failover mode = 0
Wed Aug 22 15:05:11 2018 - [info] Dead Servers:
Wed Aug 22 15:05:11 2018 - [info] Alive Servers:
Wed Aug 22 15:05:11 2018 - [info]   192.168.1.238(192.168.1.238:3306)
Wed Aug 22 15:05:11 2018 - [info]   192.168.1.241(192.168.1.241:3306)
Wed Aug 22 15:05:11 2018 - [info]   192.168.1.240(192.168.1.240:3306)
Wed Aug 22 15:05:11 2018 - [info] Alive Slaves:
Wed Aug 22 15:05:11 2018 - [info]   192.168.1.241(192.168.1.241:3306)  Version=5.7.20-log (oldest major version between slaves) log-bin:enabled
Wed Aug 22 15:05:11 2018 - [info]     Replicating from 192.168.1.238(192.168.1.238:3306)
Wed Aug 22 15:05:11 2018 - [info]     Primary candidate for the new Master (candidate_master is set)
Wed Aug 22 15:05:11 2018 - [info]   192.168.1.240(192.168.1.240:3306)  Version=5.7.20-log (oldest major version between slaves) log-bin:enabled
Wed Aug 22 15:05:11 2018 - [info]     Replicating from 192.168.1.238(192.168.1.238:3306)
Wed Aug 22 15:05:11 2018 - [info]     Not candidate for the new Master (no_master is set)
Wed Aug 22 15:05:11 2018 - [info] Current Alive Master: 192.168.1.238(192.168.1.238:3306)
Wed Aug 22 15:05:11 2018 - [info] Checking slave configurations..
Wed Aug 22 15:05:11 2018 - [warning]  relay_log_purge=0 is not set on slave 192.168.1.241(192.168.1.241:3306).
Wed Aug 22 15:05:11 2018 - [warning]  relay_log_purge=0 is not set on slave 192.168.1.240(192.168.1.240:3306).
Wed Aug 22 15:05:11 2018 - [info] Checking replication filtering settings..
Wed Aug 22 15:05:11 2018 - [info]  binlog_do_db= , binlog_ignore_db= 
Wed Aug 22 15:05:11 2018 - [info]  Replication filtering check ok.
Wed Aug 22 15:05:11 2018 - [info] GTID (with auto-pos) is not supported
Wed Aug 22 15:05:11 2018 - [info] Starting SSH connection tests..
Wed Aug 22 15:05:12 2018 - [info] All SSH connection tests passed successfully.
Wed Aug 22 15:05:12 2018 - [info] Checking MHA Node version..
Wed Aug 22 15:05:13 2018 - [info]  Version check ok.
Wed Aug 22 15:05:13 2018 - [info] Checking SSH publickey authentication settings on the current master..
Wed Aug 22 15:05:13 2018 - [info] HealthCheck: SSH to 192.168.1.238 is reachable.
Wed Aug 22 15:05:13 2018 - [info] Master MHA Node version is 0.57.
Wed Aug 22 15:05:13 2018 - [info] Checking recovery script configurations on 192.168.1.238(192.168.1.238:3306)..
Wed Aug 22 15:05:13 2018 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/mysql/log/3306/binlog/ --output_file=/usr/local/mha/save_binary_logs_test --manager_version=0.57 --start_file=itpuxdb-binlog.000003 
Wed Aug 22 15:05:13 2018 - [info]   Connecting to root@192.168.1.238(192.168.1.238:22).. 
  Creating /usr/local/mha if not exists..    ok.
  Checking output directory is accessible or not..
   ok.
  Binlog found at /mysql/log/3306/binlog/, up to itpuxdb-binlog.000003
Wed Aug 22 15:05:13 2018 - [info] Binlog setting check done.
Wed Aug 22 15:05:13 2018 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Wed Aug 22 15:05:13 2018 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='zs' --slave_host=192.168.1.241 --slave_ip=192.168.1.241 --slave_port=3306 --workdir=/usr/local/mha --target_version=5.7.20-log --manager_version=0.57 --relay_log_info=/mysql/data/3306/data/relay-log.info  --relay_dir=/mysql/data/3306/data/  --slave_pass=xxx
Wed Aug 22 15:05:13 2018 - [info]   Connecting to root@192.168.1.241(192.168.1.241:22).. 
  Checking slave recovery environment settings..
    Opening /mysql/data/3306/data/relay-log.info ... ok.
    Relay log found at /mysql/log/3306, up to relay_log.000006
    Temporary relay log file is /mysql/log/3306/relay_log.000006
    Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Wed Aug 22 15:05:13 2018 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='zs' --slave_host=192.168.1.240 --slave_ip=192.168.1.240 --slave_port=3306 --workdir=/usr/local/mha --target_version=5.7.20-log --manager_version=0.57 --relay_log_info=/mysql/data/3306/data/relay-log.info  --relay_dir=/mysql/data/3306/data/  --slave_pass=xxx
Wed Aug 22 15:05:13 2018 - [info]   Connecting to root@192.168.1.240(192.168.1.240:22).. 
  Checking slave recovery environment settings..
    Opening /mysql/data/3306/data/relay-log.info ... ok.
    Relay log found at /mysql/log/3306, up to relay_log.000006
    Temporary relay log file is /mysql/log/3306/relay_log.000006
    Testing mysql connection and privileges..mysql: [Warning] Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Wed Aug 22 15:05:13 2018 - [info] Slaves settings check done.
Wed Aug 22 15:05:13 2018 - [info] 
192.168.1.238(192.168.1.238:3306) (current master)
 +--192.168.1.241(192.168.1.241:3306)
 +--192.168.1.240(192.168.1.240:3306)

Wed Aug 22 15:05:13 2018 - [info] Checking replication health on 192.168.1.241..
Wed Aug 22 15:05:13 2018 - [info]  ok.
Wed Aug 22 15:05:13 2018 - [info] Checking replication health on 192.168.1.240..
Wed Aug 22 15:05:13 2018 - [info]  ok.
Wed Aug 22 15:05:13 2018 - [info] Checking master_ip_failover_script status:
Wed Aug 22 15:05:13 2018 - [info]   /usr/local/scripts/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.1.238 --orig_master_ip=192.168.1.238 --orig_master_port=3306 


IN SCRIPT TEST====/sbin/ifconfig eth0:0 down==/sbin/ifconfig eth0:0 192.168.1.117/24===

Checking the Status of the script.. OK 
Wed Aug 22 15:05:14 2018 - [info]  OK.
Wed Aug 22 15:05:14 2018 - [warning] shutdown_script is not defined.
Wed Aug 22 15:05:14 2018 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

管理节点执行 mha

[root@mysqldb mha]# nohup masterha_manager --conf=/etc/mha/mha.conf > /tmp/mha_manager.log < /dev/null 2>&1 &

验证启动成功的命令:查看显示状态

[root@mysqldb mha]# masterha_check_status --conf=/etc/mha/mha.conf 
mha is stopped(2:NOT_RUNNING).
[1]+  Exit 1                  nohup masterha_manager --conf=/etc/mha/mha.conf > /tmp/mha_manager.log < /dev/null 2>&1
[root@mysqldb mha]# 
[root@mysqldb mha]# nohup masterha_manager --conf=/etc/mha/mha.conf > /tmp/mha_manager.log < /dev/null 2>&1 &
[1] 14016
[root@mysqldb mha]# masterha_check_status --conf=/etc/mha/mha.conf 
mha (pid:14016) is running(0:PING_OK), master:192.168.1.238

测试主库宕机(238)

[root@mysqldb mha4mysql-node-0.57]# service mysql stop
Shutting down MySQL...........                             [确定]

切换后,MHA进程会自动停止运行
在管理节点查看:

[root@mysqldb mha]# masterha_check_status --conf=/etc/mha/mha.conf 
mha is stopped(2:NOT_RUNNING).
[1]+  Done                    nohup masterha_manager --conf=/etc/mha/mha.conf > /tmp/mha_manager.log < /dev/null 2>&1

恢复操作:把宕掉的主库
把原来的数据库开启

[root@mysqldb mha4mysql-node-0.57]# service mysql start
Starting MySQL.....                                        [确定]

检查节点240查看mysql状态

root@itpux 16:44:  [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.241
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: itpuxdb-binlog.000003
          Read_Master_Log_Pos: 1499
               Relay_Log_File: relay_log.000002
                Relay_Log_Pos: 325
        Relay_Master_Log_File: itpuxdb-binlog.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

参考链接
参考链接2

猜你喜欢

转载自blog.csdn.net/ichglauben/article/details/81944239