mysql mha

2022-11-23  本文已影响0人  会飞的小猪

公司里有个项目用到了msql mha,某一天突然发现数据库报错了,vip切换不了了。特研究了下mysql mha的部署,发现坑还真不少。
如下部署基于linux centos7,mysql5.7 mha 0.57版本。原文请参考https://juejin.cn/post/7108390910676697118,本文与原文有不一样的地方
需要四台虚拟机:

192.168.2.133  manager
192.168.2.134  master
192.168.2.135  slave1
192.168.2.136  slave2
vip:192.168.2.100

1、机器配置

关闭所有机器防火墙

systemctl disable --now firewalld
setenforce 0

修改数据库机器的hostname

 Master 节点
 hostnamectl set-hostname mysql1
 su
 
 Slave1节点
 hostnamectl set-hostname mysql2
 su
 
 Slave2节点
 hostnamectl set-hostname mysql3
 su

分别设置mysql机器的 host,以及hostname

192.168.2.134 mysql1
192.168.2.135 mysql2
192.168.2.136 mysql3

设置软连接

ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/

设置ssh无密码访问

ssh-keygen -t rsa               #一路按回车键,生成密钥。"-t rsa"指定密钥的类型。
ssh-copy-id 192.168.2.134    #将公钥传给所有数据库节点,形成免密码连接登录
ssh-copy-id 192.168.2.135
ssh-copy-id 192.168.2.136

其他机器参考如上所示。

2、数据库配置

修改数据库配置文件,需要注意三台机器的server-uuid不能一样,克隆过来的机器需要修改下。

 Master 节点
 vim /etc/my.cnf
 [mysqld]
 server-id = 1
 log_bin = master-bin         #开启二进制日志,指定存放位置
 log-slave-updates = true     #允许slave从master复制数据时可以写入到自己的二进制日志中
 
 systemctl restart mysqld     #重启mysql
 
 Slave1节点
 vim /etc/my.cnf
 [mysqld]
 server-id = 2               #三台服务器的 server-id 不能相同
 log_bin = master-bin
 relay-log = relay-log-bin
 relay-log-index = slave-relay-bin.index
 
 systemctl restart mysqld
 
 Slave2节点
 vim /etc/my.cnf
 [mysqld]
 server-id = 3               #三台服务器的 server-id 不能相同
 log_bin = master-bin
 relay-log = relay-log-bin
 relay-log-index = slave-relay-bin.index
 
 systemctl restart mysqld     #重启mysql

设置同步账号

所有数据库节点进行mysql主从同步的授权
grant replication slave on *.* to 'myslave'@'192.168.2.%' identified by '123123';
grant all privileges on *.* to 'mha'@'192.168.2.%' identified by 'manager';
grant all privileges on *.* to 'mha'@'mysql1' identified by 'manager';
grant all privileges on *.* to 'mha'@'mysql2' identified by 'manager';
grant all privileges on *.* to 'mha'@'mysql3' identified by 'manager';
flush privileges;

对slave1,slave2启动同步

change master to master_host='192.168.2.134',master_user='myslave',master_password='123123',master_auto_position=1;
start slave;   #开启同步,如有报错执行 reset slave;

设置从库状态

set global read_only=1;

在Master主库插入数据,测试数据库同步

create database test2; 
use test2;
create table shop(id int);
insert into shop values (1);

从数据库中验证是否同步成功

select * from test2.shop;

3、安装mha

所有服务器上都安装 MHA 依赖的环境,首先安装 epel 源

yum install epel-release --nogpgcheck -y

安装 MHA依赖环境

yum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN

安装 MHA 软件包,必须先在所有服务器上安装 node 组件
将安装包上传到/opt/目录中,解压安装node组件

cd /opt/
tar zxvf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make && make install

最后在 MHA manager 节点上安装 manager 组件(manager组件依赖node 组件)

cd /opt/
tar zxvf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make && make install

在 manager 节点上复制相关脚本到/usr/local/bin 目录

cp -rp /opt/mha4mysql-manager-0.57/samples/scripts /usr/local/bin

拷贝后会有四个执行文件

ll /usr/local/bin/scripts/

