master ha 安装过程

master ha 安装过程


master:192.168.199.126
slave1:192.168.199.127
slave2:192.168.199.128
slave3:192.168.199.129
binlogserver:192.168.199.128
VIP:192.168.199.125


一.安装mysql,并做主从同步
#创建mysql用户及数据目录,在所有机器上执行
ln -s /opt/mysql/mysql-5.6.27-linux-glibc2.5-x86_64 /usr/local/mysql
mkdir /data/mysql/mysql3316{data,logs,tmp} -p
useradd -s /sbin/nologin mysql
chown -R mysql:mysql /data/mysql/mysql3316
#初始化并启动
cd /usr/local/mysql
./scripts/mysql_install_db --defaults-file=/data/mysql/mysql3316/my3316.cnf
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3316/my3316.cnf &


#安装加固并授权,在master上执行
/usr/local/mysql/bin/mysql -S /tmp/mysql3316.sock -e "delete from mysql.user where user!='root' or host!='localhost';drop database test;truncate mysql.db;"
/usr/local/mysql/bin/mysql -S /tmp/mysql3316.sock -e "grant all on *.* to 'thunder'@'%' identified by 'thunder';grant replication"


/usr/local/mysql/bin/mysqldump -S /tmp/mysql3316.sock -A -B --single-transaction >116_full_20160120.sql


#把备份的数据下发到所有从上面,做主从,在所有slave上执行
/usr/local/mysql/bin/mysql -S /tmp/mysql3316.sock < 116_full_20160120.sql


#建立binlogserver,在binlogserver上执行
mkdir /data/mysql/binlog_1163316 -p
mysqlbinlog -R --host=192.168.199.126 --port=3316 --user=thunder --password=thunder --raw --stop-never mysql-bin.000001 &


二.物理机互信,在所有机器上
ssh-keygen 一路回车
ssh-copy-id 192.168.199.126
ssh-copy-id 192.168.199.127
ssh-copy-id 192.168.199.128
ssh-copy-id 192.168.199.129


执行完后测试,观察ssh到其它机器上是否需要密码


三.安装mha,node需要在所有节点上安装,manager需要在监控节点上安装
mha的下载地址为:https://code.google.com/p/mysql-master-ha/wiki/Downloads,无法翻墙可以通过baidu search
#安装epel源,我的环境为rhel6.4,centos的可以跳过
rpm -ivh http://mirrors.ustc.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm
sed -i 's/#baseurl/baseurl/g' /etc/yum.repos.d/epel.repo
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/epel.repo
#安装依赖
yum install perl-DBD-MySQL  perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager -y
#安装mha
rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm


四.配置文件,需要5个配置文件,在manager节点上配置好下发到所有节点
mkdir /etc/masterha;cd /etc/masterha
ll
total 32
-rwxr-xr-x. 1 root root   898 Jan 20 17:46 app1.conf
-rwxr-xr-x. 1 root root    57 Jan 20 18:13 drop_vip.sh
-rwxr-xr-x. 1 root root    57 Jan 20 18:13 init_vip.sh
-rwxr-xr-x. 1 root root   358 Mar  4  2015 masterha_default.conf
-rwxr-xr-x. 1 root root  3890 Jan 20 18:57 master_ip_failover
-rwxr-xr-x. 1 root root 10300 Jan 20 18:57 master_ip_online_change
=================================================
cat app1.conf 


[server default]




#mha manager工作目录
manager_workdir = /var/log/masterha/app1
manager_log = /var/log/masterha/app1/app1.log
remote_workdir = /var/log/masterha/app1


[server1]
hostname=192.168.199.126
port=3316
master_binlog_dir = /data/mysql/mysql3316/logs
candidate_master = 1
check_repl_delay = 0     #用防止master故障时,切换时slave有延迟,卡在那里切不过来。


[server2]
hostname=192.168.199.127
port=3316
master_binlog_dir=/data/mysql/mysql3316/logs
candidate_master=1
check_repl_delay=0


[server3]
hostname=192.168.199.128
port=3316
master_binlog_dir=/data/mysql/mysql3316/logs
candidate_master=1
check_repl_delay=0


[server4]
hostname=192.168.199.129
port=3316
master_binlog_dir=/data/mysql/mysql3316/logs
candidate_master=0
check_repl_delay=0


[binlog1]
hostname=192.168.199.128
master_binlog_dir=/data/mysql/binlog_1263316
candidate_master=0
check_repl_delay=0
=================================================
cat masterha_default.conf


[server default]
#MySQL的用户和密码
user=thunder
password=thunder


#系统ssh用户
ssh_user=root


#复制用户
repl_user=repl
repl_password= repl4slave


#监控
ping_interval=1
#shutdown_script=""


