## # MHA高可用(一个主库,两个从库)

# MHA高可用(一个主库,两个从库)

1、配置yum源(使用一键输入所有对话)
[root@m01 ~]# vim /etc/yum.repos.d/mysql.repo
# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/6/$basearch/
enabled=1
gpgcheck=0
注意:三个库的/etc/my.cnf 里都要开启log_bin=mysql-bin

1.1.在此设置软连接 MHA的manager管理节点在管理用户过程中,需要调用这两个命令,但MHA默认的路径在/usr/local/bin下,所以做了软连接。其实不做也可以(三个都可以做)

[root@m01 mha4mysql-manager-0.56]# ln -s /usr/bin/mysql /usr/local/bin

[root@m01 mha4mysql-manager-0.56]# ln -s /usr/bin/mysqlbinlog  /usr/local/bin

2、在主库上授予两个账户权限
mysql> grant replication slave on *.* to slave@'10.0.0.%' identified by 'slave';
Query OK, 0 rows affected (0.00 sec)


mysql> grant all privileges on *.* to mha@'10.0.0.%' identified by 'mha';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3.免密登录设置
root@m01 bin]# ssh-keygen -t rsa
root@m01 bin]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.4
[root@m01 bin]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.5

[root@lb01 ~]# ssh-keygen -t rsa
[root@lb01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.5
[root@lb01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.61

[root@lb02 etc]# ssh-keygen -t rsa
[root@lb02 etc]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.4
[root@lb02 etc]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.61
经过我权威专家测试,互相都通!

4.分别在两个从库上设置这两个参数

mysql> set global read_only=1;
Query OK, 0 rows affected (0.00 sec)

mysql> set global relay_log_purge=0;
Query OK, 0 rows affected (0.00 sec)

5、分别在三个库上建立一个文件夹

[root@m01 ~]# mkdir /usr/local/packages
[root@lb01 ~]# mkdir /usr/local/packages
[root@lb02 ~]# mkdir /usr/local/packages

6.所有节点安装MHA node所需的perl模块(DBD:mysql)

yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes perl-devel

7.解压、

[root@m01 packages]# tar xf mha4mysql-manager-0.56.tar.gz 
[root@m01 packages]# ls
mha4mysql-manager-0.56  mha4mysql-manager-0.56.tar.gz  mha4mysql-node-0.56.tar.gz
[root@m01 packages]# rm -rf mha4mysql-manager-0.56.tar.gz 
[root@m01 packages]# ls
mha4mysql-manager-0.56  mha4mysql-node-0.56.tar.gz
[root@m01 packages]# cd mha4mysql-manager-0.56/
[root@m01 mha4mysql-manager-0.56]# ls
AUTHORS  bin  COPYING  debian  inc  lib  Makefile.PL  MANIFEST  META.yml  README  rpm  samples  t  tests

8.编译环境准备(用了perl 类似cmake)(特别强调,不管是 哪台机子,先装node,后装managers)

[root@m01 mha4mysql-manager-0.56]# perl Makefile.PL
*** Module::AutoInstall version 1.03
*** Checking for Perl dependencies...
Can't locate CPAN.pm in @INC (@INC contains: inc /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at inc/Module/AutoInstall.pm line 277.

9、解决错误

[root@m01 mha4mysql-manager-0.56]# yum -y install perl-CPAN

10.编译准备

[root@m01 mha4mysql-manager-0.56]# perl Makefile.PL
*** Module::AutoInstall version 1.03
*** Checking for Perl dependencies...
[Core Features]
- DBI                   ...loaded. (1.627)
- DBD::mysql            ...loaded. (4.023)
- Time::HiRes           ...loaded. (1.9725)
- Config::Tiny          ...loaded. (2.14)
- Log::Dispatch         ...loaded. (2.41)
- Parallel::ForkManager ...loaded. (1.18)
- MHA::NodeConst        ...missing.
==> Auto-install the 1 mandatory module(s) from CPAN? [y] y
*** Dependencies will be installed the next time you type 'make'.
*** Module::AutoInstall configuration finished.
Checking if your kit is complete...
Looks good
Warning: prerequisite MHA::NodeConst 0 not found.
Writing Makefile for mha4mysql::manager

11.编译和安装

[root@m01 mha4mysql-manager-0.56]# make && make install
[root@m01 mha4mysql-manager-0.56]# ls
AUTHORS  blib     debian  lib       Makefile.PL  META.yml    README  samples  tests
bin      COPYING  inc     Makefile  MANIFEST     pm_to_blib  rpm     t

12.复制相关脚本到本地命令/usr/local/bin下(manager默认管理都在/usr/local/bin)

[root@m01 mha4mysql-manager-0.56]#  cp -ra samples/scripts/ /usr/local/bin/


[root@m01 mha4mysql-manager-0.56]# ll /usr/local/bin/
总用量 40
-r-xr-xr-x 1 root root  1995 3月   8 16:31 masterha_check_repl
-r-xr-xr-x 1 root root  1779 3月   8 16:31 masterha_check_ssh
-r-xr-xr-x 1 root root  1865 3月   8 16:31 masterha_check_status
-r-xr-xr-x 1 root root  3201 3月   8 16:31 masterha_conf_host
-r-xr-xr-x 1 root root  2517 3月   8 16:31 masterha_manager
-r-xr-xr-x 1 root root  2165 3月   8 16:31 masterha_master_monitor
-r-xr-xr-x 1 root root  2373 3月   8 16:31 masterha_master_switch
-r-xr-xr-x 1 root root  5171 3月   8 16:31 masterha_secondary_check
-r-xr-xr-x 1 root root  1739 3月   8 16:31 masterha_stop

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

13.创建mha管理文件夹并且将/usr/local/packages/mha4mysql-manager-0.56/samples/conf/app1.cnf 复制到mha总配置文件下面

root@m01 mha4mysql-manager-0.56]# mkdir /etc/masterha/
[root@m01 mha4mysql-manager-0.56]# cp samples/conf/app1.cnf /etc/masterha/

14.、master_ip_failover #自动切换时 VIP 管理的脚本

注意:master_ip_failover 是VIP自动漂移的立面写的

同master_ip_online_change相比,后者只是实现VIP自动漂移的

[root@m01 mha4mysql-manager-0.56]# cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin/

15.设置ip漂移的命令的参数脚本

下面一大串脚本,其实主要就是修改这个:VIP地址和网卡eth0
my $vip = '10.0.0.200/24';  # Virtual IP 
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";




[root@m01 mha4mysql-manager-0.56]# 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 = '192.168.100.200/24';  # Virtual IP 
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" ) {

        # $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 "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" ) {

        # 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 "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"; 
        `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=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}

14.创建/var/log/masterha/app1目录 因为这两个目录并没有(实质创建MHA软件目录并拷贝配置文件)


15.设置MHA管理manager管理节点的总配置文件

[root@m01 mha4mysql-manager-0.56]# vim /etc/masterha/app1.cnf
[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/manager.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
password=mha
user=mha
ping_interval=1
remote_workdir=/tmp
repl_password=slave
repl_user=slave
secondary_check_script= /usr/local/bin/masterha_secondary_check -s 10.0.0.4 -s 10.0.0.5
shutdown_script=""
ssh_user=root
[server1]
hostname=10.0.0.61
candidate_master=1
port=3306
[server2]
hostname=10.0.0.4
port=3306
candidate_master=1
check_repl_delay=0
[server3]
hostname=10.0.0.5
candidate_master=1
port=3306

16.检查ssh连接情况

[root@m01 bin]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
Sun Mar  7 02:23:35 2021 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sun Mar  7 02:23:35 2021 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Sun Mar  7 02:23:35 2021 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Sun Mar  7 02:23:35 2021 - [info] Starting SSH connection tests..
Sun Mar  7 02:23:36 2021 - [debug] 
Sun Mar  7 02:23:35 2021 - [debug]  Connecting via SSH from root@10.0.0.61(10.0.0.61:22) to root@10.0.0.4(10.0.0.4:22)..
Sun Mar  7 02:23:36 2021 - [debug]   ok.
Sun Mar  7 02:23:36 2021 - [debug]  Connecting via SSH from root@10.0.0.61(10.0.0.61:22) to root@10.0.0.5(10.0.0.5:22)..
Sun Mar  7 02:23:36 2021 - [debug]   ok.
Sun Mar  7 02:23:37 2021 - [debug] 
Sun Mar  7 02:23:36 2021 - [debug]  Connecting via SSH from root@10.0.0.4(10.0.0.4:22) to root@10.0.0.61(10.0.0.61:22)..
Sun Mar  7 02:23:36 2021 - [debug]   ok.
Sun Mar  7 02:23:36 2021 - [debug]  Connecting via SSH from root@10.0.0.4(10.0.0.4:22) to root@10.0.0.5(10.0.0.5:22)..
Sun Mar  7 02:23:36 2021 - [debug]   ok.
Sun Mar  7 02:23:38 2021 - [debug] 
Sun Mar  7 02:23:36 2021 - [debug]  Connecting via SSH from root@10.0.0.5(10.0.0.5:22) to root@10.0.0.61(10.0.0.61:22)..
Sun Mar  7 02:23:37 2021 - [debug]   ok.
Sun Mar  7 02:23:37 2021 - [debug]  Connecting via SSH from root@10.0.0.5(10.0.0.5:22) to root@10.0.0.4(10.0.0.4:22)..
Sun Mar  7 02:23:37 2021 - [debug]   ok.
Sun Mar  7 02:23:38 2021 - [info] All SSH connection tests passed successfully.

17.测试 MySQL 主从连接情况,最后出现 MySQL Replication Health is OK 字样说明

[root@m01 bin]# masterha_check_repl -conf=/etc/masterha/app1.cnf
Sun Mar  7 02:45:23 2021 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sun Mar  7 02:45:23 2021 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Sun Mar  7 02:45:23 2021 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Sun Mar  7 02:45:23 2021 - [info] MHA::MasterMonitor version 0.56.
Sun Mar  7 02:45:23 2021 - [error][/root/perl5/lib/perl5/MHA/ServerManager.pm, ln301] Got MySQL error when connecting 10.0.0.61(10.0.0.61:3306) :1045:Access denied for user 'mha'@'m01' (using password: YES), but this is not a MySQL crash. Check MySQL server settings.
 at /root/perl5/lib/perl5/MHA/ServerManager.pm line 297.
Sun Mar  7 02:45:24 2021 - [error][/root/perl5/lib/perl5/MHA/ServerManager.pm, ln309] Got fatal error, stopping operations
Sun Mar  7 02:45:24 2021 - [error][/root/perl5/lib/perl5/MHA/MasterMonitor.pm, ln424] Error happened on checking configurations.  at /root/perl5/lib/perl5/MHA/MasterMonitor.pm line 326.
Sun Mar  7 02:45:24 2021 - [error][/root/perl5/lib/perl5/MHA/MasterMonitor.pm, ln523] Error happened on monitoring servers.
Sun Mar  7 02:45:24 2021 - [info] Got exit code 1 (Not master dead).

MySQL Replication Health is NOT OK!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值