【环境介绍】

系统环境:Red Hat Enterprise Linux 7 + 5.7.18 + MHA version 0.57

系统

IP

主机名

备注

版本

xx系统

192.168.142.111

mysqlmha1

主库

5.7.18 -log MySQL Community Server (GPL)

192.168.142.112

mysqlmha2

备库(预主库)

192.168.142.113

mysqlmha3

备库&MHA MGM

192.168.142.111

mysqlmha1

VIP

【搭建步骤:软件部署】

主机操作配置hosts文件

192.168.142.111    mysqlmha1
192.168.142.112    mysqlmha2
192.168.142.113    mysqlmha3
192.168.142.113    mysqlmha3 ---如果有管理节点,添加即可

用户目录创建

#groupadd mysql

#useradd mysql -g mysql

#mkdir /home/mysql/logs //创建日志目录

#mkdir /home/mysql/tmp //创建pid目录

将其置于/var/lib下并解压安装包

#tar -xvf mysql-5.7.18-linux-glibc2.12-x86_64.tar.gz

#mv mysql-5.7.20-linux-glibc2.12-x86_64  mysql //改名为mysql目录

#mkdir -p /var/lib/mysql/data //创建数据目录

#chown -R mysql:mysql  /var/lib/mysql/data /home/mysql/tmp  /home/mysql/logs //修改权限

初始化mysql数据库

# cd /var/lib/mysql

# ./bin/mysqld --initialize --user=mysql --basedir=/var/lib/mysql --datadir=/var/lib/mysql /data --innodb_undo_tablespaces=3 --explicit_defaults_for_timestamp

创建配置文件,注意文件目录及server_id,其他参数可根据具体情况调整

cat >/etc/mymha.cnf
#For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql/data
socket=/var/lib/mysql/data/mysql01.sock
port = 3306
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/home/mysql/logs/mysql01.err
pid-file=/var/lib/mysql/data/mysqldb01.pid

server-id = 111
basedir=/var/lib/mysql
explicit_defaults_for_timestamp

default_storage_engine=InnoDB
default_tmp_storage_engine=InnoDB
character_set_server=utf8
user=mysql
log_timestamps=system
sync_binlog=1

innodb_flush_log_at_trx_commit=1
innodb_buffer_pool_size=1G
innodb_thread_concurrency=32
innodb_flush_method=O_DIRECT
innodb_io_capacity=200
innodb_file_per_table=1

innodb_undo_tablespaces=3

max_connections=1000
max_user_connections=100
long_query_time=1
lower_case_table_names=1
slow_query_log=1
slow_query_log_file=/home/mysql/logs/slow01.log
tmpdir=/home/mysql/tmp
wait_timeout=300
thread_cache_size=100
expire_logs_days=30

#binlog set
log-bin=/home/mysql/logs/binlog01
relay-log=/home/mysql/logs/relaylog01
binlog-format = ROW
gtid-mode = ON
enforce-gtid-consistency = ON
log-slave-updates = ON
master-info-repository = TABLE
relay-log-info-repository = TABLE
binlog-checksum = NONE

slave-parallel-workers=4
slave-preserve-commit-order=1
slave-parallel-type=LOGICAL_CLOCK

设置开机启动并启动mysql服务

#cp /var/lib/mysql/support_files/mysql.server /etc/init.d/mysql

#service mysql start

#service mysql status

配置环境变量并加载环境变量

#vi /etc/profile

PATH=/var/lib/mysql/bin:$PATH

#source /etc/profile

或者直接使用mysql启动指定配置文件,一台主机多个mysql实例时可以使用这种方式启动,我这里使用以下方式启动

#mysqld --defaults-file=/etc/mymha.cnf &

连接数据库

#mysql -uroot -p -P3306 --protocol=tcp

关闭数据库

#mysqladmin --defaults-file=/etc/mymha.cnf --protocol=tcp -P3306 shutdown -uroot -pmysql

查看数据库初始化密码

#cat /home/mysql/logs/mysql.err|grep -i 'temporary password'

使用root用户进入数据库后修改密码

Mysql>set password = password('mysql');

其他主机安装mysql步骤跟上面一致,注意修改/etc/my.cnf 中的server_id参数

其他备库或者直接使用拷贝的方法安装mysql

#scp -r /var/lib/mysql 192.168.142.xxx: /var/lib/

#chown -R mysql:mysql /var/lib/mysql

#vi /etc/my.cnf

