博客
关于我
MHA高可用配置与故障切换
阅读量:514 次
发布时间:2019-03-07

本文共 2274 字,大约阅读时间需要 7 分钟。

MHA高可用配置与故障切换

此文介绍了MySQL高可用架构(MHA)的部署与故障切换方法,通过实际实验验证MHA在故障切换中的高效性与数据一致性。

MHA概述

MHA(MySQL High Availability)是一种开源工具,专为MySQL数据库设计,支持自动故障切换。其核心功能包括:

  • 快速故障切换:在0-30秒内完成主库故障切换
  • 数据一致性保障:通过二进制日志和半同步复制机制,最大限度减少数据丢失风险
  • 在线主库切换:通过从库提升为新主库,切换时间可达0.5-2秒
  • MHA通过Manager节点和Node节点组成,Manager节点负责接收外部信号并监控数据节点状态,Node节点则是实际执行增益的工作单位。

    部署MHA

    1. 安装MHA组件

    对于3个数据节点(server1、server2、server3)及Manager节点(server4),执行以下安装命令:

    yum install -y mha4mysql-node-0.58-0.el7.centos.noarch.rpmyum install -y mha4mysql-manager-0.58-0.el7.centos.noarch.rpm

    2. MHA配置

    创建MHA软件目录及配置文件:

    mkdir /etc/masterhamkdir /root/MHA-7/mha4mysql-manager-0.58/samples/conf

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

    [server default]user=rootpassword=westosssh_user=rootmaster_binlog_dir=/data/mysqlremote_workdir=/tmpsecondary_check_script=masterha_secondary_check -s 172.25.12.1 -s 172.25.12.2ping_interval=3master_ip_failover_script=/script/masterha/master_ip_failovershutdown_script=/script/masterha/power_managerreport_script=/script/masterha/send_reportmaster_ip_online_change_script=/script/masterha/master_ip_online_changemanager_workdir=/etc/masterha/app1manager_log=/etc/masterha/app1/manager.logrepl_user=marryrepl_password=westos[server1]hostname=172.25.12.1[server2]hostname=172.25.12.2candidate_master=1[server3]hostname=172.25.12.3no_master=1

    3. 脚本权限设置

    为故障切换脚本赋予执行权限:

    chmod +x master_ip_failoverchmod +x master_ip_online_change

    4. 启动MHA

    在管理节点启动MHA:

    masterha_manager --conf=/etc/masterha/app1.cnf &

    故障切换流程

    手动故障切换

    以下操作流程旨在手动切换主库:

  • 清理故障切换文件
  • rm -rf app1.failover.complete
    1. 执行故障切换
    2. masterha_master_switch --master_state=dead --conf=/etc/masterha/app1.cnf --master_state=alive --new_master_host=172.25.12.2 --new_master_port=3306

      切换后,需启动新主库的复制:

      CHANGE MASTER TO MASTER_HOST='172.25.12.1', MASTER_PORT=3306, MASTER_USER='marry', MASTER_PASSWORD='westos'start slave;

      自动故障切换

    3. 脚本可执行性检查
    4. chmod +x master_ip_failoverchmod +x master_ip_online_change
      1. 编辑脚本
      2. vim master_ip_failover
        1. 启动故障切换
        2. ./master_ip_failover --command=start --orig_master_host=server1 --orig_master_ip=172.25.12.1 --orig_master_port=3306 --new_master_host=server2 --new_master_ip=172.25.12.2 --new_master_port=3306

          实验结果

          通过实验验证,MHA能够在故障发生时快速切换主库,确保服务持续运行。MHA通过半同步复制及二进制日志机制,有效降低数据丢失风险,最大限度保障数据库一致性。在自动切换过程中,VIP地址自动迁移,最终VIP记录在新的主库,确保业务连续性。

          本实验验证了MHA在高可用架构中的有效性,为 MySQL 集群的高可用部署提供了可靠的解决方案。

    转载地址:http://jlknz.baihongyu.com/

    你可能感兴趣的文章
    Python 子进程 Popen.communicate() 等价于 Popen.stdout.read()?
    查看>>
    Python 子进程 Popen:为什么会出现“ls *.txt“?不行?
    查看>>
    Python 子进程参数
    查看>>
    python 字典 key 和value 互换
    查看>>
    Python 字典 vs If 语句速度
    查看>>
    python 字典sorted自定义排序,按照key or value排序
    查看>>
    python 字典和列表区别_元组列表和字典的主要区别是什么?
    查看>>
    python 字符串中特定字符替换,截取
    查看>>
    Python 字符串总结
    查看>>
    python 字符串显示中文_Python字符串开头的b"、u"、r"与中文乱码
    查看>>
    Python 字符串的几种拼装方式
    查看>>
    python 存入数据库bigint_python基础_MySQL的bigint类型
    查看>>
    python 学习 面向对象编程
    查看>>
    Python 学习小结
    查看>>
    Python 学习日记第三篇 -- 字典
    查看>>
    Python 学习笔记(一)Data type
    查看>>
    python 安装 easy_install 和 pip 流程
    查看>>
    python 安装echarts
    查看>>
    python 安装opencv及问题解决
    查看>>
    Python 安装程序:“写入文件 C:\Python27\pythonw.exe 时出错“
    查看>>