zoukankan      html  css  js  c++  java
  • MySQL5.6基于MHA方式高可用搭建

    master 10.205.22.185 #MHA node

    slave1 10.205.22.186  #MHA node+MHA manager

    slave2 10.205.22.187  #MHA node

    三台服务器的MySQL已经搭建好主从架构,并互相配置好ssh免密码登录。

    1.下载MHA,并安装MHA包的依赖软件

    https://downloads.mariadb.com/MHA(或者 https://github.com/yotoobo/linux/tree/master/mha
    yum install perl-Config-Tiny perl-Log-Dispatch perl-Time-HiRes perl-Parallel-ForkManager

    2.安装MHA软件包

    rpm -ivh mha4mysql-node-0.56-0.el6.noarch #所有节点安装
    rpm -ivh mha4mysql-manager-0.56-0.el6.noarch #186管理节点安装

    3.创建用户mha管理的账号,在所有mysql服务器上都需要执行:

    GRANT ALL PRIVILEGES ON *.* TO 'mha_rep'@'10.205.22.%' IDENTIFIED BY '123456';
    
    如果是在slave服务器上安装的manager,则需要创建以本机hostname名连接的账号,不然masterha_check_repl测试通不过。
    GRANT ALL PRIVILEGES ON *.* TO 'mha_rep'@'master(主机名)' IDENTIFIED BY '123456'

    4.在MHA manager服务器添加配置文件/etc/masterha/app1.cnf(自定义),并创建相关目录和修改权限

    [server default]
    manager_workdir=/var/log/masterha/app1
    manager_log=/var/log/masterha/app1/manager.log
    secondary_check_script= masterha_secondary_check -s 10.205.22.185 -s 10.205.22.186 #实现多路由监测Master的可用性
    master_ip_failover_script=/usr/local/scripts/master_ip_failover
    
    user=mha_rep
    password=123456
    
    ssh_user=root
    repl_user=repl
    repl_password=password
    ping_interval=1
    
    [server1]
    hostname=10.205.22.185
    candidate_master=1
    master_binlog_dir=/home/data/mysql/binlog
    
    [server2]
    hostname=10.205.22.186
    candidate_master=1
    master_binlog_dir=/home/data/mysql/binlog
    
    [server3]
    hostname=10.205.22.187
    no_master=1
    master_binlog_dir=/home/data/mysql/binlog
    mkdir -p /home/data/mysql/binlog #创建相关目录
    chown -R mysql.mysql /home/data/mysql/binglog #修改权限

    5.编写自动切换VIP的脚本,/usr/local/scripts/master_ip_failover ,并加上可执行权限+x。

    !/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 = '10.205.22.111/24'; # Virtual IP
    my $gateway = '10.205.22.1'; #Gateway IP
    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 "
    
    IN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===
    
    ";
    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 
    ";
    &stop_vip();
    $exit_code = 0;
    };
    if ($@) {
    warn "Got Error: $@
    ";
    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 
    ";
    &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 
    ";
    `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
    ";
    }

    6.检查配置文件

    利用mha工具检测ssh
    masterha_check_ssh --conf=/etc/masterha_application.cnf
    
    使用mha工具check检查repl环境
    masterha_check_repl --conf=/etc/masterha_application.cnf

    7.开启MHA manager,并查看状态

    开启masterha_manager
    nohup masterha_manager --conf=/etc/masterha/app1.cnf > /tmp/mha_manager.log  2>&1 &
    (masterha_stop --conf=/etc/masterha/app1.cnf #关闭时使用的命令)

    查看
    masterha_check_status --conf=/etc/masterha/app1.cnf

    8.模拟MySQL故障,查看VIP漂移和MySQL自动切换情况(切换后MHA服务会自动停止)

    /etc/init.d/mysqld stop #master上操作
    在manager上查看切换日志:tail -f /var/log/masterha/app1/manager.log 
    查看备用节点情况:show master status G

    9.MySQL故障服务器重新加入MHA环境

    1.把故障服务器设为新的slave
    2.重新启动MHA manager
    3.查看MHA状态

    10.在线手动切换主从,没有启用MHA自动切换功能

    1.原master出现故障
    masterha_stop --conf=/etc/masterha/app1.cnf #停止
    masterha_master_switch --master_state=dead --conf=/etc/masterha/app1.cnf --dead_master_host=10.205.22.185 --dead_master_port=3306 --new_master_host=10.205.22.186 --new_master_port=3306 --ignore_last_failover

    2.把原master变为slave切换
    masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_master_host=10.205.22.185 --new_master_port=3306 --orig_master_is_new_slave

    为了保证数据完全一致性,在最快的时间内完成切换,MHA的在线切换必须满足以下条件才会切换成功,否则会切换失败。

    1.所有slave的IO线程都在运行

    2.所有slave的SQL线程都在运行

    3.所有的show slave status的输出中Seconds_Behind_Master参数小于或者等于running_updates_limit秒,如果在切换过程中不指定running_updates_limit,那么默认情况下running_updates_limit为1秒。

    4.在master端,通过show processlist输出,没有一个更新花费的时间大于running_updates_limit秒。

  • 相关阅读:
    微信小程序开发入门(二)
    微信小程序开发入门(一)
    django入门与实践(续)
    django入门与实践(开)
    Python六剑客
    python入门(二十讲):爬虫
    python入门(十九讲):多进程
    ES6箭头函数
    ES6
    数据库常用命令
  • 原文地址:https://www.cnblogs.com/wsl222000/p/5893695.html
Copyright © 2011-2022 走看看