server_id = xxx

#cd /var/lib/mysql/data

#rm auto.cnf

初始化mysql数据库

#./bin/mysqld --initialize --user=mysql --basedir=/var/lib/mysql --datadir=/var/lib/mysql /data --innodb_undo_tablespaces=3 --explicit_defaults_for_timestamp

使用root用户进入数据库后修改密码

#cat /home/mysql/logs/mysql.err|grep -i 'temporary password'

Mysql>set password = password('mysql');

执行以上后完成mysql安装

【搭建步骤:主从配置】

所有节点创建同步用户repl,ip%表示范围(1对多),密码为repl

mysql>create user repl@'192.168.142.%' IDENTIFIED BY 'repl';

赋予该用户同步所有数据库的表的权限

mysql>GRANT REPLICATION SLAVE ON *.* TO repl@'192.168.142.%';

mysql>flush privileges;

两个备库节点执行

mysql>change master to master_user= 'repl',master_host='192.168.142.111',master_password='repl',master_port=3306,MASTER_AUTO_POSITION=1;

启动从库服务

mysql> slave start;

查看主从复制状态

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.142.111
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog01.000011
          Read_Master_Log_Pos: 529
               Relay_Log_File: relaylog01.000002
                Relay_Log_Pos: 395
        Relay_Master_Log_File: binlog01.000011
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

    ......

Executed_Gtid_Set: 42f239e7-5908-11e8-8214-000c2926d694:1,
4651522f-5908-11e8-807d-000c293193c4:1,
8d7abed9-d4cd-11e7-a165-000c29c913a2:1-7,
aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:1-15:1000002-1000075
                Auto_Position: 1
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)

查看Slave_IO_Running和Slave_SQL_Running运行正常则完成主从复制搭建

【配置MHA高可用】

主库手工配置VIP

sudo /sbin/ifconfig eno16777736:2 192.168.142.114 netmask 255.255.255.0 //注意添加掩码,会引起业务无法连接

所有节点安装软件包,上传mha4mysql-node-0.57-0.el7.noarch.rpm

http://bbs.51cto.com/thread-955386-1.html

#yum install perl-DBD-MySQL -y

#yum install mha4mysql-node-0.57-0.el7.noarch.rpm

管理节点安装MHA管理软件(192.168.142.113)

先行安装相关的perl依赖包:

perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm

perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm

perl-DBD-MySQL-4.023-5.el7.x86_64.rpm

perl-IO-Compress-2.061-2.el7.noarch.rpm

perl-Net-Daemon-0.48-5.el7.noarch.rpm

perl-PlRPC-0.2020-14.el7.noarch.rpm

perl-DBI-1.627-4.el7.x86_64.rpm

perl-Class-Load-0.20-3.el7.noarch.rpm

perl-Config-Tiny-2.14-7.el7.noarch.rpm

perl-Data-OptList-0.107-9.el7.noarch.rpm

perl-IO-Socket-IP-0.21-4.el7.noarch.rpm

perl-IO-Socket-SSL-1.94-6.el7.noarch.rpm

perl-List-MoreUtils-0.33-9.el7.x86_64.rpm

perl-Email-Date-Format-1.002-15.el7.noarch.rpm

perl-Log-Dispatch-2.41-1.el7.1.noarch.rpm

perl-MIME-Lite-3.030-1.el7.noarch.rpm

perl-MIME-Types-1.38-2.el7.noarch.rpm

perl-Mail-Sender-0.8.23-1.el7.noarch.rpm

perl-Mail-Sendmail-0.79-21.el7.noarch.rpm

perl-MailTools-2.12-2.el7.noarch.rpm

perl-Module-Runtime-0.013-4.el7.noarch.rpm

perl-Module-Implementation-0.06-6.el7.noarch.rpm

perl-Net-LibIDN-0.12-15.el7.x86_64.rpm

perl-Net-SMTP-SSL-1.01-13.el7.noarch.rpm

perl-Package-DeprecationManager-0.13-7.el7.noarch.rpm

perl-Package-Stash-0.34-2.el7.noarch.rpm

perl-Package-Stash-XS-0.26-3.el7.x86_64.rpm

perl-Net-SSLeay-1.55-6.el7.x86_64.rpm

perl-Parallel-ForkManager-1.18-2.el7.noarch.rpm

perl-Params-Util-1.07-6.el7.x86_64.rpm

perl-Params-Validate-1.08-4.el7.x86_64.rpm

