环境介绍:
一般情况下,搭建MHA需要四台服务器,即一主+两从+管理节点。
在本实验中,我用的是两台虚拟机(192.169.10.241,192.169.10.242)搭建环境。
其中:
192.169.10.241上安装一主(3306)、一从(3307)、mha-manage、mha-node
192.169.10.242上安装一从(3306)、mha-node
Mysql版本:5.7.19
MHA搭建:
服务器配置互信:
1)两节点分别ssh-keygen -t rsa 生成密钥
2)把公钥对传给对端 ssh-copy-id -i /root/.ssh/id_rsa.pub 192.169.10.242(作用是把本机的公钥对写到对端的authorized_keys文件中)
3)ssh 对方测试
[root@rhel62 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
8e:e9:fd:27:95:5c:dc:b6:79:c8:bc:6d:17:c7:57:3e root@rhel62
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| . . |
| o +|
| S . = =+|
| + + +E*|
| o . . o*|
| . . . . . +|
| . ...o ..|
+-----------------+
[root@rhel62 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub 192.169.10.242
root@192.169.10.242's password:
Now try logging into the machine, with "ssh '192.169.10.241'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[root@rhel62 ~]# ssh 192.169.10.242
Last login: Sun Jul 28 02:13:44 2019 from 192.169.10.1
搭建一主二从环境:
这里我采用的是GTID+ROW,备份恢复用的是xtrabackup.
具体可参考(https://blog.youkuaiyun.com/u014710633/article/details/97896172)
安装配置mha-manage和mha-node:
1)安装包下载、安装
https://github.com/yoshinorim/mha4mysql-manager/wiki/Downloads
可以下载源码包和rpm包,我选择的是rpm包。
查看rpm包的依赖包:rpm -qpR xxx.rpm
或者直接rpm -ivh xxx.rpm 安装,缺少依赖包时会提示。
具体依赖包:
perl-CPAN、perl-DBD-MySQL 、perl-Config-Tiny 、perl-Log-Dispatch 、
perl-Parallel-ForkManager 、perl-Time-HiRes 、perl-Params-Validate
我是从这下载的rpm包:(http://www.rpm-find.net/linux/rpm2html/search.php)
[root@rhel6 u01]# ls -l|grep perl
-rw-r--r-- 1 root root 21272 Jul 30 14:57 perl-Config-Tiny-2.12-1.el6.rfx.noarch.rpm
-rw-r--r-- 1 root root 72328 Jul 30 14:57 perl-Log-Dispatch-2.26-1.el6.rf.noarch.rpm
-rw-r--r-- 1 root root 14840 Jul 30 14:57 perl-Parallel-ForkManager-0.7.5-2.2.el6.rf.noarch.rpm
-rw-r--r-- 1 root root 75820 Jul 30 14:59 perl-Params-Validate-0.92-3.el6.x86_64.rpm
每个节点安装mha-node
[root@rhel6 u01]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
Preparing... ########################################### [100%]
1:mha4mysql-node ########################################### [100%]
监控节点安装mha-manager
[root@rhel6 u01]# rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm
Preparing... ########################################### [100%]
1:mha4mysql-manager ########################################### [100%]
2)mha-manager配置
首先创建和配置参数文件
[root@rhel6 bin]# mkdir -p /etc/mha
[root@rhel6 bin]# cd /etc/mha
[root@rhel6 u01]# cat /etc/mha/mha.conf
[server default]
user=root
password=xxx
manager_workdir=/u01/mha
manager_log=/u01/mha/manager.log
remote_workdir=/u01/mha
ssh_user=root
repl_user=repl
repl_password=111111
ping_interval=1
master_ip_failover_script=/u01/mha/master_ip_failover
master_ip_online_change_script=/u01/mha/master_ip_online_change
[server1]
hostname=192.169.10.241
port=3306
master_binlog_dir=/u01/mysql/log
candidate_master=1
[server2]
hostname=192.169.10.242
port=3306
master_binlog_dir=/u01/mysql/log
candidate_master=1
check_repl_delay=0
[server3]
hostname=192.169.10.241
port=3307
master_binlog_dir=/u01/mysql3307/log
no_master=1
然后是配置两个切换脚本master_ip_failover和master_ip-online_change(在参数文件里有指定),记得在脚本里面配置vip信息
[root@rhel6 mha]# 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 $vip = '192.169.10.240/16';
my $key = "0";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
GetOptions(
'command=s' => \$command,
'ssh_user=s'=> \$ssh_user,
'orig_master_host=s' => \$orig_master_host,
'orig_master_ip=s' => \$orig_master_ip,
'orig_master_port=i' => \$orig_master_port,
'new_master_host=s' => \$new_master_host,
'new_master_ip=s' => \$new_master_ip,
'new_master_port=i' => \$new_master_port,
);
exit &main();
sub main {
print "\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@rhel6 mha]# cat 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 = '192.169.10.240/16';
my $key = "0";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
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;
}
使用脚本验证环境:
检查ssh连通性
[root@rhel6 mha]# masterha_check_ssh --conf=/etc/mha/mha.conf
Wed Jul 31 02:10:30 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Jul 31 02:10:30 2019 - [info] Reading application default configuration from /etc/mha/mha.conf..
Wed Jul 31 02:10:30 2019 - [info] Reading server configuration from /etc/mha/mha.conf..
Wed Jul 31 02:10:30 2019 - [info] Starting SSH connection tests..
Wed Jul 31 02:10:32 2019 - [debug]
Wed Jul 31 02:10:31 2019 - [debug] Connecting via SSH from root@192.169.10.241(192.169.10.241:22) to root@192.169.10.241(192.169.10.241:22)..
Wed Jul 31 02:10:31 2019 - [debug] ok.
Wed Jul 31 02:10:31 2019 - [debug] Connecting via SSH from root@192.169.10.241(192.169.10.241:22) to root@192.169.10.242(192.169.10.242:22)..
Wed Jul 31 02:10:32 2019 - [debug]
Wed Jul 31 02:10:31 2019 - [debug] Connecting via SSH from root@192.169.10.242(192.169.10.242:22) to root@192.169.10.241(192.169.10.241:22)..
Wed Jul 31 02:10:31 2019 - [debug] ok.
Wed Jul 31 02:10:31 2019 - [debug] Connecting via SSH from root@192.169.10.242(192.169.10.242:22) to root@192.169.10.241(192.169.10.241:22)..
Wed Jul 31 02:10:32 2019 - [debug] ok.
Wed Jul 31 02:10:32 2019 - [info] All SSH connection tests passed successfully.
检查主从复制运行状态
[root@rhel6 mha]# masterha_check_repl --conf=/etc/mha/mha.conf
Wed Jul 31 02:31:41 2019 - [info] Checking replication health on 192.169.10.242..
Wed Jul 31 02:31:41 2019 - [info] ok.
Wed Jul 31 02:31:41 2019 - [info] Checking replication health on 192.169.10.241..
Wed Jul 31 02:31:41 2019 - [info] ok.
Wed Jul 31 02:31:41 2019 - [info] Checking master_ip_failover_script status:
Wed Jul 31 02:31:41 2019 - [info] /u01/mha/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.169.10.241 --orig_master_ip=192.169.10.241 --orig_master_port=3306
IN SCRIPT TEST====/sbin/ifconfig eth0:1 down==/sbin/ifconfig eth0:1 192.169.10.240/24===
Checking the Status of the script.. OK
Wed Jul 31 02:31:41 2019 - [info] OK.
Wed Jul 31 02:31:41 2019 - [warning] shutdown_script is not defined.
Wed Jul 31 02:31:41 2019 - [info] Got exit code 0 (Not master dead).
检查MHA-manager状态
[root@rhel6 ~]# masterha_check_status --conf=/etc/mha/mha.conf
mha is stopped(2:NOT_RUNNING).
开启MHA-manager
[root@rhel6 mha]# nohup masterha_manager --conf=/etc/mha/mha.conf >/tmp/mha_manager.log < /dev/null 2>&1 &
[1] 23388
[root@rhel6 mha]# masterha_check_status --conf=/etc/mha/mha.conf
mha (pid:23388) is running(0:PING_OK), master:192.169.10.241
MHA运行测试:
failover:
模拟主库down掉
[root@rhel6 mha]# mysqladmin -uroot -pxxx shutdown
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
从库上检查vip,发现已经绑定上
[root@rhel62 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d8:ba:4d brd ff:ff:ff:ff:ff:ff
inet 192.169.10.242/16 brd 192.169.255.255 scope global eth0
inet 192.169.10.240/16 brd 192.169.255.255 scope global secondary eth0:0
inet6 fe80::20c:29ff:fed8:ba4d/64 scope link
valid_lft forever preferred_lft forever
5: pan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN
link/ether 6e:0b:3e:81:bf:f1 brd ff:ff:ff:ff:ff:ff
从库上检查复制状态,发现其已被提升为master
mysql> show slave status\G
Empty set (0.00 sec)
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000002
Position: 3775571
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 5f95521a-b073-11e9-9591-000c296ee978:1-130602,
7842c967-0958-11e9-9f6c-000c296ee978:1-36,
d21170f1-aa92-11e9-89bd-000c29d8ba4d:1-9851,
eb495b42-b06a-11e9-af85-000c296ee978:1-3,
fad44c35-affe-11e9-b2fc-000c296ee978:1-2
1 row in set (0.00 sec)
另一个从库上检查复制状态 ,发现其已指向新master
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.169.10.242
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 3775571
Relay_Log_File: rhel6-relay-bin.000002
Relay_Log_Pos: 414
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 3775571
Relay_Log_Space: 621
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 10243
Master_UUID: d21170f1-aa92-11e9-89bd-000c29d8ba4d
Master_Info_File: /u01/mysql3307/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: 5f95521a-b073-11e9-9591-000c296ee978:1-130602,
7842c967-0958-11e9-9f6c-000c296ee978:1-36,
d21170f1-aa92-11e9-89bd-000c29d8ba4d:1-9851,
d812b5b4-b13c-11e9-a0a8-000c296ee978:1-72,
eb495b42-b06a-11e9-af85-000c296ee978:1-3,
fad44c35-affe-11e9-b2fc-000c296ee978:1-2
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
检查MHA-manager的状态,发现其在failover之后自动停止了
[root@rhel6 mha]# masterha_check_status --conf=/etc/mha/mha.conf
mha is stopped(2:NOT_RUNNING).
启动原主库,然后将其加入到复制组中
mysql> change master to master_host='192.169.10.242',master_user='repl',master_password='111111',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.169.10.242
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 3775571
Relay_Log_File: rhel6-relay-bin.000002
Relay_Log_Pos: 414
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 3775571
Relay_Log_Space: 621
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 10243
Master_UUID: d21170f1-aa92-11e9-89bd-000c29d8ba4d
Master_Info_File: /u01/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: 5f95521a-b073-11e9-9591-000c296ee978:1-130602,
7842c967-0958-11e9-9f6c-000c296ee978:1-36,
d21170f1-aa92-11e9-89bd-000c29d8ba4d:1-9851,
eb495b42-b06a-11e9-af85-000c296ee978:1-3,
fad44c35-affe-11e9-b2fc-000c296ee978:1-2
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
switchover:
管理节点上执行switchover脚本
[root@rhel6 mha]# masterha_master_switch --conf=/etc/mha/mha.conf --master_state=alive --new_master_host=192.169.10.241 --new_master_port=3306 --orig_master_is_new_slave
Wed Jul 31 15:21:00 2019 - [info] MHA::MasterRotate version 0.56.
Wed Jul 31 15:21:00 2019 - [info] Starting online master switch..
Wed Jul 31 15:21:00 2019 - [info]
Wed Jul 31 15:21:00 2019 - [info] * Phase 1: Configuration Check Phase..
Wed Jul 31 15:21:00 2019 - [info]
Wed Jul 31 15:21:00 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Jul 31 15:21:00 2019 - [info] Reading application default configuration from /etc/mha/mha.conf..
Wed Jul 31 15:21:00 2019 - [info] Reading server configuration from /etc/mha/mha.conf..
Wed Jul 31 15:21:00 2019 - [info] GTID failover mode = 1
Wed Jul 31 15:21:00 2019 - [info] Current Alive Master: 192.169.10.242(192.169.10.242:3306)
Wed Jul 31 15:21:00 2019 - [info] Alive Slaves:
Wed Jul 31 15:21:00 2019 - [info] 192.169.10.241(192.169.10.241:3306) Version=5.7.19-log (oldest major version between slaves) log-bin:enabled
Wed Jul 31 15:21:00 2019 - [info] GTID ON
Wed Jul 31 15:21:00 2019 - [info] Replicating from 192.169.10.242(192.169.10.242:3306)
Wed Jul 31 15:21:00 2019 - [info] Primary candidate for the new Master (candidate_master is set)
Wed Jul 31 15:21:00 2019 - [info] 192.169.10.241(192.169.10.241:3307) Version=5.7.19-log (oldest major version between slaves) log-bin:enabled
Wed Jul 31 15:21:00 2019 - [info] GTID ON
Wed Jul 31 15:21:00 2019 - [info] Replicating from 192.169.10.242(192.169.10.242:3306)
Wed Jul 31 15:21:00 2019 - [info] Not candidate for the new Master (no_master is set)
It is better to execute FLUSH NO_WRITE_TO_BINLOG TABLES on the master before switching. Is it ok to execute on 192.169.10.242(192.169.10.242:3306)? (YES/no): yes
Wed Jul 31 15:21:04 2019 - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..
Wed Jul 31 15:21:04 2019 - [info] ok.
Wed Jul 31 15:21:04 2019 - [info] Checking MHA is not monitoring or doing failover..
Wed Jul 31 15:21:04 2019 - [info] Checking replication health on 192.169.10.241..
Wed Jul 31 15:21:04 2019 - [info] ok.
Wed Jul 31 15:21:04 2019 - [info] Checking replication health on 192.169.10.241..
Wed Jul 31 15:21:04 2019 - [info] ok.
Wed Jul 31 15:21:04 2019 - [info] 192.169.10.241 can be new master.
Wed Jul 31 15:21:04 2019 - [info]
From:
192.169.10.242(192.169.10.242:3306) (current master)
+--192.169.10.241(192.169.10.241:3306)
+--192.169.10.241(192.169.10.241:3307)
To:
192.169.10.241(192.169.10.241:3306) (new master)
+--192.169.10.241(192.169.10.241:3307)
+--192.169.10.242(192.169.10.242:3306)
Starting master switch from 192.169.10.242(192.169.10.242:3306) to 192.169.10.241(192.169.10.241:3306)? (yes/NO): yes
Wed Jul 31 15:21:06 2019 - [info] Checking whether 192.169.10.241(192.169.10.241:3306) is ok for the new master..
Wed Jul 31 15:21:06 2019 - [info] ok.
Wed Jul 31 15:21:06 2019 - [info] 192.169.10.242(192.169.10.242:3306): SHOW SLAVE STATUS returned empty result. To check replication filtering rules, temporarily executing CHANGE MASTER to a dummy host.
Wed Jul 31 15:21:06 2019 - [info] 192.169.10.242(192.169.10.242:3306): Resetting slave pointing to the dummy host.
Wed Jul 31 15:21:06 2019 - [info] ** Phase 1: Configuration Check Phase completed.
Wed Jul 31 15:21:06 2019 - [info]
Wed Jul 31 15:21:06 2019 - [info] * Phase 2: Rejecting updates Phase..
Wed Jul 31 15:21:06 2019 - [info]
Wed Jul 31 15:21:06 2019 - [info] Executing master ip online change script to disable write on the current master:
Wed Jul 31 15:21:06 2019 - [info] /u01/mha/master_ip_online_change --command=stop --orig_master_host=192.169.10.242 --orig_master_ip=192.169.10.242 --orig_master_port=3306 --orig_master_user='root' --orig_master_password='Cqmyg14dss' --new_master_host=192.169.10.241 --new_master_ip=192.169.10.241 --new_master_port=3306 --new_master_user='root' --new_master_password='Cqmyg14dss' --orig_master_ssh_user=root --new_master_ssh_user=root --orig_master_is_new_slave
Wed Jul 31 15:21:06 2019 888130 Set read_only on the new master.. ok.
Wed Jul 31 15:21:06 2019 903813 Waiting all running 2 threads are disconnected.. (max 1500 milliseconds)
{'Time' => '495','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '111','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => 'rhel6:41394'}
{'Time' => '458','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '113','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => 'rhel6:41403'}
Wed Jul 31 15:21:07 2019 406644 Waiting all running 2 threads are disconnected.. (max 1000 milliseconds)
{'Time' => '495','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '111','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => 'rhel6:41394'}
{'Time' => '458','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '113','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => 'rhel6:41403'}
Wed Jul 31 15:21:07 2019 907152 Waiting all running 2 threads are disconnected.. (max 500 milliseconds)
{'Time' => '496','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '111','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => 'rhel6:41394'}
{'Time' => '459','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '113','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => 'rhel6:41403'}
Wed Jul 31 15:21:08 2019 408732 Set read_only=1 on the orig master.. ok.
Wed Jul 31 15:21:08 2019 410690 Waiting all running 2 queries are disconnected.. (max 500 milliseconds)
{'Time' => '496','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '111','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => 'rhel6:41394'}
{'Time' => '459','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '113','Info' => undef,'User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => 'rhel6:41403'}
Disabling the VIP on old master: 192.169.10.242
Wed Jul 31 15:21:09 2019 257942 Killing all application threads..
Wed Jul 31 15:21:09 2019 271282 done.
Wed Jul 31 15:21:09 2019 - [info] ok.
Wed Jul 31 15:21:09 2019 - [info] Locking all tables on the orig master to reject updates from everybody (including root):
Wed Jul 31 15:21:09 2019 - [info] Executing FLUSH TABLES WITH READ LOCK..
Wed Jul 31 15:21:09 2019 - [info] ok.
Wed Jul 31 15:21:09 2019 - [info] Orig master binlog:pos is mysql-bin.000002:3775571.
Wed Jul 31 15:21:09 2019 - [info] Waiting to execute all relay logs on 192.169.10.241(192.169.10.241:3306)..
Wed Jul 31 15:21:09 2019 - [info] master_pos_wait(mysql-bin.000002:3775571) completed on 192.169.10.241(192.169.10.241:3306). Executed 0 events.
Wed Jul 31 15:21:09 2019 - [info] done.
Wed Jul 31 15:21:09 2019 - [info] Getting new master's binlog name and position..
Wed Jul 31 15:21:09 2019 - [info] mysql-bin.000009:314
Wed Jul 31 15:21:09 2019 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.169.10.241', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='xxx';
Wed Jul 31 15:21:09 2019 - [info] Executing master ip online change script to allow write on the new master:
Wed Jul 31 15:21:09 2019 - [info] /u01/mha/master_ip_online_change --command=start --orig_master_host=192.169.10.242 --orig_master_ip=192.169.10.242 --orig_master_port=3306 --orig_master_user='root' --orig_master_password='Cqmyg14dss' --new_master_host=192.169.10.241 --new_master_ip=192.169.10.241 --new_master_port=3306 --new_master_user='root' --new_master_password='Cqmyg14dss' --orig_master_ssh_user=root --new_master_ssh_user=root --orig_master_is_new_slave
Wed Jul 31 15:21:09 2019 414554 Set read_only=0 on the new master.
Enabling the VIP - 192.169.10.240/16 on the new master - 192.169.10.241
Wed Jul 31 15:21:09 2019 - [info] ok.
Wed Jul 31 15:21:09 2019 - [info]
Wed Jul 31 15:21:09 2019 - [info] * Switching slaves in parallel..
Wed Jul 31 15:21:09 2019 - [info]
Wed Jul 31 15:21:09 2019 - [info] -- Slave switch on host 192.169.10.241(192.169.10.241:3307) started, pid: 27819
Wed Jul 31 15:21:09 2019 - [info]
Wed Jul 31 15:21:09 2019 - [info] Log messages from 192.169.10.241 ...
Wed Jul 31 15:21:09 2019 - [info]
Wed Jul 31 15:21:09 2019 - [info] Waiting to execute all relay logs on 192.169.10.241(192.169.10.241:3307)..
Wed Jul 31 15:21:09 2019 - [info] master_pos_wait(mysql-bin.000002:3775571) completed on 192.169.10.241(192.169.10.241:3307). Executed 0 events.
Wed Jul 31 15:21:09 2019 - [info] done.
Wed Jul 31 15:21:09 2019 - [info] Resetting slave 192.169.10.241(192.169.10.241:3307) and starting replication from the new master 192.169.10.241(192.169.10.241:3306)..
Wed Jul 31 15:21:09 2019 - [info] Executed CHANGE MASTER.
Wed Jul 31 15:21:09 2019 - [info] Slave started.
Wed Jul 31 15:21:09 2019 - [info] End of log messages from 192.169.10.241 ...
Wed Jul 31 15:21:09 2019 - [info]
Wed Jul 31 15:21:09 2019 - [info] -- Slave switch on host 192.169.10.241(192.169.10.241:3307) succeeded.
Wed Jul 31 15:21:09 2019 - [info] Unlocking all tables on the orig master:
Wed Jul 31 15:21:09 2019 - [info] Executing UNLOCK TABLES..
Wed Jul 31 15:21:09 2019 - [info] ok.
Wed Jul 31 15:21:09 2019 - [info] Starting orig master as a new slave..
Wed Jul 31 15:21:09 2019 - [info] Resetting slave 192.169.10.242(192.169.10.242:3306) and starting replication from the new master 192.169.10.241(192.169.10.241:3306)..
Wed Jul 31 15:21:09 2019 - [info] Executed CHANGE MASTER.
Wed Jul 31 15:21:10 2019 - [info] Slave started.
Wed Jul 31 15:21:10 2019 - [info] All new slave servers switched successfully.
Wed Jul 31 15:21:10 2019 - [info]
Wed Jul 31 15:21:10 2019 - [info] * Phase 5: New master cleanup phase..
Wed Jul 31 15:21:10 2019 - [info]
Wed Jul 31 15:21:10 2019 - [info] 192.169.10.241: Resetting slave info succeeded.
Wed Jul 31 15:21:10 2019 - [info] Switching master to 192.169.10.241(192.169.10.241:3306) completed successfully.
检查switchover后的vip状态和各节点的复制信息
[root@rhel6 mha]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
link/ether 00:0c:29:6e:e9:78 brd ff:ff:ff:ff:ff:ff
inet 192.169.10.241/16 brd 192.169.255.255 scope global eth0
inet 192.169.10.240/32 scope global eth0
inet 192.169.10.240/16 brd 192.169.255.255 scope global secondary eth0:0
inet6 fe80::20c:29ff:fe6e:e978/64 scope link
valid_lft forever preferred_lft forever
5: pan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN
link/ether 2a:e9:6e:55:e4:a1 brd ff:ff:ff:ff:ff:ff
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000009
Position: 314
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 5f95521a-b073-11e9-9591-000c296ee978:1-130602,
7842c967-0958-11e9-9f6c-000c296ee978:1-36,
d21170f1-aa92-11e9-89bd-000c29d8ba4d:1-9851,
eb495b42-b06a-11e9-af85-000c296ee978:1-3,
fad44c35-affe-11e9-b2fc-000c296ee978:1-2
1 row in set (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.169.10.241
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 314
Relay_Log_File: rhel6-relay-bin.000002
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000009
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 314
Relay_Log_Space: 574
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 10241
Master_UUID: 5f95521a-b073-11e9-9591-000c296ee978
Master_Info_File: /u01/mysql3307/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: 5f95521a-b073-11e9-9591-000c296ee978:1-130602,
7842c967-0958-11e9-9f6c-000c296ee978:1-36,
d21170f1-aa92-11e9-89bd-000c29d8ba4d:1-9851,
d812b5b4-b13c-11e9-a0a8-000c296ee978:1-72,
eb495b42-b06a-11e9-af85-000c296ee978:1-3,
fad44c35-affe-11e9-b2fc-000c296ee978:1-2
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.169.10.241
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 314
Relay_Log_File: rhel62-relay-bin.000002
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000009
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 314
Relay_Log_Space: 575
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 10241
Master_UUID: 5f95521a-b073-11e9-9591-000c296ee978
Master_Info_File: /u01/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: 5f95521a-b073-11e9-9591-000c296ee978:1-130602,
7842c967-0958-11e9-9f6c-000c296ee978:1-36,
d21170f1-aa92-11e9-89bd-000c29d8ba4d:1-9851,
eb495b42-b06a-11e9-af85-000c296ee978:1-3,
fad44c35-affe-11e9-b2fc-000c296ee978:1-2
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
关于MHA的一些疑问:
1)当master挂掉后,如果其和latest slave之前仍存在延迟,那么failover之后这部分数据岂不是丢了
2)mha中基于gtid复制和基于pos复制,二者有什么区别