基本环境
manager & binlog 192.168.1.41
master 192.168.1.42
slave1 192.168.1.43
slave2 192.168.1.44
binlog server 192.168.1.41
确认/etc/my.cnf中的参数
binlog_format=row
gtid_mode = on
enforce_gtid_consistency = on
log-slave-updates = 1
skip_slave_start = 1
并配制好主从关系
本例中复制用户名与密码为
grant replication slave on *.* to rep@'192.168.1.%' identified by 'pwd4rep';
互信
配制ssh两两互通
ssh-keygen
cd ~/.ssh
cat id_rsa.pub > authorized_keys
该文件要包括所有机子的公匙,各节点均保留一份同样的文件
内容约如下
cat authorized_keys
ssh-rsa ....IwkGJcdjTkAashNWlVfD2cQ== root@mha41
ssh-rsa ....KGht29PlsTwCXm3BEGXUPvw== root@mha42
ssh-rsa ....+fb1fM71iGx42U5/CE3qH0Q== root@mha43
ssh-rsa ....uMdoOtmZ9wMqkg9vDwgW9KQ== root@mha44
安装mha
1、manager
41作为manager要安装
mha4mysql-node和mha4mysql-manage
yum install perl-DBD-MySQL
另外还有部分文件本地源中没有,需要下载或连线安装
yum install perl*
tar xzvf mha4mysql-node-0.56.tar.gz
cd mha4mysql-node-0.56
perl Makefile.PL
make && make install
tar xzvf mha4mysql-manager-0.56.tar.gz
cd mha4mysql-manager-0.56
perl Makefile.PL
make && make install
2、node
42、43、44作为node只需要安装node
yum install perl-DBD-MySQL
tar xzvf mha4mysql-node-0.56.tar.gz
cd mha4mysql-node-0.56
perl Makefile.PL
make && make install
配制mha
41上执行
mkdir --p /etc/mha/{app1,scripts}
cd /tmp/mha4mysql-manager-0.56
cp samples/conf/* /etc/mha/
cp samples/scripts/* /etc/mha/scripts/
mv /etc/mha/app1.cnf /etc/mha/app1/
mv /etc/mha/masterha_default.cnf /etc/
mysql增加用户供mha使用
grant all privileges on *.* to mha@'192.168.1.%' identified by 'pwd4mha';
相关文件配制如下
app1.cnf
[root@oel64 ~]# cat /etc/mha/app1/app1.cnf
[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/manager.log
remote_workdir=/var/log/masterha/app1
[server1]
hostname=192.168.1.42
master_binlog_dir=/data/mysql/user_3306/data/
candidate_master=1
check_repl_delay=0
[server2]
hostname=192.168.1.43
master_binlog_dir=/data/mysql/user_3306/data/
candidate_master=1
check_repl_delay=0
[server3]
hostname=192.168.1.44
master_binlog_dir=/data/mysql/user_3306/data/
ignore_fail=1
no_master=1
# [server4]
# hostname=host4
# no_master=1
[binlog1]
hostname=192.168.1.41
master_binlog_dir=/data/mysql/binlog/
ignore_fail=1
no_master=1
masterha_default.cnf
[root@oel64 ~]# cat /etc/masterha_default.cnf
[server default]
user= mha
password= pwd4mha
ssh_user=root
# master_binlog_dir= /var/lib/mysql,/var/log/mysql
# remote_workdir=/data/log/masterha
repl_user= rep
repl_password= pwd4rep
secondary_check_script= masterha_secondary_check -s mha42 -s mha43 -s mha44
ping_interval=1
master_ip_failover_script=/etc/mha/scripts/master_ip_failover
# shutdown_script= /script/masterha/power_manager
# report_script= /script/masterha/send_report
master_ip_online_change_script= /etc/mha/scripts/master_ip_online_change
master_ip_online_change
[root@oel64 ~]# cat /etc/mha/scripts/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.1.45'; # Virtual IP
my $gateway = '192.168.1.2';#Gateway IP
my $interface = 'eth0';
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig $interface:$key $vip;/sbin/arping -I $interface -c 3 -s $vip $gateway >/dev/null 2>&1";
my $ssh_stop_vip = "/sbin/ifconfig $interface:$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} );
}
## 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 {
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
&start_vip();
exit 0;
}
else {
&usage();
exit 1;
}
}
# simple system call that enable the VIP on the new master
sub start_vip() {
`ssh $new_master_ssh_user\@$new_master_ip \" $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 --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
die;
}
master_ip_failover
[root@oel64 ~]# cat /etc/mha/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 = '192.168.1.45'; # Virtual IP
my $gateway = '192.168.1.1';#Gateway IP
my $interface = 'eth0';
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig $interface:$key $vip;/sbin/arping -I $interface -c 3 -s $vip $gateway";
my $ssh_stop_vip = "/sbin/ifconfig $interface:$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 $@;
# If you want to continue failover, exit 10.
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
# do nothing
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_ip \" $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";
}
启动binlog server
mysqlbinlog -R --host=192.168.1.42 --user=mha --password='pwd4mha' --raw --stop-never mysql-bin.000001 &
可以看到42上的binlog已取到41上
[root@oel64 binlog]# ls -lh
总用量 1.4M
-rw-r--r--. 1 root root 68K 4月 10 04:58 mysql-bin.000001
-rw-r--r--. 1 root root 1.2M 4月 10 04:58 mysql-bin.000002
-rw-r--r--. 1 root root 654 4月 10 04:58 mysql-bin.000003
-rw-r--r--. 1 root root 143 4月 10 04:58 mysql-bin.000004
-rw-r--r--. 1 root root 429 4月 10 04:58 mysql-bin.000005
-rw-r--r--. 1 root root 590 4月 10 04:58 mysql-bin.000006
-rw-r--r--. 1 root root 238 4月 10 04:58 mysql-bin.000007
-rw-r--r--. 1 root root 1.5K 4月 10 04:58 mysql-bin.000008
验正
1、验正ssh
masterha_check_ssh --conf=/etc/mha/app1/app1.cnf
部分结果如下:
Mon Dec 29 08:12:41 2014 -- [info] Reading default configuration from /etc/masterha_default.cnf..
Mon Dec 29 08:12:41 2014 -- [info] Reading application default configuration from /etc/mha/app1/app1.cnf..
Mon Dec 29 08:12:41 2014 -- [info] Reading server configuration from /etc/mha/app1/app1.cnf..
Mon Dec 29 08:12:41 2014 -- [info] Starting SSH connection tests..
Mon Dec 29 08:12:42 2014 -- [debug]
Mon Dec 29 08:12:41 2014 -- [debug] Connecting via SSH from root@192.168.1.31(192.168.1.31:22) to root@192.168.1.32(192.168.1.32:22)..
Warning: Permanently added '192.168.1.31' (RSA) to the list of known hosts.
Mon Dec 29 08:12:42 2014 -- [debug] ok.
Mon Dec 29 08:12:42 2014 -- [debug]
Mon Dec 29 08:12:41 2014 -- [debug] Connecting via SSH from root@192.168.1.32(192.168.1.32:22) to root@192.168.1.31(192.168.1.31:22)..
Mon Dec 29 08:12:42 2014 -- [debug] ok.
Mon Dec 29 08:12:42 2014 -- [info] All SSH connection tests passed successfully.
2、验正主从复制
masterha_check_repl --conf=/etc/mha/app1/app1.cnf
部分结果如下:
Fri Apr 10 05:22:53 2015 - [info] Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/data/mysql/binlog/ --output_file=/var/log/masterha/app1/save_binary_logs_test --manager_version=0.56 --start_file=mysql-bin.000008
Fri Apr 10 05:22:53 2015 - [info] Connecting to root@192.168.1.41(192.168.1.41:22)..
Creating /var/log/masterha/app1 if not exists.. ok.
Checking output directory is accessible or not..
ok.
Binlog found at /data/mysql/binlog/, up to mysql-bin.000008
Fri Apr 10 05:22:53 2015 - [info] Binlog setting check done.
Fri Apr 10 05:22:53 2015 - [info] Checking SSH publickey authentication settings on the current master..
Fri Apr 10 05:22:54 2015 - [info] HealthCheck: SSH to 192.168.1.42 is reachable.
Fri Apr 10 05:22:54 2015 - [info]
192.168.1.42(192.168.1.42:3306) (current master)
+--192.168.1.43(192.168.1.43:3306)
+--192.168.1.44(192.168.1.44:3306)
Fri Apr 10 05:22:54 2015 - [info] Checking replication health on 192.168.1.43..
Fri Apr 10 05:22:54 2015 - [info] ok.
Fri Apr 10 05:22:54 2015 - [info] Checking replication health on 192.168.1.44..
Fri Apr 10 05:22:54 2015 - [info] ok.
Fri Apr 10 05:22:54 2015 - [info] Checking master_ip_failover_script status:
Fri Apr 10 05:22:54 2015 - [info] /etc/mha/scripts/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.1.42 --orig_master_ip=192.168.1.42 --orig_master_port=3306
IN SCRIPT TEST====/sbin/ifconfig eth0:1 down==/sbin/ifconfig eth0:1 192.168.1.45;/sbin/arping -I eth0 -c 3 -s 192.168.1.45 192.168.1.1===
Checking the Status of the script.. OK
Fri Apr 10 05:22:58 2015 - [info] OK.
Fri Apr 10 05:22:58 2015 - [warning] shutdown_script is not defined.
Fri Apr 10 05:22:58 2015 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
启停
1、启动
nohup masterha_manager --conf=/etc/mha/app1/app1.cnf > /tmp/mha_manager.log 2>&1 &
2、检测状态
masterha_check_status --conf=/etc/mha/app1/app1.cnf
app1 (pid:30679) is running(0:PING_OK), master:192.168.1.42
3、停止
masterha_stop --conf=/etc/mha/app1/app1.cnf
Stopped app1 successfully.
[2]+ Exit 1 nohup masterha_manager --conf=/etc/mha/app1/app1.cnf > /tmp/mha_manager.log 2>&1
自动切换
正常启动mha后,停止42的mysql服务,可以观察到自动切换
tail /var/log/masterha/app1/manager.log
Check MHA Manager logs at oel64:/var/log/masterha/app1/manager.log for details.
Started automated(non-interactive) failover.
Invalidated master IP address on 192.168.1.42(192.168.1.42:3306)
Selected 192.168.1.43(192.168.1.43:3306) as a new master.
192.168.1.43(192.168.1.43:3306): OK: Applying all logs succeeded.
192.168.1.43(192.168.1.43:3306): OK: Activated master IP address.
192.168.1.44(192.168.1.44:3306): OK: Slave started, replicating from 192.168.1.43(192.168.1.43:3306)
192.168.1.43(192.168.1.43:3306): Resetting slave info succeeded.
Master failover to 192.168.1.43(192.168.1.43:3306) completed successfully.
这时43变为新的主机
可以在41上用命令看
[root@oel64 binlog]# mysql -umha -ppwd4mha -h192.168.1.45 -e 'select @@hostname';
Warning: Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mha43 |
+------------+
44上可以看到,指向了43
mysql> select @@hostname;
+------------+
| @@hostname |
+------------+
| mha44 |
+------------+
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.168.1.43
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
这期间mha调用了master_ip_failover
[root@oel64 app1]# cat manager.log | grep "master_ip_failover"
Fri Apr 10 21:23:39 2015 - [info] Checking master_ip_failover_script status:
Fri Apr 10 21:23:39 2015 - [info] /etc/mha/scripts/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.1.42 --orig_master_ip=192.168.1.42 --orig_master_port=3306
Fri Apr 10 21:24:12 2015 - [info] /etc/mha/scripts/master_ip_failover --orig_master_host=192.168.1.42 --orig_master_ip=192.168.1.42 --orig_master_port=3306 --command=stopssh --ssh_user=root
Fri Apr 10 21:24:13 2015 - [info] /etc/mha/scripts/master_ip_failover --command=start --ssh_user=root --orig_master_host=192.168.1.42 --orig_master_ip=192.168.1.42 --orig_master_port=3306 --new_master_host=192.168.1.43 --new_master_ip=192.168.1.43 --new_master_port=3306 --new_master_user='mha' --new_master_password='pwd4mha'
分别是启动、关闭、切换 vip
手动切换
42修复后要重新设为master
1、同步42上的数据
cat manager.log | grep "change master" -i
Fri Apr 10 21:24:13 2015 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.1.43', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='rep', MASTER_PASSWORD='xxx';
更改上面的语句并执行
CHANGE MASTER TO MASTER_HOST='192.168.1.43',
MASTER_PORT=3306,
MASTER_AUTO_POSITION=1,
MASTER_USER='rep',
MASTER_PASSWORD='pwd4rep';
同步完成后再切换
masterha_master_switch --master_state=alive --conf=/etc/mha/app1/app1.cnf --new_master_host=192.168.1.42 --orig_master_is_new_slave --running_updates_limit=10000
上述语句会调用master_ip_online_change
/etc/mha/scripts/master_ip_online_change
--command=stop
--orig_master_host=192.168.1.43
--orig_master_ip=192.168.1.43
--orig_master_port=3306
--orig_master_user='mha'
--orig_master_password='pwd4mha'
--new_master_host=192.168.1.42
--new_master_ip=192.168.1.42
--new_master_port=3306
--new_master_user='mha'
--new_master_password='pwd4mha'
--orig_master_ssh_user=root
--new_master_ssh_user=root
--orig_master_is_new_slave
/etc/mha/scripts/master_ip_online_change
--command=start
--orig_master_host=192.168.1.43
--orig_master_ip=192.168.1.43
--orig_master_port=3306
--orig_master_user='mha'
--orig_master_password='pwd4mha'
--new_master_host=192.168.1.42
--new_master_ip=192.168.1.42
--new_master_port=3306
--new_master_user='mha'
--new_master_password='pwd4mha'
--orig_master_ssh_user=root
--new_master_ssh_user=root
--orig_master_is_new_slave
切换完后验正vip
[root@oel64 binlog]# mysql -umha -ppwd4mha -h192.168.1.45 -e 'select @@hostname';
Warning: Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mha42 |
+------------+