首页 > 数据库 >MySQL-MHA搭建

MySQL-MHA搭建

时间:2023-06-12 21:48:07浏览次数:76  
标签:-- mha host master ssh MySQL new MHA 搭建

1、测试环境

目前MHA主要支持一主多从的架构,要搭建MHA,要求一个复制集群中必须最少有三台数据库服务器,一主二从,即一台充当master,一台充当备用master,另外一台充当从库.

一主两从的环境,通过GTID方式配置主从复制同步环境

用途 IP地址 安装软件 MySQL版本
主库 db01 192.168.3.112 mha-node mysql 8.0.24
从库 db02 192.168.3.113 mha-node mysql 8.0.24
从库 db03 192.168.3.114 mha-manager、mha-node mysql 8.0.24

创建MySQL相关命令软链接

## 三台服务器都需执行
# ln -s /opt/mysql/bin/mysqlbinlog /usr/bin/
# ln -s /opt/mysql/bin/mysql /usr/bin/

配置三台主机之间ssh免密互信登录,配置完成三台主机验证确认无误

## 三台服务器都需执行
# rm -rf /root/.ssh/
## 只在主库上执行
# cd /root/.ssh
# mv id_rsa.pub authorized_keys
# ssh-keygen
# scp -r /root/.ssh 192.168.3.114:/root  
# scp -r /root/.ssh 192.168.3.113:/root

mha软件下载

mha版本

  • 0.56 适用于 mysql5.6 版本系列

  • 0.57 适用于 mysql5.7 版本系列

  • 0.58 适用于 mysql8.0 版本系列

安装mha软件

## 三台服务器都需执行
# yum install perl-DBD-MySQL -y
# rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm

## 在db01上创建需要的mha数据库用户 
db01 [(none)]>create user mha@'192.168.3.%' identified with mysql_native_password by 'mha';

db01 [(none)]>grant all privileges on *.* to mha@'192.168.3.%';

## 在db03安装mha-manger软件
# yum install -y perl-Config-Tiny epel-release perl-Log-Dispatch perl-ParallelForkManager perl-Time-HiRes
# yum install mha4mysql-manager-0.58-0.el7.centos.noarch.rpm -y

# mkdir -p /etc/mha
# mkdir -p /var/log/mha/app1
vim /etc/mha/app1.cnf
[server default]
manager_log=/var/log/mha/app1/manager
manager_workdir=/var/log/mha/app1
master_binlog_dir=/opt/mysql/binlog/
user=mha
password=mha
ping_interval=2
repl_password=repl
repl_user=repl
ssh_user=root
[server1]
hostname=192.168.3.112
port=3306
[server2]
hostname=192.168.3.113
candidate_master=1
port=3306
[server3]
hostname=192.168.3.114
port=3306

## 互信检查
masterha_check_ssh --conf=/etc/mha/app1.cnf
## 主从状态检查
masterha_check_repl --conf=/etc/mha/app1.cn
## 启动mha
# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null> /var/log/mha/app1/manager.log 2>&1 &
## 查看mha状态
# masterha_check_status --conf=/etc/mha/app1.cnf

配置 VIP 过程

## 配置脚本
# 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.3.111/24';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
my $ssh_Bcast_arp= "/sbin/arping -I ens33 -c 3 -A 192.168.3.111";
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";
}

## 添加脚本路径到manager配置文件
# vim /etc/mha/app1.cnf
master_ip_failover_script=/usr/local/bin/master_ip_failover

## 重启mha
# masterha_stop --conf=/etc/mha/app1.cnf

## 手动添加VIP
# masterha_check_status --conf=/etc/mha/app1.cnf
# ifconfig ens33:1 192.168.3.111/24

配置邮件提醒

# more send_report 
#!/usr/bin/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 Mail::Sender;
use Getopt::Long;
 