perl-Sub-Install-0.926-6.el7.noarch.rpm

perl-Sys-Syslog-0.33-3.el7.x86_64.rpm

perl-TimeDate-2.30-2.el7.noarch.rpm

perl-Try-Tiny-0.12-2.el7.noarch.rpm

#yum install mha4mysql-manager-0.57-0.el7.noarch.rpm

所有节点配置SSH互信

创建主机MHA用户及,所有节点执行配置

#useradd mha

#passwd mha //密码为mha

赋予mha用户有sudo权限,一定要执行以下操作,否则mha用户会没有权限进行修改相关主机操作

#echo "mha    ALL=(ALL)   NOPASSWD: ALL" >> /etc/sudoers

#sudo sed -i 's/Defaults    requiretty/#Defaults    requiretty/g' /etc/sudoers

#sudo cat /etc/sudoers | grep requiretty

#su - mha

$ssh-keygen -t rsa

$ssh-copy-id -i ~/.ssh/id_rsa.pub mha@192.168.142.111

$ssh-copy-id -i ~/.ssh/id_rsa.pub mha@192.168.142.112

$ssh-copy-id -i ~/.ssh/id_rsa.pub mha@192.168.142.113

主节点创建mha用户

mysql>GRANT ALL PRIVILEGES ON *.* TO 'mha'@'192.168.142.%' IDENTIFIED BY 'Mha_ahm%0118'; //注意密码变动需要修改后面的配置文件mha用户密码

创建MHA切换脚本和配置文件

#mkdir -p /etc/masterha

#mkdir -p /var/log/masterha/app1/

#chown -R mha:mha /var/log/masterha/app1/

#cd /etc/masterha

[root@mysqlmha3 masterha]# cat app1.cnf
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_ip_failover_script="/usr/bin/master_ip_failover"
master_ip_online_change_script="/usr/bin/master_ip_online_change"
ping_interval=5
remote_workdir=/tmp
repl_password=repl  ###数据库用户信息,如果用户密码修改,则修改相应的用户及密码
repl_user=repl
secondary_check_script="/usr/bin/masterha_secondary_check -s 192.168.142.112 -s 192.168.142.113  --user=root --master_host=mysqlmha1 --master_ip=192.168.142.111 --master_port=3306"
ssh_user=mha    ###主机用户,如果用户密码修改,则修改相应的用户及密码
user=mha
password=Mha_ahm%0118

[server1]  ###主库信息
hostname=192.168.142.111
master_binlog_dir="/mysql/mysql57/logs"
port=3306

[server2]  ###预备主库信息
hostname=192.168.142.112
master_binlog_dir="/mysql/mysql57/logs"
port=3306
candidate_master=1
check_repl_delay=0

[server3]  ###备库信息
hostname=192.168.142.113
master_binlog_dir="/mysql/mysql57/logs"
port=3306
no_master=1
[root@mysqlmha3 masterha]#

上传配置文件

cd /usr/bin

masterha_secondary_check

master_ip_online_change

master_ip_failover

[root@mysqlmha3 bin]# cat >masterha_secondary_check
#!/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

use strict;
use warnings FATAL => 'all';

use English qw(-no_match_vars);
use Getopt::Long;
use Pod::Usage;
use MHA::ManagerConst;

my @monitoring_servers;
my (
  $help,        $version,         $ssh_user,  $ssh_port,
  $ssh_options, $master_host,     $master_ip, $master_port,
  $master_user, $master_password, $ping_type
);
my $timeout = 5;

$| = 1;
GetOptions(
  'help'              => \$help,
  'version'           => \$version,
  'secondary_host=s'  => \@monitoring_servers,
  'user=s'            => \$ssh_user,
  'port=s'            => \$ssh_port,
  'options=s'         => \$ssh_options,
  'master_host=s'     => \$master_host,
  'master_ip=s'       => \$master_ip,
  'master_port=i'     => \$master_port,
  'master_user=s'     => \$master_user,
  'master_password=s' => \$master_password,
  'ping_type=s'       => \$ping_type,
  'timeout=i'         => \$timeout,
);

if ($version) {
  print "masterha_secondary_check version $MHA::ManagerConst::VERSION.\n";
  exit 0;
}

if ($help) {
  pod2usage(0);
}

unless ($master_host) {
  pod2usage(1);
}

sub exit_by_signal {
  exit 1;
}
local $SIG{INT} = $SIG{HUP} = $SIG{QUIT} = $SIG{TERM} = \&exit_by_signal;