------------ 虚线内是注释 -------------------------------------------------------
master_ip_failover #自动切换时 VIP 的管理脚本
master_ip_online_change #在线切换时 VIP 的管理脚本
power_manager #故障发生后关闭主机的脚本
send_report #因故障切换后发送报警的脚本


复制上述的自动切换时 VIP 的管理脚本到 /usr/local/bin 目录,这里使用master_ip_failover脚本来管理 VIP 和故障切换

cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin

修改内容如下:(删除原有内容,直接复制并修改vip相关参数。可在拷贝前输入 :set paste 解决vim粘贴乱序问题)
vim /usr/local/bin/master_ip_failover

#!/usr/bin/env perl

#  Copyright (C) 2011 DeNA Co.,Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#  Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

## Note: This is a sample script and is not complete. Modify the script based on your environment.

use strict;
use warnings FATAL => 'all';

use Getopt::Long;
use MHA::DBHelper;

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

#############################添加内容部分#########################################
my $vip = '192.168.2.100';                             #指定vip的地址
my $brdc = '192.168.2.255';                            #指定vip的广播地址
my $ifdev = 'ens33';                                    #指定vip绑定的网卡
my $key = '1';                                          #指定vip绑定的虚拟网卡序列号
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";   
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";    
my $exit_code = 0;                                      #指定退出状态码为0
#my $ssh_start_vip = "/usr/sbin/ip addr add $vip/24 brd $brdc dev $ifdev label $ifdev:$key;/usr/sbin/arping -q -A -c 1 -I $ifdev $vip;iptables -F;";
#my $ssh_stop_vip = "/usr/sbin/ip addr del $vip/24 dev $ifdev label $ifdev:$key";
##################################################################################
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,
  'new_master_user=s'     => \$new_master_user,
  'new_master_password=s' => \$new_master_password,
);

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();
      # updating global catalog, etc
      $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();
      my $new_master_handler = new MHA::DBHelper();

      # args: hostname, port, user, password, raise_error_or_not
      $new_master_handler->connect( $new_master_ip, $new_master_port,
        $new_master_user, $new_master_password, 1 );

      ## Set read_only=0 on the new master
      $new_master_handler->disable_log_bin_local();
      print "Set read_only=0 on the new master.\n";
      $new_master_handler->disable_read_only();

      ## Creating an app user on the new master
      print "Creating app user on the new master..\n";
      #FIXME_xxx_create_user( $new_master_handler->{dbh} );
      $new_master_handler->enable_log_bin_local();
      $new_master_handler->disconnect();

      ## Update master ip on the catalog database, etc
      #FIXME_xxx;

      $exit_code = 0;
    };
    if ($@) {
      warn $@;

      # If you want to continue failover, exit 10.
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "status" ) {
    print "Checking the Status of the script.. OK \n";
    # do nothing
    exit 0;
  }
  else {
    &usage();
    exit 1;
  }
}
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";
}


编辑mha配置文件
vim /etc/masterha/app1.cnf

[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/usr/local/mysql/data
master_ip_failover_script=/usr/local/bin/master_ip_failover
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
password=manager
ping_interval=1
remote_workdir=/tmp
repl_password=123123
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.2.135 -s 192.168.2.136
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=192.168.2.134
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.2.135
port=3306

[server3]
hostname=192.168.2.136
port=3306

开始进行mha校验
ssh校验

masterha_check_ssh -conf=/etc/masterha/app1.cnf

主从校验

masterha_check_repl -conf=/etc/masterha/app1.cnf

还需要手动配置master机器的vip

/sbin/ifconfig ens33:1 192.168.2.100/24

至此,配置全部完成,下面开始验证mha选主过程

4、mha测试

启动mha

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 &
校验状态
 masterha_check_status --conf=/etc/masterha/app1.cnf

停掉master机器的mysql

systemctl stop mysqld

查看mha日志是否正常。
这个时候我们去master机器执行ifconfig 查看vip是否down掉,并且看slave1机器上的vip是否启用。


image.png

查看主从状态是否正常,测试135是是主,136是从

上一篇 下一篇

猜你喜欢

热点阅读