#切换调用的脚本
master_ip_failover_script= /etc/masterha/master_ip_failover
master_ip_online_change_script= /etc/masterha/master_ip_online_change
=================================================
cat drop_vip.sh


vip="192.168.199.125/32"
/sbin/ip addr del $vip dev eth0
=================================================
cat init_vip.sh


vip="192.168.199.125/32"
/sbin/ip addr add $vip dev eth0
=================================================
cat master_ip_failover 
#!/usr/bin/env 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 Getopt::Long;
use MHA::DBHelper;
#自定义该组机器的vip
my $vip = "192.168.199.125";
my $if = "eth0";
my (
  $command,        $ssh_user,         $orig_master_host,
  $orig_master_ip, $orig_master_port, $new_master_host,
  $new_master_ip,  $new_master_port,  $new_master_user,
  $new_master_password
);


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,
  'new_master_user=s'     => \$new_master_user,
  'new_master_password=s' => \$new_master_password,
);


sub add_vip {
        my $output1 = `ssh $orig_master_host /sbin/ip addr del $vip/32 dev $if`;
        my $output2 = `ssh $new_master_host /sbin/ip addr add $vip/32 dev $if`;


}
exit &main();


sub main {
  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 {


      # updating global catalog, etc
      $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 {
      my $new_master_handler = new MHA::DBHelper();


      # args: hostname, port, user, password, raise_error_or_not
      $new_master_handler->connect( $new_master_ip, $new_master_port,
        $new_master_user, $new_master_password, 1 );


      ## Set read_only=0 on the new master
      $new_master_handler->disable_log_bin_local();
      print "Set read_only=0 on the new master.\n";
      $new_master_handler->disable_read_only();


      ## Creating an app user on the new master
      #print "Creating app user on the new master..\n";
      #FIXME_xxx_create_user( $new_master_handler->{dbh} );
      $new_master_handler->enable_log_bin_local();
      $new_master_handler->disconnect();


      ## Update master ip on the catalog database, etc
      &add_vip();
      $exit_code = 0;
    };
    if ($@) {
      warn $@;


      # If you want to continue failover, exit 10.
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "status" ) {


    # do nothing
    exit 0;
  }
  else {
    &usage();
    exit 1;
  }
}


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";
}
=================================================
cat master_ip_online_change


#!/usr/bin/env 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 Getopt::Long;
use MHA::DBHelper;
use MHA::NodeUtil;
use Time::HiRes qw( sleep gettimeofday tv_interval );
use Data::Dumper;


my $_tstart;
my $_running_interval = 0.1;
#添加vip定义
my $vip = "192.168.199.125";
my $if = "eth0";


my (
  $command,              $orig_master_is_new_slave, $orig_master_host,
  $orig_master_ip,       $orig_master_port,         $orig_master_user,
  $orig_master_password, $orig_master_ssh_user,     $new_master_host,
  $new_master_ip,        $new_master_port,          $new_master_user,
  $new_master_password,  $new_master_ssh_user,
);
GetOptions(
  'command=s'                => \$command,
  'orig_master_is_new_slave' => \$orig_master_is_new_slave,
  'orig_master_host=s'       => \$orig_master_host,
  'orig_master_ip=s'         => \$orig_master_ip,
  'orig_master_port=i'       => \$orig_master_port,
  'orig_master_user=s'       => \$orig_master_user,
  'orig_master_password=s'   => \$orig_master_password,
  'orig_master_ssh_user=s'   => \$orig_master_ssh_user,
  'new_master_host=s'        => \$new_master_host,
  'new_master_ip=s'          => \$new_master_ip,
  'new_master_port=i'        => \$new_master_port,
  'new_master_user=s'        => \$new_master_user,
  'new_master_password=s'    => \$new_master_password,
  'new_master_ssh_user=s'    => \$new_master_ssh_user,
);


exit &main();
sub drop_vip {
        my $output = `ssh $orig_master_host /sbin/ip addr del $vip/32 dev $if`;
        #mysql里的连接全部干掉
        #FIXME
}
sub add_vip {
        my $output = `ssh $new_master_host /sbin/ip addr add $vip/32 dev $if`;


}




sub current_time_us {
  my ( $sec, $microsec ) = gettimeofday();
  my $curdate = localtime($sec);
  return $curdate . " " . sprintf( "%06d", $microsec );
}


sub sleep_until {
  my $elapsed = tv_interval($_tstart);
  if ( $_running_interval > $elapsed ) {
    sleep( $_running_interval - $elapsed );
  }
}