$ssh_user    = "mha" unless ($ssh_user);
$ssh_port    = 22     unless ($ssh_port);
$master_port = 3306   unless ($master_port);

if ($ssh_options) {
  $MHA::ManagerConst::SSH_OPT_CHECK = $ssh_options;
}
$MHA::ManagerConst::SSH_OPT_CHECK =~ s/VAR_CONNECT_TIMEOUT/$timeout/;

# 0: master is not reachable from all monotoring servers
# 1: unknown errors
# 2: at least one of monitoring servers is not reachable from this script
# 3: master is reachable from at least one of monitoring servers
my $exit_code = 0;

foreach my $monitoring_server (@monitoring_servers) {
  my $ssh_user_host = $ssh_user . '@' . $monitoring_server;
  my $command =
"ssh $MHA::ManagerConst::SSH_OPT_CHECK -p $ssh_port $ssh_user_host \"perl -e "
    . "\\\"use IO::Socket::INET; my \\\\\\\$sock = IO::Socket::INET->new"
    . "(PeerAddr => \\\\\\\"$master_host\\\\\\\", PeerPort=> $master_port, "
    . "Proto =>'tcp', Timeout => $timeout); if(\\\\\\\$sock) { close(\\\\\\\$sock); "
    . "exit 3; } exit 0;\\\" \"";
  my $ret = system($command);
  $ret = $ret >> 8;
  if ( $ret == 0 ) {
    print
"Monitoring server $monitoring_server is reachable, Master is not reachable from $monitoring_server. OK.\n";
    next;
  }
  if ( $ret == 3 ) {
    if ( defined $ping_type
      && $ping_type eq $MHA::ManagerConst::PING_TYPE_INSERT )
    {
      my $ret_insert;
      my $command_insert =
          "ssh $MHA::ManagerConst::SSH_OPT_CHECK -p $ssh_port $ssh_user_host \'"
        . "/usr/bin/mysql -u$master_user -p$master_password -h$master_host "
        . "-e \"CREATE DATABASE IF NOT EXISTS infra; "
        . "CREATE TABLE IF NOT EXISTS infra.chk_masterha (\\`key\\` tinyint NOT NULL primary key,\\`val\\` int(10) unsigned NOT NULL DEFAULT '0'\) engine=MyISAM; "
        . "INSERT INTO infra.chk_masterha values (1,unix_timestamp()) ON DUPLICATE KEY UPDATE val=unix_timestamp()\"\'";
      my $sigalrm_timeout = 3;
      eval {
        local $SIG{ALRM} = sub {
          die "timeout.\n";
        };
        alarm $sigalrm_timeout;
        $ret_insert = system($command_insert);
        $ret_insert = $ret_insert >> 8;
        alarm 0;
      };
      if ( $@ || $ret_insert != 0 ) {
        print
"Monitoring server $monitoring_server is reachable, Master is not writable from $monitoring_server. OK.\n";
        next;
      }
    }
    print "Master is reachable from $monitoring_server!\n";
    $exit_code = 3;
    last;
  }
  else {
    print "Monitoring server $monitoring_server is NOT reachable!\n";
    $exit_code = 2;
    last;
  }
}

exit $exit_code;

# ############################################################################
# Documentation
# ############################################################################

=pod

=head1 NAME

masterha_secondary_check - Checking master availability from additional network routes

=head1 SYNOPSIS

masterha_secondary_check -s secondary_host1 -s secondary_host2 .. --user=ssh_username --master_host=host --master_ip=ip --master_port=port

See online reference (http://code.google.com/p/mysql-master-ha/wiki/Parameters#secondary_check_script) for details.

=head1 DESCRIPTION

See online reference (http://code.google.com/p/mysql-master-ha/wiki/Parameters#secondary_check_script) for details.

[root@mysqlmha3 bin]#

[root@mysqlmha3 bin]# 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;
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,
);

my $vip = '192.168.142.114';  # Virtual IP
my $key = "2";
my $gateway = '192.168.142.2';
my $netmask = "255.255.255.0";
my $ssh_start_vip = "sudo /sbin/ifconfig eno16777736:$key $vip netmask $netmask;/sbin/arping -I eno16777736 -c 3 -s $vip $gateway >/dev/null 2>&1";
my $ssh_stop_vip  = "sudo /sbin/ifconfig eno16777736:$key down";
my $sshuser = "mha";
 
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 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";
      #FIXME_xxx_drop_app_user($orig_master_handler);

## 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
      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" ) {
    ## 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";
      #FIXME_xxx_create_app_user($new_master_handler);
      $new_master_handler->enable_log_bin_local();
      $new_master_handler->disconnect();
     
      print "Enabling the VIP - $vip on the new master - $new_master_host \n";
      &start_vip();

## 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;
  }
}

