当前位置: 首页 > news >正文

MySQL数据库的高可用

一、MHA工作原理

1、MHA的工作原理

        1、MHA利用 select 1 as value 指令判断master服务器的健康性,一旦master宕机,MHA从宕机崩溃idmaster保存二进制日志事件(binlog events)

        2、识别含有最新更新的slave

        3、应用差异的中继日志(relay log)到其他的slave

        4、应用从master保存的二进制日志事件(binlog events)

        5、提升一个slave为最新的master

        6、使其他的slave连接新的master进行复制

2、MHA的组成及作用

MHA软件由两部分组成,manager工具包和node工具包:

        manager工具包主要包含以下工具:

                masterha_check_ssh :检查MHA的ssh配置状况

                masterha_check_repl :检查MySQL复制状况

                masterha_manger :启动MHA(主程序)

                masterha_check_status :检查当前MHA运行状态

                masterha_master_monitor :检查master是否宕机

                masterha_master_switch :故障转移(自动或手动)

                masterha_conf_host :添加或删除配置的server信息

        node工具包:通常由MHA manager的脚本触发,无需人为操作,主要包含:

                save_binary_logs :保存和复制master的二进制日志

                apply_diff_relay_logs :识别差异的中继日志事件并将其差异的事件应用于其他的slave

                filter_mysqlbinlog :去除不必要的rollback 事件(MHA已不再使用此工具)

                purge_relay_logs :清除中继日志 (不会阻塞SQL线程)

        自定义扩展:

                secondary_check_script: 通过多条网络路由检测master的可用性

                master_ip_ailover_script:更新Application使用的masterip

                shutdown_script: 强制关闭master节点

                report_script: 发送报告

                init_conf_load_script: 加载初始配置参数

                master_ip_online_change_script:更新master节点ip地址

二、配置前提

1、MySQL数据库的主从复制

详细部署请看之前的文章:mysql主从复制及故障修复_mariadb主从复制异常恢复-CSDN博客

验证并提前创建基于MHA服务管理的账号:

show processlist;

show replica status\G

创建基于MHA服务管理的账号:

create user 'mhauser'@'%';
alter user 'mhauser'@'%' identified with mysql_native_password by '123456';
grant all privileges on *.* to 'mhauser'@'%';
select user,host,authentication_string from mysql.user;

重置二进制日志

reset master;

2、提前准备好manger和node安装包

安装包下载地址:https://code.google.com/archive/p/mysql-master-ha/downloads

安装sshpass服务,实现MHA服务与MySQL数据库之间基于key密码认证

yum -y install sshpass
cd
rm -rf /root/.ssh
HOSTS="
192.168.10.110
192.168.10.120
192.168.10.130
"
PASS=123456
ssh-keygen -P "" -f /root/.ssh/id_rsa &> /dev/null
for i in $HOSTS;do{sshpass -p $PASS ssh-copy-id -o StrictHostKeyChecking=no -i /root/.ssh/id_rsa.pub $i  &> /dev/nullscp -rp /root/.ssh $i:/root/}&
done
echo "免密登录配置成功"

3、准备好邮件服务,以备服务告警

yum -y install postfix

  • 编辑/etc/postfix/main.cf文件。
  • 设置发件人域名,找到myhostnamemydomain参数。例如,如果你的服务器主机名是mail.example.com,则myhostname = mail.example.commydomain = example.com
  • 配置 SMTP 认证,添加或修改以下行:
    • smtpd_sasl_type = dovecot
    • smtpd_sasl_path = private/auth
    • smtpd_sasl_local_domain = $myhostname
    • smtpd_sasl_security_options = noanonymous
    • broken_sasl_auth_clients = yes
  • 告诉 Postfix 使用 Dovecot 进行用户认证(假设已经安装并配置了 Dovecot 用于本地用户认证)。

  • 配置发件人地址和 SMTP 认证信息
    • 编辑/etc/postfix/sasl/sasl_passwd文件,添加以下内容:
      • smtp.qq.com your_email@qq.com:your_smtp_authorization_code
      • 其中your_email@qq.com是你的 QQ 邮箱地址,your_smtp_authorization_code是你从 QQ 邮箱获取的 SMTP 授权码。
    • 运行postmap /etc/postfix/sasl/sasl_passwd命令,将sasl_passwd文件转换为 Postfix 可以使用的数据库格式。

安装s-nail软件包来提供mail命令,编写邮件测试,使其将指定的内容发到自己的邮箱中

编写触发邮件的脚本,并授权

vim /usr/local/bin/postfix.sh

添加授权

chmod +x /usr/local/bin/postfix.sh

三、安装mha服务

1、manager节点

        manager节点安装manager程序包和node包,其他被管理节点安装node包

先将事先准备号的安装包传输到主机上

因为需要安装一些mha的依赖包,隐藏需要进行yum install 对安装包进行安装

yum -y install mha4mysql-manager-0.55-1.el5.noarch.rpm
yum -y install mha4mysql-node-0.54-1.el5.noarch.rpm

2、MHA程序配置

对MHA中所安装的mha程序进行设置

先创建目录,将准备号的app1.cnf 文件放入

mkdir /etc/mastermha