sub get_threads_util {
  my $dbh                    = shift;
  my $my_connection_id       = shift;
  my $running_time_threshold = shift;
  my $type                   = shift;
  $running_time_threshold = 0 unless ($running_time_threshold);
  $type                   = 0 unless ($type);
  my @threads;


  my $sth = $dbh->prepare("SHOW PROCESSLIST");
  $sth->execute();


  while ( my $ref = $sth->fetchrow_hashref() ) {
    my $id         = $ref->{Id};
    my $user       = $ref->{User};
    my $host       = $ref->{Host};
    my $command    = $ref->{Command};
    my $state      = $ref->{State};
    my $query_time = $ref->{Time};
    my $info       = $ref->{Info};
    $info =~ s/^\s*(.*?)\s*$/$1/ if defined($info);
    next if ( $my_connection_id == $id );
    next if ( defined($query_time) && $query_time < $running_time_threshold );
    next if ( defined($command)    && $command eq "Binlog Dump" );
    next if ( defined($user)       && $user eq "system user" );
    next
      if ( defined($command)
      && $command eq "Sleep"
      && defined($query_time)
      && $query_time >= 1 );


    if ( $type >= 1 ) {
      next if ( defined($command) && $command eq "Sleep" );
      next if ( defined($command) && $command eq "Connect" );
    }


    if ( $type >= 2 ) {
      next if ( defined($info) && $info =~ m/^select/i );
      next if ( defined($info) && $info =~ m/^show/i );
    }


    push @threads, $ref;
  }
  return @threads;
}