# A simple system call that enable the VIP on the new master
sub start_vip() {
    `ssh $sshuser\@$new_master_host \" $ssh_start_vip \"`;
}

# A simple system call that disable the VIP on the old_master
sub stop_vip() {
    `ssh $sshuser\@$orig_master_host \" $ssh_stop_vip \"`;
}

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;
}

[root@mysqlmha3 bin]#

[root@mysqlmha3 bin]# cat >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 $gateway = '192.168.142.2';
my $vip = '192.168.142.114';
my $netmask = "255.255.255.0";
my $key = "2";
my $ssh_start_vip = "sudo /sbin/ifconfig eno16777736:$key $vip netmask $netmask;/sbin/arping -I eno16777736 -c 3 -s $vip $gateway >/dev/null 2>&1";
my $ssh_stop_vip = "sudo /sbin/ifconfig eno16777736:$key down";
$ssh_user = "mha";
 
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;
        #};
 
 
        eval {
                print "Disabling the VIP on old master: $orig_master_host \n";
                #my $ping=`ping -c 1 10.0.0.13 | grep "packet loss" | awk -F',' '{print $3}' | awk '{print $1}'`;
                #if ( $ping le "90.0%" && $ping gt "0.0%" ){
                #$exit_code = 0;
                #}
                #else {
 
                &stop_vip();
 
                # 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 {
            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_ip \" $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";
}
 
# the end.

[root@mysqlmha3 bin]#

赋予权限,否则mha无法执行脚本

chmod +x master_ip_failover

chmod +x master_ip_online_change

chmod +x masterha_secondary_check

至此,已经完成MHA脚本部署

【测试脚本】

#su – mha

检查节点间的ssh互信状态是否正常,如果有报错,确认用户,用户互信及密码

$masterha_check_ssh --conf=/etc/masterha/app1.cnf

[root@mysqlmha3 bin]# su - mha
上一次登录:四 5月 17 00:47:13 CST 2018pts/6 上
[mha@mysqlmha3 ~]$ masterha_check_ssh --conf=/etc/masterha/app1.cnf
Thu May 17 04:39:51 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu May 17 04:39:51 2018 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Thu May 17 04:39:51 2018 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Thu May 17 04:39:51 2018 - [info] Starting SSH connection tests..
Thu May 17 04:39:51 2018 - [debug]
Thu May 17 04:39:51 2018 - [debug]  Connecting via SSH from mha@192.168.142.111(192.168.142.111:22) to mha@192.168.142.112(192.168.142.112:22)..
Thu May 17 04:39:51 2018 - [debug]   ok.
Thu May 17 04:39:51 2018 - [debug]  Connecting via SSH from mha@192.168.142.111(192.168.142.111:22) to mha@192.168.142.113(192.168.142.113:22)..
Thu May 17 04:39:51 2018 - [debug]   ok.
Thu May 17 04:39:52 2018 - [debug]
Thu May 17 04:39:51 2018 - [debug]  Connecting via SSH from mha@192.168.142.112(192.168.142.112:22) to mha@192.168.142.111(192.168.142.111:22)..
Thu May 17 04:39:52 2018 - [debug]   ok.
Thu May 17 04:39:52 2018 - [debug]  Connecting via SSH from mha@192.168.142.112(192.168.142.112:22) to mha@192.168.142.113(192.168.142.113:22)..
Thu May 17 04:39:52 2018 - [debug]   ok.
Thu May 17 04:39:52 2018 - [debug]
Thu May 17 04:39:52 2018 - [debug]  Connecting via SSH from mha@192.168.142.113(192.168.142.113:22) to mha@192.168.142.111(192.168.142.111:22)..
Thu May 17 04:39:52 2018 - [debug]   ok.
Thu May 17 04:39:52 2018 - [debug]  Connecting via SSH from mha@192.168.142.113(192.168.142.113:22) to mha@192.168.142.112(192.168.142.112:22)..
Thu May 17 04:39:52 2018 - [debug]   ok.
Thu May 17 04:39:52 2018 - [info] All SSH connection tests passed successfully.
[mha@mysqlmha3 ~]$

