mysql mha

公司里有个项目用到了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是从

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,525评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,203评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,862评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,728评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,743评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,590评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,330评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,244评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,693评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,885评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,001评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,723评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,343评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,919评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,042评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,191评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,955评论 2 355

推荐阅读更多精彩内容

  • 一、MHA 1、解压mysql包到/opt目录 [root@localhost opt]# tar -zxvf m...
    1727_7759阅读 98评论 0 0
  • 部署过程 主从环境准备 首先先搭建一主两从GTID复制环境,此处不赘述 配置关键程序软链接 (所有节点均执行) 配...
    这货不是王马勺阅读 254评论 0 1
  • 第1章 MySQL高可用介绍 1.1 企业高可用评估标准 评估标准是 全年无故障率指标: 3个9 99.9%...
    张新雨有点胖噢阅读 972评论 0 1
  • 一、高可用及读写分离 1.主从复制架构演变介绍 1.1 基本机构 (1)一主一从(2)一主多从(3)多级主从(4)...
    我要笑阅读 218评论 0 0
  • MHA具体的搭建环境如下: 角色 IP地址 ...
    mk_阅读 685评论 0 4