sub main {
  if ( $command eq "stop" ) {
    ## Gracefully killing connections on the current master
    # 1. Set read_only= 1 on the new master
    # 2. DROP USER so that no app user can establish new connections
    # 3. Set read_only= 1 on the current master
    # 4. Kill current queries
    # * Any database access failure will result in script die.
    my $exit_code = 1;
    eval {
      ## Setting read_only=1 on the new master (to avoid accident)
      my $new_master_handler = new MHA::DBHelper();


      # args: hostname, port, user, password, raise_error(die_on_error)_or_not
      $new_master_handler->connect( $new_master_ip, $new_master_port,
        $new_master_user, $new_master_password, 1 );
      print current_time_us() . " Set read_only on the new master.. ";
      $new_master_handler->enable_read_only();
      if ( $new_master_handler->is_read_only() ) {
        print "ok.\n";
      }
      else {
        die "Failed!\n";
      }
      $new_master_handler->disconnect();


      # Connecting to the orig master, die if any database error happens
      my $orig_master_handler = new MHA::DBHelper();
      $orig_master_handler->connect( $orig_master_ip, $orig_master_port,
        $orig_master_user, $orig_master_password, 1 );


      ## Drop application user so that nobody can connect. Disabling per-session binlog beforehand
      $orig_master_handler->disable_log_bin_local();
     # print current_time_us() . " Drpping app user on the orig master..\n";
      print current_time_us() . " drop vip $vip..\n";
      #drop_app_user($orig_master_handler);
     &drop_vip();


      ## Waiting for N * 100 milliseconds so that current connections can exit
      my $time_until_read_only = 15;
      $_tstart = [gettimeofday];
      my @threads = get_threads_util( $orig_master_handler->{dbh},
        $orig_master_handler->{connection_id} );
      while ( $time_until_read_only > 0 && $#threads >= 0 ) {
        if ( $time_until_read_only % 5 == 0 ) {
          printf
"%s Waiting all running %d threads are disconnected.. (max %d milliseconds)\n",
            current_time_us(), $#threads + 1, $time_until_read_only * 100;
          if ( $#threads < 5 ) {
            print Data::Dumper->new( [$_] )->Indent(0)->Terse(1)->Dump . "\n"
              foreach (@threads);
          }
        }
        sleep_until();
        $_tstart = [gettimeofday];
        $time_until_read_only--;
        @threads = get_threads_util( $orig_master_handler->{dbh},
          $orig_master_handler->{connection_id} );
      }


      ## Setting read_only=1 on the current master so that nobody(except SUPER) can write
      print current_time_us() . " Set read_only=1 on the orig master.. ";
      $orig_master_handler->enable_read_only();
      if ( $orig_master_handler->is_read_only() ) {
        print "ok.\n";
      }
      else {
        die "Failed!\n";
      }


      ## Waiting for M * 100 milliseconds so that current update queries can complete
      my $time_until_kill_threads = 5;
      @threads = get_threads_util( $orig_master_handler->{dbh},
        $orig_master_handler->{connection_id} );
      while ( $time_until_kill_threads > 0 && $#threads >= 0 ) {
        if ( $time_until_kill_threads % 5 == 0 ) {
          printf
"%s Waiting all running %d queries are disconnected.. (max %d milliseconds)\n",
            current_time_us(), $#threads + 1, $time_until_kill_threads * 100;
          if ( $#threads < 5 ) {
            print Data::Dumper->new( [$_] )->Indent(0)->Terse(1)->Dump . "\n"
              foreach (@threads);
          }
        }
        sleep_until();
        $_tstart = [gettimeofday];
        $time_until_kill_threads--;
        @threads = get_threads_util( $orig_master_handler->{dbh},
          $orig_master_handler->{connection_id} );
      }


      ## Terminating all threads
      print current_time_us() . " Killing all application threads..\n";
      $orig_master_handler->kill_threads(@threads) if ( $#threads >= 0 );
      print current_time_us() . " done.\n";
      $orig_master_handler->enable_log_bin_local();
      $orig_master_handler->disconnect();


      ## After finishing the script, MHA executes FLUSH TABLES WITH READ LOCK
      $exit_code = 0;
    };
    if ($@) {
      warn "Got Error: $@\n";
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "start" ) {
    ## Activating master ip on the new master
    # 1. Create app user with write privileges
    # 2. Moving backup script if needed
    # 3. Register new master's ip to the catalog database


# We don't return error even though activating updatable accounts/ip failed so that we don't interrupt slaves' recovery.
# If exit code is 0 or 10, MHA does not abort
    my $exit_code = 10;
    eval {
      my $new_master_handler = new MHA::DBHelper();


      # args: hostname, port, user, password, raise_error_or_not
      $new_master_handler->connect( $new_master_ip, $new_master_port,
        $new_master_user, $new_master_password, 1 );


      ## Set read_only=0 on the new master
      $new_master_handler->disable_log_bin_local();
      print current_time_us() . " Set read_only=0 on the new master.\n";
      $new_master_handler->disable_read_only();


      ## Creating an app user on the new master
      #print current_time_us() . " Creating app user on the new master..\n";
      print current_time_us() . "Add vip $vip on $if..\n";
     # create_app_user($new_master_handler);
      &add_vip();
      $new_master_handler->enable_log_bin_local();
      $new_master_handler->disconnect();


      ## Update master ip on the catalog database, etc
      $exit_code = 0;
    };
    if ($@) {
      warn "Got Error: $@\n";
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "status" ) {


    # do nothing
    exit 0;
  }
  else {
    &usage();
    exit 1;
  }
}


sub usage {
  print
"Usage: master_ip_online_change --command=start|stop|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";
  die;
}
=================================================
#创建完脚本后,给执行权限
chmod 755 /etc/masterha/*


五.启动mha
启动前先检查ssh是否成功
masterha_check_ssh --global_conf=/etc/masterha/masterha_default.conf --conf=/etc/masterha/app1.conf 
出现如下提示则代表成功
Wed Jan 20 20:20:31 2016 - [info] All SSH connection tests passed successfully.


检查复制状态
masterha_check_repl --global_conf=/etc/masterha/masterha_default.conf --conf=/etc/masterha/app1.conf 
出现如下提示则代表成功
MySQL Replication Health is OK.


启动masterha manager
nohup masterha_manager --global_conf=/etc/masterha/masterha_default.conf --conf=/etc/masterha/app1.conf >/tmp/mha.log 2>&1 &
这时可以做故障切换了,这里就不再描述了,可以让主库down,也可以直接关机


六.日常维护命令
1.检查当前masterha的状态:
masterha_check_status --global_conf=/etc/masterha/masterha_default.conf --conf=/etc/masterha/app1.conf       
app1 (pid:19524) is running(0:PING_OK), master:192.168.199.126
2.停止masterha
masterha_stop --global_conf=/etc/masterha/masterha_default.conf --conf=/etc/masterha/app1.conf
Stopped app1 successfully.
3.failover后下次重启

每次failover切换后会在管理目录下生成文件app1.failover.complete,如有有这个文件,再次启动manager会不成功,需要手动删除该文件,也可以在启动时加上参数--ignore_last_failover

4.手动切换,适用于masterha_manager没有启动,主库挂掉,可以通过以下命令切换过来
masterha_master_switch --global_conf=/etc/masterha/masterha_default.conf --conf=/etc/masterha/app1.conf --dead_master_host=192.168.199.127 --dead_master_port=3316 --master_state=dead --new_master_host=192.168.199.126 --new_master_port=3316 --igonre_last_failover

5.手动在线切换,需要关闭masterha_manager
masterha_master_switch --global_conf=/etc/masterha/masterha_default.conf --conf=/etc/masterha/app1.conf --master_state=alive --new_master_host=192.168.199.127 -orig_master_is_new_slave --new_master_port=3316
Wed Jan 20 20:35:41 2016 - [info] Switching master to 192.168.199.127(192.168.199.127:3316) completed successfully.
也可以加上--running_updates_limit=1000 切换时候选master如果有延迟的话,mha切换不成功,加上此参数表示延迟此时间范围内都可切换,单位为s,但是切换的时间长短由recover时relay日志的大小决定。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值