检查mysql主从复制是否正常,如果有报错,确认脚本文件权限是否准确,配置文件信息是否准确

$masterha_check_repl --conf=/etc/masterha/app1.cnf

[mha@mysqlmha3 ~]$ masterha_check_repl --conf=/etc/masterha/app1.cnf
Thu May 17 04:40:15 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu May 17 04:40:15 2018 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Thu May 17 04:40:15 2018 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Thu May 17 04:40:15 2018 - [info] MHA::MasterMonitor version 0.57.
Thu May 17 04:40:16 2018 - [info] GTID failover mode = 1
Thu May 17 04:40:16 2018 - [info] Dead Servers:
Thu May 17 04:40:16 2018 - [info] Alive Servers:
Thu May 17 04:40:16 2018 - [info]   192.168.142.111(192.168.142.111:3306)
Thu May 17 04:40:16 2018 - [info]   192.168.142.112(192.168.142.112:3306)
Thu May 17 04:40:16 2018 - [info]   192.168.142.113(192.168.142.113:3306)
Thu May 17 04:40:16 2018 - [info] Alive Slaves:
Thu May 17 04:40:16 2018 - [info]   192.168.142.112(192.168.142.112:3306)  Version=5.7.18-log (oldest major version between slaves) log-bin:enabled
Thu May 17 04:40:16 2018 - [info]     GTID ON
Thu May 17 04:40:16 2018 - [info]     Replicating from 192.168.142.111(192.168.142.111:3306)
Thu May 17 04:40:16 2018 - [info]     Primary candidate for the new Master (candidate_master is set)
Thu May 17 04:40:16 2018 - [info]   192.168.142.113(192.168.142.113:3306)  Version=5.7.18-log (oldest major version between slaves) log-bin:enabled
Thu May 17 04:40:16 2018 - [info]     GTID ON
Thu May 17 04:40:16 2018 - [info]     Replicating from 192.168.142.111(192.168.142.111:3306)
Thu May 17 04:40:16 2018 - [info]     Not candidate for the new Master (no_master is set)
Thu May 17 04:40:16 2018 - [info] Current Alive Master: 192.168.142.111(192.168.142.111:3306)
Thu May 17 04:40:16 2018 - [info] Checking slave configurations..
Thu May 17 04:40:16 2018 - [info]  read_only=1 is not set on slave 192.168.142.113(192.168.142.113:3306).
Thu May 17 04:40:16 2018 - [info] Checking replication filtering settings..
Thu May 17 04:40:16 2018 - [info]  binlog_do_db= , binlog_ignore_db=
Thu May 17 04:40:16 2018 - [info]  Replication filtering check ok.
Thu May 17 04:40:16 2018 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Thu May 17 04:40:16 2018 - [info] Checking SSH publickey authentication settings on the current master..
Thu May 17 04:40:16 2018 - [info] HealthCheck: SSH to 192.168.142.111 is reachable.
Thu May 17 04:40:16 2018 - [info]
192.168.142.111(192.168.142.111:3306) (current master)
 +--192.168.142.112(192.168.142.112:3306)
 +--192.168.142.113(192.168.142.113:3306)

Thu May 17 04:40:16 2018 - [info] Checking replication health on 192.168.142.112..
Thu May 17 04:40:16 2018 - [info]  ok.
Thu May 17 04:40:16 2018 - [info] Checking replication health on 192.168.142.113..
Thu May 17 04:40:16 2018 - [info]  ok.
Thu May 17 04:40:16 2018 - [info] Checking master_ip_failover_script status:
Thu May 17 04:40:16 2018 - [info]   /usr/bin/master_ip_failover --command=status --ssh_user=mha --orig_master_host=192.168.142.111 --orig_master_ip=192.168.142.111 --orig_master_port=3306

IN SCRIPT TEST====sudo /sbin/ifconfig eno16777736:2 down==sudo /sbin/ifconfig eno16777736:2 192.168.142.114 netmask 255.255.255.0;/sbin/arping -I eno16777736 -c 3 -s 192.168.142.114 192.168.142.2 >/dev/null 2>&1===

Checking the Status of the script.. OK
Thu May 17 04:40:19 2018 - [info]  OK.
Thu May 17 04:40:19 2018 - [warning] shutdown_script is not defined.
Thu May 17 04:40:19 2018 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.
[mha@mysqlmha3 ~]$

