现在的位置: 首页 > 综合 > 正文

MySQL 5.5 keepalived + haproxy + MHA 自动容灾

2014年02月22日 ⁄ 综合 ⁄ 共 7608字 ⁄ 字号 评论关闭

vip 192.168.161.80

mysql master 192.168.161.81

mysql backup 192.168.161.82(haproxy master)

mysql slave     192.168.161.83 (haproxy backup)

keepalived安装:

yum -y install ipvsadm openssl-devel libnl* popt-static
uname -r
ln -s /usr/src/kernels/2.6.32-279.el6.x86_64 /usr/src/linux
wget http://www.keepalived.org/software/keepalived-1.2.6.tar.gz
tar zxf keepalived-1.2.6.tar.gz 
cd keepalived-1.2.6
./configure
make 
make install 
#在82上配置
vim /usr/local/etc/keepalived/keepalived.conf
global_defs {
   notification_email {
     DBA@xiaomi.com
   }
   notification_email_from 192.168.161.82@xiaomi.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id  NodeA
}

vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 82
    nopreempt
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.161.80/24
    }
}

在83上配置
vim /usr/local/etc/keepalived/keepalived.conf
global_defs {
   notification_email {
     DBA@xiaomi.com
   }
   notification_email_from 192.168.161.83@xiaomi.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id  NodeA
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth1
    virtual_router_id 83
    nopreempt
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.161.80/24
    }
}


/usr/local/sbin/keepalived -f /usr/local/etc/keepalived/keepalived.conf

haproxy安装:

wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.22.tar.gz
tar zxvf haproxy-1.4.22.tar.gz
cd haproxy-1.4.22
uname -a //查看linux内核版本
make TARGET=linux26 PREFIX=/usr/local/haproxy-1.4.22
make install PREFIX=/usr/local/haproxy-1.4.22
#在82和83上创建conf文件
vim /usr/local/haproxy-1.4.22/conf/haproxy.conf
global
        maxconn 10240
        chroot /usr/local/haproxy-1.4.22
        uid 99
        gid 99
        daemon
        quiet
        nbproc 3
        pidfile /tmp/haproxy.pid

defaults
        mode http
        retries 2
        option redispatch
        option abortonclose
        maxconn 4096
        timeout check 2000
        log 127.0.0.1 local0 err #[err warning info debug]

listen test1
        bind 0.0.0.0:5306
        mode tcp
        maxconn 4086
        #log 127.0.0.1 local0 debug
        server s1 192.168.161.81:3306

#创建替换脚本 这里没有使用平滑重启
vim /opt/shell/haproxy_reload.sh
#!/bin/sh
sed 's/'$1'/'$2'/g' -i /usr/local/haproxy-1.4.22/conf/haproxy.conf
ps aux | grep 'haproxy -f /usr/local/haproxy-1.4.22/conf/haproxy.conf' | grep -v grep | awk '{print $2}' | xargs kill
/usr/local/haproxy-1.4.22/sbin/haproxy -f /usr/local/haproxy-1.4.22/conf/haproxy.conf

#启动haproxy
/usr/local/haproxy-1.4.22/sbin/haproxy  -f /usr/local/haproxy-1.4.22/conf/haproxy.conf 


在81和83上安装mha node,在82上安装mha manager

#mysql bin
cp /usr/local/mysql/bin/* /usr/local/bin/

#mysql node
yum install perl-CPAN -y
tar zxf mha4mysql-node-0.53.tar.gz
cd mha4mysql-node-0.53
perl Makefile.PL 
make && make install

#mha manager
yum install perl-Config-Tiny perl-Params-Validate perl-Log-Dispatch perl-Parallel-ForkManager -y
tar zxf mha4mysql-manager-0.53.tar.gz
cd mha4mysql-manager-0.53
perl Makefile.PL 
make && make install

#在三台服务器上分别执行public ssh
ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@182.168.161.80

 ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.161.81ssh-copy-id -i /root/.ssh/id_rsa.pub root@182.168.161.82

ssh-copy-id -i /root/.ssh/id_rsa.pub root@182.168.161.83

#replication user
GRANT ALL PRIVILEGES ON *.* TO 'rep_user'@'192.168.%' IDENTIFIED BY '123456';

#slave set
set global read_only=1;
set global relay_log_purge=0;
01 00 * * * /usr/local/bin/purge_relay_logs -user=rep_user -password=123456 -disable_relay_log_purge >> /tmp/purge_relay_logs.log 2>&1

#check
masterha_check_ssh --conf=/usr/local/mha4mysql-manager-0.53/conf/app1.cnf --remove_dead_master_conf 
masterha_check_repl --conf=/usr/local/mha4mysql-manager-0.53/conf/app1.cnf --remove_dead_master_conf

#start
nohup masterha_manager --conf=/usr/local/mha4mysql-manager-0.53/conf/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /tmp/mha_manager.log 2>&1 &

#status
masterha_check_status --conf=/usr/local/mha4mysql-manager-0.53/conf/app1.cnf

#log
tail -1000 /var/log/masterha/app1/manager.log

#error
#[error][/usr/lib/perl5/site_perl/5.8.8/MHA/ManagerUtil.pm, ln178] Got ERROR: Use of uninitialized value in scalar chomp at /usr/lib/perl5/site_perl/5.8.8/MHA/ManagerConst.pm line 90.
vi /usr/local/share/perl5/MHA/ManagerConst.pm
89   my $msg  = $args{message};
90   $msg = "" unless($msg);  <=插入一行
91   chomp $msg;

mha 配置文件

vim/usr/local/mha4mysql-manager-0.53/conf/app1.cnf

[server default]
manager_workdir=/usr/local/mha4mysql-manager-0.53
manager_log=/usr/local/mha4mysql-manager-0.53/manager.log
master_ip_failover_script="/usr/local/mha4mysql-manager-0.53/scripts/master_ip_failover"
master_ip_online_change_script="/usr/local/mha4mysql-manager-0.53/scripts/master_ip_online_change"
ping_interval=1
user=xm_dba
password=123456
repl_user=rep_user
repl_password=123456
ssh_user=root

[server1]
hostname=192.168.161.81
port=3306
candidate_master=1
master_binlog_dir="/home/mysql/data3306"

[server2]
hostname=192.168.161.82
port=3306
candidate_master=1
master_binlog_dir="/home/mysql/data3306"

[server3]
hostname=192.168.161.83
port=3306
no_master=1
master_binlog_dir="/home/mysql/data3306"


vim /usr/local/mha4mysql-manager-0.53/scripts/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.161.80';  # Virtual IP 
my $key = "80"; 
my $ssh_start_vip = "ifup eth0:$key ";
my $ssh_stop_vip = "ifdown eth0:$key ";
my $check_vip = "ip add";
my $m_haproxy_ip = "192.168.161.82";
my $s_haproxy_ip = "192.168.161.83";
my $haproxy_change_master = "";
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,
);
#my $haproxy_change_master = "/opt/shell/haproxy_reload.sh $orig_master_ip $new_master_ip ";

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\@$vip \" $check_vip \"`;
        exit 0;
    }
    else {
        &usage();
        exit 1;
    }
}

# A simple system call that enable the VIP on the new master 
sub start_vip() {
    $haproxy_change_master = "/opt/shell/haproxy_reload.sh $orig_master_ip $new_master_ip ";
    `ssh $ssh_user\@$m_haproxy_ip \" $haproxy_change_master \"`;
    `ssh $ssh_user\@$s_haproxy_ip \" $haproxy_change_master \"`;
    #`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的脚本改过来的,以后在进一步优化吧。





抱歉!评论已关闭.