[server default]
user='mhauser'
password='123456'
manager_workdir=/data/mastermha/app1/
manager_log=/data/mastermha/app1/manager.log
remote_workdir=/data/mastermha/app1/
ssh_user=root
repl_user='repluser'
repl_password='123456'
ping_interval=1
master_ip_failover_script=/usr/local/bin/master_ip_failover
report_script=/usr/local/bin/sendmail.sh
check_repl_delay=0
master_binlog_dir=/data/mysql/
[server1]
hostname=192.168.10.110
candidate_master=1
[server2]
hostname=192.168.10.120
candidate_master=1
[server3]
hostname=192.168.10.130

将VIP漂移脚本导入到指定位置,并进行授权

#!/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.10.88/24';
my $gateway = '192.168.10.1';
my $interface = 'eth0';
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig $interface:$key $vip;/sbin/arping -I $interface -c 3 -s $vip $gateway > /dev/null 2>&1";
my $ssh_stop_vip = "/sbin/ifconfig $interface:$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";
}

从脚本上可以看到,VIP为192.168.10.88/24

在主MySQL数据库中,设置VIP(只需要在主MySQL数据库上),设置到其本地网卡ens32上

ifconfig ens32:1 192.168.10.88/24

3、检测程序

(1)、检测MHA程序的功能

检查manager管理节点上运行MHA语法和配置检测程序,检测运行环境是否正常

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

出现错误,找不到 SSHCheck.pm 模块

这个模块在 /usr/lib/perl5/vendor_perl/MHA/SSHCheck.pm  下,将其复制到对应的目录下

cp -r /usr/lib/perl5/vendor_perl/MHA /usr/lib64/perl5/vendor_perl/

再次运行检测

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

检测无误后,开始前台启动MHA进程,并提供主MySQL数据库的通用日志实时监控 

masterha_manager --conf=/etc/mastermha/app1.cnf

生产环境放在后台执行

masterha_manager --conf=/etc/mastermha/app1.cnf &> /dev/null

打开主MySQL数据库的通用日志实时监控

tail -f node.log

可以看到其每隔1秒检测一次主服务器的状态信息

(2)、查看主从MySQL数据库的读写状态(0为写,1为读)

select @@read_only;

只有主服务器可以正常写入数据,从服务器只能读数据

(3)、停服验证

将主节点进行停服,验证MHA是否将某个从节点升级为主节点

systemctl stop mysqld

原本的主服务器停服后,原本在其上的VIP  10.88已经消失

二原本的10.120从服务器,其原本的读权变为了写权

VIP成功漂移到10.120上

中继日志已删除,IP飘过来

这时原本MHA前台启动的进程,自动退出了,MHA默认是一次性的,要想其继续实现高可用实现VIP迁移,需要手动进行设置

(4)、将原本的主MySQL数据库回归

先在其上添加change master to 使其成为10.120的从服务器

启动从节点复制功能

(5)、对manager管理节点上运行的MHA语法和配置进行检测程序

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

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

(6)、对MHA服务进行重新启动检测后台MySQL服务器状态信息

masterha_manager --conf=/etc/mastermha/app1.cnf

将现主服务器120,进行停服测验,VIP是否可以继续进行漂移

service mysqld stop

可以看到VIP并未再次进行漂移,其主要原因是因为,MHA默认为一次性服务策略,当一次服务结束后会生成一个/data/mastermha/app1/app1.failover.complete,文件阻止服务再次进行VIP漂移,因此这时需要进行手动调整,将生成的文件进行删除

对主MySQL服务再次进行停止服务

查看VIP是否漂移到另一备用节点上

成功实现VIP的漂移与再次漂移,如果还想要使得120服务再次进行主从复制,者需要在次在服务器进行change master to 指定主服务器IP以及端口,账号以及密码,二进制日志文件以及从什么节点开始复制。


http://www.mrgr.cn/news/56366.html

相关文章:

  • 【二刷hot100】day 4
  • 【软件安装与配置】 vue
  • Python面向对象编程
  • aws 把vpc残留删除干净
  • 立即调用的函数表达式(IIFE)
  • 六、栈————相关概念详解
  • 应对 .DevicData-X-XXXXXXXX 勒索病毒:防御与恢复策略
  • 07 实战:视频捕获
  • 高效HR运营,10佳系统指南
  • 支付域——交易系统设计
  • 防火墙是什么?科普为保护应用层而生的可靠工具
  • MacPro M3无法运行minikube 和 docker
  • 衣柜是在乳胶漆之前装还是之后装好呢?
  • 《使用Gin框架构建分布式应用》阅读笔记:p108-p126
  • Java程序设计:spring boot(7)——数据访问操作
  • 10月22日
  • 虚拟机风格
  • Java笔试06
  • 【功能安全】汽车功能安全个人认证证书
  • 搭建AlexNet神经网络,并搭建自己的图像分类训练和测试的模板,模板通用!!!均有详细注释。
  • 平衡二叉树最全代码
  • 使用SpringBoot自定义注解+AOP+redisson锁来实现防接口幂等性重复提交
  • Java避坑案例 - 消除代码重复_模板方法与工厂模式的最佳实践
  • 【10月最新】植物大战僵尸杂交版即将新增【植物】内容介绍预告(附最新版本下载链接)
  • 23种设计模式具体实现方法
  • 点云数据介绍