以上脚本运行输出正常则MHA部署完成,容易出错部分已经标红,注意IP信息准确。

【高可用测试】

由于输出信息较多,请查看mysql MHA架构高可用测试章节。

mysql MHA架构搭建过程的更多相关文章

  1. CentOS6.8下MySQL MHA架构搭建笔记

    转载请注明出处,本文地址:http://www.cnblogs.com/ajiangg/p/6552855.html 以下是CentOS6.8下MySQL MHA架构搭建笔记 IP资源规划: 192. ...

  2. CENTOS6.6 下mysql MHA架构搭建

    本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 摘要: 本篇是自己搭建的一篇mysql MHA文章 前面的安装步骤基 ...

  3. MySQL MHA环境搭建

    MHA功能: 1,从故障的mysql保存二进制日志时间(binlog events);2,识别含有最新更新的slave:3,应用差异的中继日志(relay log)到其他的slave:4,应用从mas ...

  4. 【Mysql】Mysql主从库搭建过程(爬完坑后整理所得)

    Mysql主从数据库搭建流程 新手开始学习mysql主从库,遇到一些问题,总结后写出以下流程 下面以5.7.23版本为例介绍 第一步:去官网下载5.7.23版本的免安装压缩包形式的mysql文件,贴上 ...

  5. CENTOS6.6下mysql MMM架构搭建

    本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn MMM(Master-Master replication mana ...

  6. MHA架构搭建中遇到的问题

    1. 两个包:mha4mysql-manager-0.56-0.el6.noarch.rpm 和 mha4mysql-node-0.56-0.el6.norch.rpm 地址:https://code ...

  7. mysql主从架构搭建

    1.配置文件,开启二进制日志 vim /etc/my.cnf 在mysql下增加如下内容 server-id= log-bin=mysql-bin relay-log=mysql-relay 2.登录 ...

  8. MySQL高可用架构之基于MHA的搭建

    一.MySQL MHA架构介绍: MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Fa ...

  9. MySQL高可用之MHA的搭建 转

     http://www.cnblogs.com/muhu/p/4045780.html http://www.cnblogs.com/gomysql/p/3675429.html http://www ...

随机推荐

  1. 三数之和的golang实现

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

  2. 【Python 14】分形树绘制2.0(重复五角星+Turtle库文档)

    1.案例描述 加入循环操作绘制重复不同大小的图形 2.案例分析 3.turtle库补充 # 画笔控制函数 turtle.penup() # 抬起画笔,之后移动画笔不绘制图形 turtle.pendow ...

  3. .NET CORE学习笔记系列(2)——依赖注入【1】控制反转IOC

    原文:https://www.cnblogs.com/artech/p/net-core-di-01.html 一.流程控制的反转 IoC的全名Inverse of Control,翻译成中文就是“控 ...

  4. Flink流处理的时间窗口

    Flink流处理的时间窗口 对于流处理系统来说,流入的消息是无限的,所以对于聚合或是连接等操作,流处理系统需要对流入的消息进行分段,然后基于每一段数据进行聚合或是连接等操作. 消息的分段即称为窗口,流 ...

  5. 异步渲染页面怎么点击checkbox获取value值

    前后端分离时 后端向前端传递json数据  前端根据需要进行页面渲染 因为是异步渲染 想要获取获取渲染数据里面的值时获取不到的 介绍两个方法: 1,设置全局变量 即渲染时在html页面设置全局变量 如 ...

  6. .net core EF的简单使用

    1.在mysql中新建一个表 2.在控制台中装个EF包  Install-Package Pomelo.EntityFrameworkCore.MySql 3.新建一个Person类 4.创建DbCo ...

  7. wget命令使用报错 certificate common name 'xxx' doesn't match requestde host name

    使用wget命令 wget http://www.monkey.org/~provos/libevent-1.2.tar.gz 报如下错 error:certificate common name & ...

  8. 使用css画一个箭头

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  9. 原生js设置rem

    使用rem是为了界面响应不同尺寸的手机,引入下面的方法就可以使用rem了. setFontSize: function (doc, win) { var docEl = doc.documentEle ...

  10. JDBC连接池之C3P0

    1.导入jar包 c3p0-0.9.1.jar mchange-commons-java-0.2.3.4(注:该jar包是c3p0数据库连接池的辅助包,没有这个包系统启动的时候会报classnotfo ...