#new_master_host and new_slave_hosts are set only when recovering master succeeded
my ( $dead_master_host, $new_master_host, $new_slave_hosts, $subject, $body );
my $smtp='smtp.qq.com';        # smtp服务器
my $mail_from='@qq.com';       # 发件箱
my $mail_user='';              # QQ号
my $mail_pass='';              # 授权码
#my $mail_to=['[email protected]','[email protected]'];  # 多个收件箱
my $mail_to='@qq.com';         # 收件箱
 
GetOptions(
  'orig_master_host=s' => \$dead_master_host,
  'new_master_host=s'  => \$new_master_host,
  'new_slave_hosts=s'  => \$new_slave_hosts,
  'subject=s'          => \$subject,
  'body=s'             => \$body,
);
 
# Do whatever you want here
mailToContacts($smtp,$mail_from,$mail_user,$mail_pass,$mail_to,$subject,$body);
 
sub mailToContacts {
	my ($smtp, $mail_from, $mail_user, $mail_pass, $mail_to, $subject, $msg ) = @_;
	open my $DEBUG, ">/tmp/mail.log"
		or die "Can't open the debug	file:$!\n";
	my $sender = new Mail::Sender {
		ctype		=> 'text/plain;charset=utf-8',
		encoding	=> 'utf-8',
		smtp		=> $smtp,
		from		=> $mail_from,
		auth		=> 'LOGIN',
		TLS_allowed	=> '0',
		authid		=> $mail_user,
		authpwd		=> $mail_pass,
		to		=> $mail_to,
		subject		=> $subject,
		debug		=> $DEBUG
	};
	$sender->MailMsg(
		{
			msg => $msg,
			debug => $DEBUG
		}
	) or print $Mail::Sender::Error;
	return 1;
}
 
exit 0;

## manager配置文件中添加,重启mha
report_script=/usr/local/bin/send_report

对于异步复制方式,可以添加 binlog 的日志补偿,减少数据丢失。

## db03 创建保存binlog日志文件目录 
# mkdir -p /data/binlog_master/
# chown -R mysql.mysql /data/*
# cd /data/binlog_master/
# mysql -e "show slave status \G"|grep "Master_Log"
# masterha_check_status --conf=/etc/mha/app1.cnf

# mysqlbinlog -R --host=192.168.3.112 --user=mha --password=mha --raw --stop-never mysql-bin.000002 &

# vim /etc/mha/app1.cnf
[binlog1]
no_master=1
hostname=192.168.3.114
master_binlog_dir=/data/binlog_master/

# masterha_stop --conf=/etc/mha/app1.cnf
# nohup masterha_manager --conf=/etc/mha/app1.cnf --
remove_dead_master_conf --ignore_last_failover < /dev/null>
/var/log/mha/app1/manager.log 2>&1 &

binlog server不是100%可靠的。他模拟的是从库IO线程的功能(异步)。

替代方案: 5.7+GTID+增强半同步。

注意的是,当master数据库出现故障后,切换主库后,mha程序自动停止,只能监测切换一次,需要手动进行恢复mha高可用。

数据一致性保证同步配置

#*************** rpl_semi_sync ***************
rpl_semi_sync_master_enabled =ON
rpl_semi_sync_master_timeout =5000
rpl_semi_sync_master_wait_for_slave_count =1
rpl_semi_sync_master_wait_no_slave =ON
rpl_semi_sync_master_wait_point =AFTER_SYNC
rpl_semi_sync_slave_enabled =ON
#*************** group commit ***************
binlog_group_commit_sync_delay =1
binlog_group_commit_sync_no_delay_count =1000
#*************** gtid ***************
gtid_mode =ON
enforce_gtid_consistency =ON
log_slave_update =1
#*************** gtid ***************
slave_parallel_type =LOGICAL_CLOCK
slave_parallel_workers =4
master_info_repository =TABLE
relay_log_info_repository =TABLE

标签:--,mha,host,master,ssh,MySQL,new,MHA,搭建
From: https://www.cnblogs.com/zbc230/p/17472932.html

