OS:CentOS Linux release 7.6.1810 (Core)
db: 5.7.30 MySQL Community Server (GPL)
ip | hostname | ||
172.168.1.178 | mysqldb1 | master | Node |
172.168.1.179 | mysqldb2 | slave1 | Node |
172.168.1.180 | mysqldb3 | slave2 | Node Manager
|
172.168.1.188 | vip |
基于GTID的半同步复制,一主两从
[root@mysqldb1 ~]# cat /etc/my.cnf
[client]
port = 3306
socket = /data/mysqldata/mysql.sock
default-character-set=utf8mb4
[mysql]
prompt="(\\u@\\h_\\d) [\\R:\\m]> "
[mysqld]
server_id=1
datadir=/data/mysqldata
socket=/data/mysqldata/mysql.sock
gtid_mode=on
enforce_gtid_consistency=on
log_bin=on
binlog_format=row
symbolic-links=0
[mysqld_safe]
log-error=/data/mysqldata/error.log
pid-file=/data/mysqldata/mysql.pid
!includedir /etc/my.cnf.d
[root@mysqldb2 ~]# cat /etc/my.cnf
[client]
port = 3306
socket = /data/mysqldata/mysql.sock
default-character-set=utf8mb4
[mysql]
prompt="(\\u@\\h_\\d) [\\R:\\m]> "
[mysqld]
server_id=2
datadir=/data/mysqldata
socket=/data/mysqldata/mysql.sock
log_bin=on
binlog_format=row
gtid_mode=on
enforce_gtid_consistency=on
log_slave_updates=1
symbolic-links=0
[mysqld_safe]
log-error=/data/mysqldata/error.log
pid-file=/data/mysqldata/mysql.pid
创建管理用户与同步用户
create user 'zs'@'%' identified by'123456';
grant all privileges on *.* to 'zs'@'%';
flush privileges;
create user 'repl'@'%' identified by'repl';
grant replication slave on *.* to 'repl'@'%';
flush privileges;
mysqldb1 全备
/usr/local/mysql/bin/mysqldump --single-transaction -uroot -pmysql -A >all.sql
scp all.sql root@mysqldb2:/root/
scp all.sql root@mysqldb3:/root/
mysqldb2 mysqldb3
>mysql -uroot -pmysql
>source all.sql
change master to master_host='172.168.1.178',master_user='repl',master_password='repl',master_auto_position=1;
start slave;
[root@mysqldb1 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.168.1.178 mysqldb1
172.168.1.179 mysqldb2
172.168.1.180 mysqldb3
每个机器上执行
ssh-keygen -t dsa -P '' -f id_dsa
cat id_dsa.pub >> authorized_keys
scp 172.168.1.179:/root/.ssh/id_dsa.pub ./id_dsa.pub.179
scp 172.168.1.180:/root/.ssh/id_dsa.pub ./id_dsa.pub.180
cat id_dsa.pub.179 >> authorized_keys
cat id_dsa.pub.180 >> authorized_keys
scp authorized_keys 172.168.1.179:/root/.ssh
scp authorized_keys 172.168.1.180:/root/.ssh
测试ssh
ssh root@mysqldb2
ssh root@mysqldb3
ssh root@mysqldb1
yum源配置
[root@mysqldb1 yum.repos.d]# cat epel.repo
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
#baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch/debug
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1
[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
#baseurl=http://download.fedoraproject.org/pub/epel/7/SRPMS
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-source-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1
安装相关包
yum install -y lrzsz perl-DBD-MySQL perl-Config-Tiny* erl-Time-HiRes* erl-Params-Validate* perl-Log-Dispatch* erl-Parallel-ForkManager* perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker cpan perl-CPAN*
unzip mha4mysql-node-master.zip
perl Makefile.PL
make && make install
unzip mha4mysql-manager-master.zip
perl Makefile.PL
make
make install
(过程比较长,耐心等待)
mkdir -p /usr/local/mha
mkdir -p /etc/mha
mkdir -p /usr/local/scripts
[root@mysqldb3 mha]# cat /etc/mha/mha.conf
[server default]
user=zs
password=123456
manager_workdir=/usr/local/mha
manager_log=/usr/local/mha/manager.log
remote_workdir=/usr/local/mha
ssh_user=root
repl_user=repl
repl_password=repl
ping_interval=1
master_ip_failover_script=/usr/local/scripts/master_ip_failover
master_ip_online_change_script=/usr/local/scripts/master_ip_online_change
[server1]
hostname=172.168.1.178
port=3306
ssh_port=22
master_binlog_dir=/data/mysqldata
candidate_master=1
[server2]
hostname=172.168.1.179
port=3306
ssh_port=22
master_binlog_dir=/data/mysqldata
candidate_master=1
[server3]
hostname=172.168.1.180
port=3306
ssh_port=22
master_binlog_dir=/data/mysqldata
candidate_master=1
[root@mysqldb3 scripts]# cat /usr/local/scripts/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;
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
);
my $vip = '172.168.1.188/24';
my $key = '0';
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$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,
'new_master_user=s' => \$new_master_user,
'new_master_password=s' => \$new_master_password,
);
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\@$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\n";
}
[root@mysqldb3 scripts]# cat /usr/local/scripts/master_ip_online_change
#!/usr/bin/env perl
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 = '172.168.1.188';
my $brdc = '172.168.1.255';
my $ifdev = 'ens33';
my $key = '1';
my $ssh_start_vip = "/usr/sbin/ip addr add $vip/24 brd $brdc dev $ifdev label $ifdev:$key;/usr/sbin/arping -q -A -c 1 -I $ifdev $vip;iptables -F;";
my $ssh_stop_vip = "/usr/sbin/ip addr del $vip/24 dev $ifdev label $ifdev:$key";
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} );
}
print "Disabling the VIP on old master: $orig_master_host \n";
&stop_vip();
## 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";
#FIXME_xxx_create_app_user($new_master_handler);
#$new_master_handler->enable_log_bin_local();
$new_master_handler->disconnect();
## Update master ip on the catalog database, etc
print "Enabling the VIP - $vip on the new master - $new_master_host \n";
&start_vip();
$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 $new_master_ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
`ssh $orig_master_ssh_user\@$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 --orig_master_user=user --orig_master_password=password --orig_master_ssh_user=sshuser --new_master_host=host --new_master_ip=ip --new_master_port=port --new_master_user=user --new_master_password=password --new_master_ssh_user=sshuser \n";
die;
}
chmod +x *
[root@mysqldb3 scripts]# ll
total 16
-rw-r--r--. 1 root root 0 Jul 20 22:07 all
-rwxr-xr-x. 1 root root 3766 Jul 20 22:18 master_ip_failover
-rwxr-xr-x. 1 root root 10042 Jul 20 22:52 master_ip_online_change
[root@mysqldb3 scripts]# masterha_check_ssh --conf=/etc/mha/mha.conf
Tue Jul 21 09:55:10 2020 - [info] Reading default configuration from /etc/masterha_default.cnf..
Tue Jul 21 09:55:10 2020 - [info] Reading application default configuration from /etc/mha/mha.conf..
Tue Jul 21 09:55:10 2020 - [info] Reading server configuration from /etc/mha/mha.conf..
Tue Jul 21 09:55:10 2020 - [info] Starting SSH connection tests..
Tue Jul 21 09:55:12 2020 - [debug]
Tue Jul 21 09:55:10 2020 - [debug] Connecting via SSH from [email protected](172.168.1.179:22) to [email protected](172.168.1.178:22)..
Tue Jul 21 09:55:11 2020 - [debug] ok.
Tue Jul 21 09:55:11 2020 - [debug] Connecting via SSH from [email protected](172.168.1.179:22) to [email protected](172.168.1.180:22)..
Tue Jul 21 09:55:12 2020 - [debug] ok.
Tue Jul 21 09:55:12 2020 - [debug]
Tue Jul 21 09:55:10 2020 - [debug] Connecting via SSH from [email protected](172.168.1.178:22) to [email protected](172.168.1.179:22)..
Tue Jul 21 09:55:11 2020 - [debug] ok.
Tue Jul 21 09:55:11 2020 - [debug] Connecting via SSH from [email protected](172.168.1.178:22) to [email protected](172.168.1.180:22)..
Tue Jul 21 09:55:12 2020 - [debug] ok.
Tue Jul 21 09:55:13 2020 - [debug]
Tue Jul 21 09:55:11 2020 - [debug] Connecting via SSH from [email protected](172.168.1.180:22) to [email protected](172.168.1.178:22)..
Tue Jul 21 09:55:12 2020 - [debug] ok.
Tue Jul 21 09:55:12 2020 - [debug] Connecting via SSH from [email protected](172.168.1.180:22) to [email protected](172.168.1.179:22)..
Tue Jul 21 09:55:13 2020 - [debug] ok.
Tue Jul 21 09:55:13 2020 - [info] All SSH connection tests passed successfully.
[root@mysqldb3 scripts]# masterha_check_repl --conf=/etc/mha/mha.conf
Tue Jul 21 10:10:30 2020 - [info] Reading default configuration from /etc/masterha_default.cnf..
Tue Jul 21 10:10:30 2020 - [info] Reading application default configuration from /etc/mha/mha.conf..
Tue Jul 21 10:10:30 2020 - [info] Reading server configuration from /etc/mha/mha.conf..
Tue Jul 21 10:10:30 2020 - [info] MHA::MasterMonitor version 0.58.
Tue Jul 21 10:10:31 2020 - [info] GTID failover mode = 1
Tue Jul 21 10:10:31 2020 - [info] Dead Servers:
Tue Jul 21 10:10:31 2020 - [info] Alive Servers:
Tue Jul 21 10:10:31 2020 - [info] 172.168.1.178(172.168.1.178:3306)
Tue Jul 21 10:10:31 2020 - [info] 172.168.1.179(172.168.1.179:3306)
Tue Jul 21 10:10:31 2020 - [info] 172.168.1.180(172.168.1.180:3306)
Tue Jul 21 10:10:31 2020 - [info] Alive Slaves:
Tue Jul 21 10:10:31 2020 - [info] 172.168.1.179(172.168.1.179:3306) Version=5.7.30-log (oldest major version between slaves) log-bin:enabled
Tue Jul 21 10:10:31 2020 - [info] GTID ON
Tue Jul 21 10:10:31 2020 - [info] Replicating from 172.168.1.178(172.168.1.178:3306)
Tue Jul 21 10:10:31 2020 - [info] Primary candidate for the new Master (candidate_master is set)
Tue Jul 21 10:10:31 2020 - [info] 172.168.1.180(172.168.1.180:3306) Version=5.7.30-log (oldest major version between slaves) log-bin:enabled
Tue Jul 21 10:10:31 2020 - [info] GTID ON
Tue Jul 21 10:10:31 2020 - [info] Replicating from 172.168.1.178(172.168.1.178:3306)
Tue Jul 21 10:10:31 2020 - [info] Primary candidate for the new Master (candidate_master is set)
Tue Jul 21 10:10:31 2020 - [info] Current Alive Master: 172.168.1.178(172.168.1.178:3306)
Tue Jul 21 10:10:31 2020 - [info] Checking slave configurations..
Tue Jul 21 10:10:31 2020 - [info] read_only=1 is not set on slave 172.168.1.179(172.168.1.179:3306).
Tue Jul 21 10:10:31 2020 - [info] Checking replication filtering settings..
Tue Jul 21 10:10:31 2020 - [info] binlog_do_db= , binlog_ignore_db=
Tue Jul 21 10:10:31 2020 - [info] Replication filtering check ok.
Tue Jul 21 10:10:31 2020 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Tue Jul 21 10:10:31 2020 - [info] Checking SSH publickey authentication settings on the current master..
Tue Jul 21 10:10:31 2020 - [info] HealthCheck: SSH to 172.168.1.178 is reachable.
Tue Jul 21 10:10:31 2020 - [info]
172.168.1.178(172.168.1.178:3306) (current master)
+--172.168.1.179(172.168.1.179:3306)
+--172.168.1.180(172.168.1.180:3306)
Tue Jul 21 10:10:31 2020 - [info] Checking replication health on 172.168.1.179..
Tue Jul 21 10:10:31 2020 - [info] ok.
Tue Jul 21 10:10:31 2020 - [info] Checking replication health on 172.168.1.180..
Tue Jul 21 10:10:31 2020 - [info] ok.
Tue Jul 21 10:10:31 2020 - [info] Checking master_ip_failover_script status:
Tue Jul 21 10:10:31 2020 - [info] /usr/local/scripts/master_ip_failover --command=status --ssh_user=root --orig_master_host=172.168.1.178 --orig_master_ip=172.168.1.178 --orig_master_port=3306
IN SCRIPT TEST====/sbin/ifconfig ens33:0 down==/sbin/ifconfig ens33:0 172.168.1.188/24===
Checking the Status of the script.. OK
Tue Jul 21 10:10:31 2020 - [info] OK.
Tue Jul 21 10:10:31 2020 - [warning] shutdown_script is not defined.
Tue Jul 21 10:10:31 2020 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
添加vip
主库172.168.1.178上执行
ip addr add 172.168.1.188 dev ens33
启动mha
[root@mysqldb3 scripts]# nohup masterha_manager --conf=/etc/mha/mha.conf >/tmp/mha_manager.log &
[1] 25616
[root@mysqldb3 scripts]# nohup: ignoring input and redirecting stderr to stdout
[root@mysqldb3 scripts]# masterha_check_status --conf=/etc/mha/mha.conf
mha (pid:25616) is running(0:PING_OK), master:172.168.1.178
[root@mysqldb1 yum.repos.d]# /etc/init.d/mysql.server stop
Shutting down MySQL............ SUCCESS!
查看mha 日志
Tue Jul 21 10:20:36 2020 - [warning] Got error on MySQL select ping: 2006 (MySQL server has gone away)
Tue Jul 21 10:20:36 2020 - [info] Executing SSH check script: exit 0
Tue Jul 21 10:20:36 2020 - [info] HealthCheck: SSH to 172.168.1.178 is reachable.
Tue Jul 21 10:20:37 2020 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '172.168.1.178' (111))
Tue Jul 21 10:20:37 2020 - [warning] Connection failed 2 time(s)..
Tue Jul 21 10:20:38 2020 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '172.168.1.178' (111))
Tue Jul 21 10:20:38 2020 - [warning] Connection failed 3 time(s)..
Tue Jul 21 10:20:39 2020 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '172.168.1.178' (111))
Tue Jul 21 10:20:39 2020 - [warning] Connection failed 4 time(s)..
Tue Jul 21 10:20:39 2020 - [warning] Master is not reachable from health checker!
Tue Jul 21 10:20:39 2020 - [warning] Master 172.168.1.178(172.168.1.178:3306) is not reachable!
Tue Jul 21 10:20:39 2020 - [warning] SSH is reachable.
Tue Jul 21 10:20:39 2020 - [info] Connecting to a master server failed. Reading configuration file /etc/masterha_default.cnf and /etc/mha/mha.conf again, and trying to connect to all servers to check server status..
Tue Jul 21 10:20:39 2020 - [info] Reading default configuration from /etc/masterha_default.cnf..
Tue Jul 21 10:20:39 2020 - [info] Reading application default configuration from /etc/mha/mha.conf..
Tue Jul 21 10:20:39 2020 - [info] Reading server configuration from /etc/mha/mha.conf..
Tue Jul 21 10:20:40 2020 - [info] GTID failover mode = 1
Tue Jul 21 10:20:40 2020 - [info] Dead Servers:
Tue Jul 21 10:20:40 2020 - [info] 172.168.1.178(172.168.1.178:3306)
Tue Jul 21 10:20:40 2020 - [info] Alive Servers:
Tue Jul 21 10:20:40 2020 - [info] 172.168.1.179(172.168.1.179:3306)
Tue Jul 21 10:20:40 2020 - [info] 172.168.1.180(172.168.1.180:3306)
Tue Jul 21 10:20:40 2020 - [info] Alive Slaves:
Tue Jul 21 10:20:40 2020 - [info] 172.168.1.179(172.168.1.179:3306) Version=5.7.30-log (oldest major version between slaves) log-bin:enabled
Tue Jul 21 10:20:40 2020 - [info] GTID ON
Tue Jul 21 10:20:40 2020 - [info] Replicating from 172.168.1.178(172.168.1.178:3306)
Tue Jul 21 10:20:40 2020 - [info] Primary candidate for the new Master (candidate_master is set)
Tue Jul 21 10:20:40 2020 - [info] 172.168.1.180(172.168.1.180:3306) Version=5.7.30-log (oldest major version between slaves) log-bin:enabled
Tue Jul 21 10:20:40 2020 - [info] GTID ON
Tue Jul 21 10:20:40 2020 - [info] Replicating from 172.168.1.178(172.168.1.178:3306)
Tue Jul 21 10:20:40 2020 - [info] Primary candidate for the new Master (candidate_master is set)
Tue Jul 21 10:20:40 2020 - [info] Checking slave configurations..
Tue Jul 21 10:20:40 2020 - [info] read_only=1 is not set on slave 172.168.1.179(172.168.1.179:3306).
Tue Jul 21 10:20:40 2020 - [info] Checking replication filtering settings..
Tue Jul 21 10:20:40 2020 - [info] Replication filtering check ok.
Tue Jul 21 10:20:40 2020 - [info] Master is down!
Tue Jul 21 10:20:40 2020 - [info] Terminating monitoring script.
Tue Jul 21 10:20:40 2020 - [info] Got exit code 20 (Master dead).
Tue Jul 21 10:20:40 2020 - [info] MHA::MasterFailover version 0.58.
Tue Jul 21 10:20:40 2020 - [info] Starting master failover.
Tue Jul 21 10:20:40 2020 - [info]
Tue Jul 21 10:20:40 2020 - [info] * Phase 1: Configuration Check Phase..
Tue Jul 21 10:20:40 2020 - [info]
Tue Jul 21 10:20:41 2020 - [info] GTID failover mode = 1
Tue Jul 21 10:20:41 2020 - [info] Dead Servers:
Tue Jul 21 10:20:41 2020 - [info] 172.168.1.178(172.168.1.178:3306)
Tue Jul 21 10:20:41 2020 - [info] Checking master reachability via MySQL(double check)...
Tue Jul 21 10:20:41 2020 - [info] ok.
Tue Jul 21 10:20:41 2020 - [info] Alive Servers:
Tue Jul 21 10:20:41 2020 - [info] 172.168.1.179(172.168.1.179:3306)
Tue Jul 21 10:20:41 2020 - [info] 172.168.1.180(172.168.1.180:3306)
Tue Jul 21 10:20:41 2020 - [info] Alive Slaves:
Tue Jul 21 10:20:41 2020 - [info] 172.168.1.179(172.168.1.179:3306) Version=5.7.30-log (oldest major version between slaves) log-bin:enabled
Tue Jul 21 10:20:41 2020 - [info] GTID ON
Tue Jul 21 10:20:41 2020 - [info] Replicating from 172.168.1.178(172.168.1.178:3306)
Tue Jul 21 10:20:41 2020 - [info] Primary candidate for the new Master (candidate_master is set)
Tue Jul 21 10:20:41 2020 - [info] 172.168.1.180(172.168.1.180:3306) Version=5.7.30-log (oldest major version between slaves) log-bin:enabled
Tue Jul 21 10:20:41 2020 - [info] GTID ON
Tue Jul 21 10:20:41 2020 - [info] Replicating from 172.168.1.178(172.168.1.178:3306)
Tue Jul 21 10:20:41 2020 - [info] Primary candidate for the new Master (candidate_master is set)
Tue Jul 21 10:20:41 2020 - [info] Starting GTID based failover.
Tue Jul 21 10:20:41 2020 - [info]
Tue Jul 21 10:20:41 2020 - [info] ** Phase 1: Configuration Check Phase completed.
Tue Jul 21 10:20:41 2020 - [info]
Tue Jul 21 10:20:41 2020 - [info] * Phase 2: Dead Master Shutdown Phase..
Tue Jul 21 10:20:41 2020 - [info]
Tue Jul 21 10:20:41 2020 - [info] Forcing shutdown so that applications never connect to the current master..
Tue Jul 21 10:20:41 2020 - [info] Executing master IP deactivation script:
Tue Jul 21 10:20:41 2020 - [info] /usr/local/scripts/master_ip_failover --orig_master_host=172.168.1.178 --orig_master_ip=172.168.1.178 --orig_master_port=3306 --command=stopssh --ssh_user=root
IN SCRIPT TEST====/sbin/ifconfig ens33:0 down==/sbin/ifconfig ens33:0 172.168.1.188/24===
Disabling the VIP on old master: 172.168.1.178
Tue Jul 21 10:20:41 2020 - [info] done.
Tue Jul 21 10:20:41 2020 - [warning] shutdown_script is not set. Skipping explicit shutting down of the dead master.
Tue Jul 21 10:20:41 2020 - [info] * Phase 2: Dead Master Shutdown Phase completed.
Tue Jul 21 10:20:41 2020 - [info]
Tue Jul 21 10:20:41 2020 - [info] * Phase 3: Master Recovery Phase..
Tue Jul 21 10:20:41 2020 - [info]
Tue Jul 21 10:20:41 2020 - [info] * Phase 3.1: Getting Latest Slaves Phase..
Tue Jul 21 10:20:41 2020 - [info]
Tue Jul 21 10:20:41 2020 - [info] The latest binary log file/position on all slaves is on.000006:194
Tue Jul 21 10:20:41 2020 - [info] Retrieved Gtid Set: b98d9078-c026-11ea-b4dd-000c294198f0:1-2
Tue Jul 21 10:20:41 2020 - [info] Latest slaves (Slaves that received relay log files to the latest):
Tue Jul 21 10:20:41 2020 - [info] 172.168.1.179(172.168.1.179:3306) Version=5.7.30-log (oldest major version between slaves) log-bin:enabled
Tue Jul 21 10:20:41 2020 - [info] GTID ON
Tue Jul 21 10:20:41 2020 - [info] Replicating from 172.168.1.178(172.168.1.178:3306)
Tue Jul 21 10:20:41 2020 - [info] Primary candidate for the new Master (candidate_master is set)
Tue Jul 21 10:20:41 2020 - [info] 172.168.1.180(172.168.1.180:3306) Version=5.7.30-log (oldest major version between slaves) log-bin:enabled
Tue Jul 21 10:20:41 2020 - [info] GTID ON
Tue Jul 21 10:20:41 2020 - [info] Replicating from 172.168.1.178(172.168.1.178:3306)
Tue Jul 21 10:20:41 2020 - [info] Primary candidate for the new Master (candidate_master is set)
Tue Jul 21 10:20:41 2020 - [info] The oldest binary log file/position on all slaves is on.000006:194
Tue Jul 21 10:20:41 2020 - [info] Retrieved Gtid Set: b98d9078-c026-11ea-b4dd-000c294198f0:1-2
Tue Jul 21 10:20:41 2020 - [info] Oldest slaves:
Tue Jul 21 10:20:41 2020 - [info] 172.168.1.179(172.168.1.179:3306) Version=5.7.30-log (oldest major version between slaves) log-bin:enabled
Tue Jul 21 10:20:41 2020 - [info] GTID ON
Tue Jul 21 10:20:41 2020 - [info] Replicating from 172.168.1.178(172.168.1.178:3306)
Tue Jul 21 10:20:41 2020 - [info] Primary candidate for the new Master (candidate_master is set)
Tue Jul 21 10:20:41 2020 - [info] 172.168.1.180(172.168.1.180:3306) Version=5.7.30-log (oldest major version between slaves) log-bin:enabled
Tue Jul 21 10:20:41 2020 - [info] GTID ON
Tue Jul 21 10:20:41 2020 - [info] Replicating from 172.168.1.178(172.168.1.178:3306)
Tue Jul 21 10:20:41 2020 - [info] Primary candidate for the new Master (candidate_master is set)
Tue Jul 21 10:20:41 2020 - [info]
Tue Jul 21 10:20:41 2020 - [info] * Phase 3.3: Determining New Master Phase..
Tue Jul 21 10:20:41 2020 - [info]
Tue Jul 21 10:20:41 2020 - [info] Searching new master from slaves..
Tue Jul 21 10:20:41 2020 - [info] Candidate masters from the configuration file:
Tue Jul 21 10:20:41 2020 - [info] 172.168.1.179(172.168.1.179:3306) Version=5.7.30-log (oldest major version between slaves) log-bin:enabled
Tue Jul 21 10:20:41 2020 - [info] GTID ON
Tue Jul 21 10:20:41 2020 - [info] Replicating from 172.168.1.178(172.168.1.178:3306)
Tue Jul 21 10:20:41 2020 - [info] Primary candidate for the new Master (candidate_master is set)
Tue Jul 21 10:20:41 2020 - [info] 172.168.1.180(172.168.1.180:3306) Version=5.7.30-log (oldest major version between slaves) log-bin:enabled
Tue Jul 21 10:20:41 2020 - [info] GTID ON
Tue Jul 21 10:20:41 2020 - [info] Replicating from 172.168.1.178(172.168.1.178:3306)
Tue Jul 21 10:20:41 2020 - [info] Primary candidate for the new Master (candidate_master is set)
Tue Jul 21 10:20:41 2020 - [info] Non-candidate masters:
Tue Jul 21 10:20:41 2020 - [info] Searching from candidate_master slaves which have received the latest relay log events..
Tue Jul 21 10:20:41 2020 - [info] New master is 172.168.1.179(172.168.1.179:3306)
Tue Jul 21 10:20:41 2020 - [info] Starting master failover..
Tue Jul 21 10:20:41 2020 - [info]
From:
172.168.1.178(172.168.1.178:3306) (current master)
+--172.168.1.179(172.168.1.179:3306)
+--172.168.1.180(172.168.1.180:3306)
To:
172.168.1.179(172.168.1.179:3306) (new master)
+--172.168.1.180(172.168.1.180:3306)
Tue Jul 21 10:20:41 2020 - [info]
Tue Jul 21 10:20:41 2020 - [info] * Phase 3.3: New Master Recovery Phase..
Tue Jul 21 10:20:41 2020 - [info]
Tue Jul 21 10:20:41 2020 - [info] Waiting all logs to be applied..
Tue Jul 21 10:20:41 2020 - [info] done.
Tue Jul 21 10:20:41 2020 - [info] Getting new master's binlog name and position..
Tue Jul 21 10:20:41 2020 - [info] on.000001:458
Tue Jul 21 10:20:41 2020 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='172.168.1.179', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='xxx';
Tue Jul 21 10:20:41 2020 - [info] Master Recovery succeeded. File:Pos:Exec_Gtid_Set: on.000001, 458, b98d9078-c026-11ea-b4dd-000c294198f0:1-2
Tue Jul 21 10:20:41 2020 - [info] Executing master IP activate script:
Tue Jul 21 10:20:41 2020 - [info] /usr/local/scripts/master_ip_failover --command=start --ssh_user=root --orig_master_host=172.168.1.178 --orig_master_ip=172.168.1.178 --orig_master_port=3306 --new_master_host=172.168.1.179 --new_master_ip=172.168.1.179 --new_master_port=3306 --new_master_user='zs' --new_master_password=xxx
IN SCRIPT TEST====/sbin/ifconfig ens33:0 down==/sbin/ifconfig ens33:0 172.168.1.188/24===
Enabling the VIP - 172.168.1.188/24 on the new master - 172.168.1.179
Tue Jul 21 10:20:42 2020 - [info] OK.
Tue Jul 21 10:20:42 2020 - [info] ** Finished master recovery successfully.
Tue Jul 21 10:20:42 2020 - [info] * Phase 3: Master Recovery Phase completed.
Tue Jul 21 10:20:42 2020 - [info]
Tue Jul 21 10:20:42 2020 - [info] * Phase 4: Slaves Recovery Phase..
Tue Jul 21 10:20:42 2020 - [info]
Tue Jul 21 10:20:42 2020 - [info]
Tue Jul 21 10:20:42 2020 - [info] * Phase 4.1: Starting Slaves in parallel..
Tue Jul 21 10:20:42 2020 - [info]
Tue Jul 21 10:20:42 2020 - [info] -- Slave recovery on host 172.168.1.180(172.168.1.180:3306) started, pid: 25855. Check tmp log /usr/local/mha/172.168.1.180_3306_20200721102040.log if it takes time..
Tue Jul 21 10:20:44 2020 - [info]
Tue Jul 21 10:20:44 2020 - [info] Log messages from 172.168.1.180 ...
Tue Jul 21 10:20:44 2020 - [info]
Tue Jul 21 10:20:42 2020 - [info] Resetting slave 172.168.1.180(172.168.1.180:3306) and starting replication from the new master 172.168.1.179(172.168.1.179:3306)..
Tue Jul 21 10:20:42 2020 - [info] Executed CHANGE MASTER.
Tue Jul 21 10:20:43 2020 - [info] Slave started.
Tue Jul 21 10:20:43 2020 - [info] gtid_wait(b98d9078-c026-11ea-b4dd-000c294198f0:1-2) completed on 172.168.1.180(172.168.1.180:3306). Executed 0 events.
Tue Jul 21 10:20:44 2020 - [info] End of log messages from 172.168.1.180.
Tue Jul 21 10:20:44 2020 - [info] -- Slave on host 172.168.1.180(172.168.1.180:3306) started.
Tue Jul 21 10:20:44 2020 - [info] All new slave servers recovered successfully.
Tue Jul 21 10:20:44 2020 - [info]
Tue Jul 21 10:20:44 2020 - [info] * Phase 5: New master cleanup phase..
Tue Jul 21 10:20:44 2020 - [info]
Tue Jul 21 10:20:44 2020 - [info] Resetting slave info on the new master..
Tue Jul 21 10:20:44 2020 - [info] 172.168.1.179: Resetting slave info succeeded.
Tue Jul 21 10:20:44 2020 - [info] Master failover to 172.168.1.179(172.168.1.179:3306) completed successfully.
Tue Jul 21 10:20:44 2020 - [info]
----- Failover Report -----
mha: MySQL Master failover 172.168.1.178(172.168.1.178:3306) to 172.168.1.179(172.168.1.179:3306) succeeded
Master 172.168.1.178(172.168.1.178:3306) is down!
Check MHA Manager logs at mysqldb3:/usr/local/mha/manager.log for details.
Started automated(non-interactive) failover.
Invalidated master IP address on 172.168.1.178(172.168.1.178:3306)
Selected 172.168.1.179(172.168.1.179:3306) as a new master.
172.168.1.179(172.168.1.179:3306): OK: Applying all logs succeeded.
172.168.1.179(172.168.1.179:3306): OK: Activated master IP address.
172.168.1.180(172.168.1.180:3306): OK: Slave started, replicating from 172.168.1.179(172.168.1.179:3306)
172.168.1.179(172.168.1.179:3306): Resetting slave info succeeded.
Master failover to 172.168.1.179(172.168.1.179:3306) completed successfully.
[root@mysqldb3 scripts]# masterha_master_switch --conf=/etc/mha/mha.conf --master_state=alive --orig_master_is_new_slave --new_master_host=172.168.1.178 --new_master_port=3306 --running_updates_limit=10000
Tue Jul 21 09:58:24 2020 - [info] MHA::MasterRotate version 0.58.
Tue Jul 21 09:58:24 2020 - [info] Starting online master switch..
Tue Jul 21 09:58:24 2020 - [info]
Tue Jul 21 09:58:24 2020 - [info] * Phase 1: Configuration Check Phase..
Tue Jul 21 09:58:24 2020 - [info]
Tue Jul 21 09:58:24 2020 - [info] Reading default configuration from /etc/masterha_default.cnf..
Tue Jul 21 09:58:24 2020 - [info] Reading application default configuration from /etc/mha/mha.conf..
Tue Jul 21 09:58:24 2020 - [info] Reading server configuration from /etc/mha/mha.conf..
Tue Jul 21 09:58:25 2020 - [error][/usr/local/share/perl5/MHA/ServerManager.pm, ln653] There are 2 non-slave servers! MHA manages at most one non-slave server. Check configurations.
Tue Jul 21 09:58:25 2020 - [error][/usr/local/share/perl5/MHA/ManagerUtil.pm, ln177] Got ERROR: at /usr/local/share/perl5/MHA/MasterRotate.pm line 86.
重置主从关系
(root@localhost_(none)) [10:09]> reset master;
Query OK, 0 rows affected (0.06 sec)
(root@localhost_(none)) [10:09]> change master to master_host='172.168.1.178',master_user='repl',master_password='repl',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
(root@localhost_(none)) [10:09]> start slave;
Can't exec "mysqlbinlog": No such file or directory at /usr/local/share/perl5/MHA/BinlogManager.pm line 106.
solution:
ln -s /usr/local/mysql-5.7.30-linux-glibc2.12-x86_64/bin/mysqlbinlog /usr/local/bin/mysqlbinlog
ln -s /usr/local/mysql-5.7.30-linux-glibc2.12-x86_64/bin/mysql /usr/local/bin/mysql