mysql 5.7.25+MHA(0.58)+redhat6.9
http://code.google.com/p/mysql-master-ha/
分别下载:
mha4mysql-node-0.58
mha4mysql-manger-0.58
版本要选下,主页的版本,编译的时候失败
[root@DEV-ZZZZ] mha4mysql-node-0.58]# perl Makefile.PL
*** Module::AutoInstall version 1.06
*** Checking for Perl dependencies…
[Core Features]
make && make install
perl Makefile.PL
make && make install
由于MHA manager通过SSH访问所有的node节点,各个node节点也同样通过SSH来相互发送不同的relay log 文件,所以要在每一个node和manager上配置SSH无密码登陆。
ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
(如果mange和备库再同一个节点,本身给自己也要做)
主路径:/usr/local/masterha
cd masterha
mkdir config
mkdir data
mkdir log
mkdir scripts
他可以管理多个mha ,所以最好就是分不同的集群(建议而已,目录结构习惯)
下级目录为dev1 对应的集群名称,里面包含
config —配置文件
data —管理数据
log —日志
scripts —脚本
[server default]
# mysql user and password
user=rep_mha #MHA管理账户
password=rep_mha
ssh_user=root
repl_user=repl
repl_password=xxx@xxxxx
ssh_user=root
ping_interval=2 #ping间隔时间,用来检查master是否正常
# working directory on the manager
manager_workdir=/usr/local/masterha/dev1/data #MHA工作目录
# working directory on MySQL servers
remote_workdir=/usr/local/masterha/dev1/data #MHA工作目录
master_ip_failover_script=/usr/local/masterha/dev1/scripts/master_ip_failover
master_ip_online_change_script=/usr/local/masterha/dev1/scripts/master_ip_online_change
[server1]
hostname=xxx.xxx.xxx.27
master_binlog_dir=/usr/local/mysql/data
#指定MASTER节点binlog存放目录
candidate_master=1
#master宕机后,优先启用这台作为master
[server2]
hostname=xxx.xxx.xxx.28
master_binlog_dir=/usr/local/mysql/data
#指定MASTER节点binlog存放目录
candidate_master=1
#master宕机后,优先启用这台作为master
[server3]
hostname=xxx.xxx.xxx.29
master_binlog_dir=/usr/local/mysql/data
#指定MASTER节点binlog存放目录
no_master=1
#设置na_master=1,使服务器不能成为master
grant all privileges on . to ‘rep_mha’@‘xxx.xxx.xxx.26’ identified by ‘xxx@xxxxx’;
grant all privileges on . to ‘xxx@xxxxx’@‘xxx.xxx.xxx.27’ identified by ‘xxx@xxxxx’;
grant all privileges on . to ‘xxx@xxxxx’@‘xxx.xxx.xxx.28’ identified by ‘xxx@xxxxx’;
grant all privileges on . to ‘xxx@xxxxx’@‘xxx.xxx.xxx.29’ identified by ‘xxx@xxxxx’;
flush privileges;
创建repl用户,在主和备主都需要互相
检查ssh互信
masterha_check_ssh --conf=/usr/local/masterha/config/mha.cnf
Tue May 21 17:19:23 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue May 21 17:19:23 2019 - [info] Reading application default configuration from /usr/local/masterha/config/mha.cnf..
Tue May 21 17:19:23 2019 - [info] Reading server configuration from /usr/local/masterha/config/mha.cnf..
Tue May 21 17:19:23 2019 - [info] Starting SSH connection tests..
Tue May 21 17:19:24 2019 - [debug]
Tue May 21 17:19:23 2019 - [debug] Connecting via SSH from [email protected](xxx.xxx.xxx.27:22) to [email protected](xxx.xxx.xxx.28:22)..
Tue May 21 17:19:23 2019 - [debug] ok.
Tue May 21 17:19:23 2019 - [debug] Connecting via SSH from [email protected](xxx.xxx.xxx.27:22) to [email protected](xxx.xxx.xxx.29:22)..
Tue May 21 17:19:24 2019 - [debug] ok.
Tue May 21 17:19:24 2019 - [debug]
Tue May 21 17:19:24 2019 - [debug] Connecting via SSH from [email protected](xxx.xxx.xxx.28:22) to [email protected](xxx.xxx.xxx.27:22)..
Tue May 21 17:19:24 2019 - [debug] ok.
Tue May 21 17:19:24 2019 - [debug] Connecting via SSH from [email protected](xxx.xxx.xxx.28:22) to [email protected](xxx.xxx.xxx.29:22)..
Tue May 21 17:19:24 2019 - [debug] ok.
Tue May 21 17:19:25 2019 - [debug]
Tue May 21 17:19:24 2019 - [debug] Connecting via SSH from [email protected](xxx.xxx.xxx.29:22) to [email protected](xxx.xxx.xxx.27:22)..
Tue May 21 17:19:24 2019 - [debug] ok.
Tue May 21 17:19:25 2019 - [debug] Connecting via SSH from [email protected](xxx.xxx.xxx.29:22) to [email protected](xxx.xxx.xxx.28:22)..
Tue May 21 17:19:25 2019 - [debug] ok.
Tue May 21 17:19:25 2019 - [info] All SSH connection tests passed successfully.
检查主从复制是否合适MHA配置
masterha_check_repl --conf=/usr/local/masterha/dev1/config/mha.cnf
Thu May 23 09:05:00 2019 - [info] Checking replication health on xxx.xxx.xxx.28..
Thu May 23 09:05:00 2019 - [info] ok.
Thu May 23 09:05:00 2019 - [info] Checking replication health on xxx.xxx.xxx.29..
Thu May 23 09:05:00 2019 - [info] ok.
Thu May 23 09:05:00 2019 - [warning] master_ip_failover_script is not defined.
Thu May 23 09:05:00 2019 - [warning] shutdown_script is not defined.
Thu May 23 09:05:00 2019 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
验证成功
其中有有几个问题:
1.注意配置文件项后面是否有空格
会报错如下:
2.socket等版本太老。
可以使用cpan 阿里源 进行更新
cpan
install YAML
install Socket
install IO::Socket::INET6
#打开cpan
cpan
#查看配置>o conf
#指定阿里云cpan源,国内还是这个比较稳定,被墙的你懂的>o conf urllist push http://mirrors.aliyun.com/CPAN/
#提交更改>o conf commit
#查看是否修改成功>o conf urllist
#安装相关插件,记得要按yes>install IO::Socket::INET6
#也是相关插件,记得要按yes>install ExtUtils::Constant
#还是相关插件,记得要按yes>install YAML
#安装上本坑的重点,socket插件>install Socket
#顺便也将坑1的问题也修复一下吧,记得要按yes>install DBD::mysql
出现如下报错
Wed May 22 09:35:31 2019 - [error][/usr/local/share/perl5/MHA/ManagerUtil.pm, ln123]
"NI_NUMERICHOST" is not exported by the Socket module
"getaddrinfo" is not exported by the Socket module
"getnameinfo" is not exported by the Socket module
Can't continue after import errors at /usr/local/share/perl5/MHA/NodeUtil.pm line 29
BEGIN failed--compilation aborted at /usr/local/share/perl5/MHA/NodeUtil.pm line 29.
每台机器上都需要
3.报错:
Wed May 22 11:23:21 2019 - [info] Connecting to [email protected](xxx.xxx.xxx.28:22)..
Can't exec "mysqlbinlog": No such file or directory at /usr/local/share/perl5/MHA/BinlogManager.pm line 106.
mysqlbinlog version command failed with rc 1:0, please verify PATH, LD_LIBRARY_PATH, and client options
通过创建软连接
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/local/bin/mysqlbinlog
ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
每台机器都需要
nohup masterha_manager --conf=/usr/local/masterha/dev1/config/mha.cnf > /usr/local/masterha/dev1/log/dev1.log 2>&1 &
masterha_check_status --conf=/usr/local/masterha/config/mha.cnf
mha (pid:18426) is running(0:PING_OK), master:xxx.xxx.xxx.27
1.开启一个窗口查询
2.28关闭主mysql
service mysql stop
Shutting down MySQL............ [ OK ]
3.执行查询select * from mysql.user;
4.ifconfig查看主机上的ip ,vip已漂移到新的主机27上
5.查看新的主重状态
6.相关日志:
Fri May 24 13:54:58 2019 - [info] Replicating from xxx.xxx.xxx.28(xxx.xxx.xxx.28:3306)
Fri May 24 13:54:58 2019 - [info] Not candidate for the new Master (no_master is set)
Fri May 24 13:54:58 2019 - [info] Checking slave configurations..
Fri May 24 13:54:58 2019 - [info] read_only=1 is not set on slave xxx.xxx.xxx.27(xxx.xxx.xxx.27:3306).
Fri May 24 13:54:58 2019 - [warning] relay_log_purge=0 is not set on slave xxx.xxx.xxx.27(xxx.xxx.xxx.27:3306).
Fri May 24 13:54:58 2019 - [info] read_only=1 is not set on slave xxx.xxx.xxx.29(xxx.xxx.xxx.29:3306).
Fri May 24 13:54:58 2019 - [warning] relay_log_purge=0 is not set on slave xxx.xxx.xxx.29(xxx.xxx.xxx.29:3306).
Fri May 24 13:54:58 2019 - [info] Checking replication filtering settings..
Fri May 24 13:54:58 2019 - [info] Replication filtering check ok.
Fri May 24 13:54:58 2019 - [info] Master is down!
Fri May 24 13:54:58 2019 - [info] Terminating monitoring script.
Fri May 24 13:54:58 2019 - [info] Got exit code 20 (Master dead).
1.关闭mha_mange
masterha_stop --conf=/usr/local/masterha/dev1/config/mha.cnf
2.检查状态:
masterha_check_status --conf=/usr/local/masterha/dev1/config/mha.cnf
masterha_check_ssh --conf=/usr/local/masterha/dev1/config/mha.cnf
masterha_check_repl --conf=/usr/local/masterha/dev1/config/mha.cnf
3.切换:
masterha_master_switch --master_state=alive --conf=/usr/local/masterha/config/mha.cnf --new_master_host=xxx.xxx.xxx.28 --orig_master_is_new_slave --running_updates_limit=1000
–orig_master_is_new_slave切换时加上此参数是将原master变为slave节点,如果不加此参数,原来的master将不启动
–running_updates_limit=10000 切换时候选master如果有延迟的话,mha切换不能成功,加上此参数表示延迟在此时间范围内都可切换(单位为s),但是切换的时间长短是由recover时relay日志的大小决定
Mon May 27 21:31:04 2019 - [info] Checking master_ip_failover_script status:
Mon May 27 21:31:04 2019 - [info] /usr/local/masterha/ste/scripts/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.111.78 --orig_master_ip=192.168.111.78 --orig_master_port=3306
Mon May 27 21:31:04 2019 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln427] Error happened on checking configurations. Can't exec "/usr/local/masterha/ste/scripts/master_ip_failover": Permission denied at /usr/local/share/perl5/MHA/ManagerUtil.pm line 68.
Mon May 27 21:31:04 2019 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln525] Error happened on monitoring servers.
Mon May 27 21:31:04 2019 - [info] Got exit code 1 (Not master dead).
MySQL Replication Health is NOT OK!
Mon May 27 21:31:04 2019 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln229] Failed to get master_ip_failover_script status with return code 1:0.
Mon May 27 21:31:04 2019 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln427] Error happened on checking configurations. at /usr/local/bin/masterha_check_repl line 48.
Mon May 27 21:31:04 2019 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln525] Error happened on monitoring servers.
Mon May 27 21:31:04 2019 - [info] Got exit code 1 (Not master dead).
chmod u+x 对应的执行文件
对应的脚本
#!/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 = ''xxx.xxx.xxx.xxx';
my $ssh_start_vip = "/sbin/ifconfig eth0:1 $vip";
my $ssh_flush_vip = "arping -c 4 -A -I eth0:1 $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:1 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" ) {
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;
}
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";
exit 0;
}
else {
&usage();
exit 1;
}
}
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
`ssh $ssh_user\@$new_master_host \" $ssh_flush_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";
#!/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 = 'xxx.xxx.xxx';
my $ssh_start_vip = "/sbin/ifconfig eth0:1 $vip";
my $ssh_flush_vip = "arping -c 4 -A -I eth0:1 $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:1 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";
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 \"`;
`ssh $new_master_ssh_user\@$new_master_host \" $ssh_flush_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;
}