相关文章

  • 熟悉常用的mysql增删改查命令
    增删改查,掌握基本的数据库基础操作命令1.创建数据库createdatabasepems2;这个是连接数据库的工具查询已有的数据库,也可以在服务器进入数据库命令行查询mysql>showdatabases;+--------------------+|Database|+--------------------+|information_schema||mysql......
  • vulhub靶场搭建,以及使用方法
    如何安装vulhub靶场备份文件(如果没有重要的东西,就非必要)cp/etc/yum.repos.d/CentOS-Base.repo/etc/yum.repos.d/CentOS-Base.repo.bak下载阿里云覆盖原文件curl-o/etc/yum.repos.d/CentOS-Base.repohttp://mirrors.aliyun.com/repo/Centos-7.repo查看内容确定成功覆盖......
  • Docker 安装 MySQL8 数据库
    创建数据卷mkdir-p/usr/mysql/conf/usr/mysql/datachmod-R755/usr/mysql/创建配置文件vim/usr/mysql/conf/my.cnf:[client]#socket=/usr/mysql/mysqld.sockdefault-character-set=utf8mb4[mysqld]#pid-file=/var/run/mysqld/mysqld.pid#sock......
  • 尚医通day01-【项目环境搭建和医院设置详细步骤】(内附源码)
    第01章-项目介绍1、课程介绍项目名称:尚医通预约挂号统一平台项目原型:https://www.114yygh.com北京市预约挂号统一平台项目技术栈:前后端分离后端技术:SpringBoot+SpringCloud+MyBatisPlus+MySQL+MongoDB+Redis+RabbitMQ+Docker+EasyExcel+API远程接口调......
  • 使用containerd从0搭建k8s(kubernetes)集群
    准备环境准备两台服务器节点,如果需要安装虚拟机,可以参考《wmware和centos安装过程》机器名IP角色CPU内存centos01192.168.109.130master4核2Gcentos02192.168.109.131node4核2G设置主机名,所有节点都执行vim/etc/hosts#增加192.168.109.130cento......
  • 尚医通day09-【用户平台搭建详细步骤】(内附源码)
    页面预览首页医院详情第01章-服务器端渲染和客户端渲染1、搜索引擎优化1.1、什么是搜索引擎优化SEO是网站为了获得更多的流量,对网站的结构及内容进行调整和优化,以便搜索引擎(百度,google等)更好抓取到网站的内容,提高自已的网站排名。1.2、搜索引擎工作流程1.3、简单的S......
  • Qt打包程序移动到新环境时提示 QMYSQL driver not loaded
    Qt版本是:Qt6.3.2MySQL版本是:mysql8.0.33运行时日志提示如下:Warning:File:()Line:(0)QSqlDatabase:QMYSQLdrivernotloaded(2023-06-1217:16:56)Warning:File:()Line:(0)QSqlDatabase:availabledrivers:QSQLITEQMARIADBQMYSQLQODBCQPSQL(2023-06-1217:16:56)......
  • 【Ubuntu22.04】安装MySQL数据库,修改root用户密码,实现远程访问,
    预备条件本次实验使用静态IP的地址192.168.1.81作为mysql-001服务器地址,并配置为本地域名mysql-001:打开Powershell(Window自带)使用SSH方式连接服务器,用户名test,密码:123456:安装Mysql:更新软件源aptupdate安装MySQL8.0,因为Ubuntu22.04不支持MySQL5.7sudoaptinst......
  • 数据库Navicat for MySQL 初步学习
     首先引入命名空间然后 staticMySqlConnectionconn=null;  在里面定义我们数据库的位置  目前联系的是本地数据库所有就按本地来 第二数据库内容包括 增 删 改 查  其中的 MySqlCommandcmd=newMySqlCommand("insertintouserinfosetname='xi......
  • 1分钟搭建自己的Chatjpt网站
    首先搭建一个GPT网站,我们需要一台云服务器和一份镜像文件。我这里以阿里云为例,然后点击左上角的产品选择云服务器,点击立即购买。在这里选择阿里云提供的国际服务器,好处是可以避免网络带来的烦恼,在购买页选择按量付费,并且地域选择美国硅谷服务器,为了降低成本,我们可以选择一核0.5g的......