MySQL高可用之MHA
Posted justin 齐齐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL高可用之MHA相关的知识,希望对你有一定的参考价值。
背景介绍
高可用架构对于互联网服务基本是标配,无论是应用服务还是数据库服务都需要做到高可用。本文是对mysql数据库的高可用方案中,基于主从复制的MHA软件理论部分进行梳理和小结。
MHA软件介绍
1.MHA软件是由MHA Manager(管理节点)和MHA Node(数据节点)组成
2.MHA Manager可以单独部署在一台独立的机器上,也可以部署在一台slave节点上
3.MHA Node运行在每台MySQL服务器上
4.MHA Manager会定时探测集群中的master节点,当master出现故障时,自动将数据最新的slave提升为新的master,然后将其他slave重新指向新的master恢复主从复制。
5.整个故障转移过程对应用程序完全透明
MHA架构介绍
1.目前MHA主要支持一主多从的架构,要搭建MHA,要求一个复制集群中必须最少有三台数据库服务器
2.在一主两从的MHA集群架构中,一台充当master,一台充当备用master,一台充当slave
3.因为至少需要三台服务器,出于机器成本的考虑,淘宝在该基础上做了改进,目前淘宝TMHA支持一主一从
4.我们自己使用时,也可以使用1主1从,但是master主机宕机后无法切换,以及无法补全binlog。不过,master的mysqld进程crash后,还是可以切换成功,以及补全binlog
MHA切换原理
自动故障切换过程
1.配置文件检查阶段,这个阶段会检查整个集群配置文件配置
2.宕机的master处理,这个阶段包括vip移除操作,主机关机操作
3.复制dead master和最新slave相差的relay log,并保存到MHA Manager定义的目录下
4.识别含有最新更新的slave
5.应用从master保存的二进制日志事件(binlog events)
6.提升一个slave为新的master
7.使其他的slave连接新的master进行复制
手工故障切换过程
注意事项:MHA Manager必须没有运行
1.手工调用MHA进行故障切换,masterha_master_switch
2.在经历配置文件检查、存活服务器检查、集群复制检查、备用master设置检查后,询问master阵亡,是否进行切换
3.宕机的master处理,这个阶段包括vip移除操作,主机关机操作
4.复制dead master和最新slave相差的relay log,并保存到MHA Manager定义的目录下
5.识别含有最新更新的slave
6.应用从master保存的二进制日志事件(binlog events)
7.提升一个slave为新的master
8.使其他的slave连接新的master进行复制
手工在线切换过程
1.检测集群复制设置和确定当前master
2.确定新的master
3.阻塞写入到当前master
4.等待所有slave赶上复制
5.授予写入到新的master
6.重新设置slave从新的master进行复制
本文主要介绍如何搭建MHA集群,希望给你带来帮助
基本环境介绍
Linux:CentOS 7.4
MySQL:5.7.21+传统复制(开启GTID不搭建binlog server)
VIP:192.168.68.100
角色 | ip地址 | 主机名 | server_id | 类型 |
---|---|---|---|---|
Master | 192.168.68.161 | server1 | 1413306 | 写 |
Candicate master | 192.168.68.162 | server2 | 1423306 | 读 |
Manager/Slave | 192.168.68.163 | server3 | 1433306 | 读/监控/管理 |
server1
主从复制搭建
#创建复制用户
grant replication slave on *.* to ‘repl‘@‘192.168.68.%‘ identified by ‘repl4slave‘;
flush privileges;
#创建监控用户
grant all privileges on *.* to ‘xmm‘@‘192.168.68.%‘ identified by ‘xmmxmm‘;
flush privileges;
MHA安装
#软件安装
yum localinstall -y mha4mysql-node-0.56-0.el6.noarch
#SSH登陆检查
ssh server2
ssh server3
如果不是一个新master
#master导出数据
mysqldump --master-data=2 --single-transaction -R --triggers -A >server1-all-180329.sql
VIP手工绑定
绑定:/sbin/ip addr add 192.168.68.100/32 dev ens32
解绑:/sbin/ip addr del 192.168.68.100/32 dev ens32
server2
主从复制搭建
#配置和开启复制
change master to master_host=‘192.168.68.161‘,master_port=3306,master_user=‘repl‘,master_password=‘repl4slave‘,master_auto_position=1;
start slave;
#slave开启read_only
mysql -h127.0.0.1 -p -e ‘set global read_only=1‘
mysql -h127.0.0.1 -p -e ‘set global relay_log_purge=0‘
MHA安装
#软件安装
yum localinstall -y mha4mysql-node-0.56-0.el6.noarch
#SSH登陆检查
ssh server1
ssh server3
如果master不是新的
#导入数据
mysql -h127.0.0.1 -uroot -p < /tmp/data/server1-all-180329.sql
server3
主从复制搭建
#导入数据、配置和开启复制
mysql -h127.0.0.1 -uroot -p < /tmp/data/server1-all-180329.sql
change master to master_host=‘192.168.68.135‘,master_port=3306,master_user=‘repl‘,master_password=‘repl4slave‘,master_auto_position=1;
start slave;
#slave开启read_only
mysql -h127.0.0.1 -p -e ‘set global read_only=1‘
mysql -h127.0.0.1 -p -e ‘set global relay_log_purge=0‘
MHA安装
#软件安装
yum localinstall -y mha4mysql-node-0.56-0.el6.noarch
yum localinstall -y mha4mysql-manager-0.56-0.el6.noarch
#SSH无密钥验证登陆
ssh-keygen
cd ~/.ssh/
cat id_rsa.pub > authorized_keys
chmod 600 *
scp -r ~/.ssh/ [email protected]168.68.161:~/
scp -r ~/.ssh/ [email protected]168.68.162:~/
ssh server2
ssh server3
#创建MHA工作目录,上传配置文件和脚本
mkdir -p /etc/masterha
masterha_default.cnf
app1.cnf
master_ip_failover
master_ip_online_change
chmod +x master_ip*
#开启debug
masterha_default.cnf --> log_level=debug
#manager上检查ssh/rple/开启mha进程
masterha_check_ssh xxx
masterha_check_repl xxx
masterha_manager xxx
配置文件和脚本详细
masterha_default.cnf
[server default]
#MySQL的用户和密码
user=xmm
password=xmmxmm
#系统ssh用户
ssh_user=root
#复制用户
repl_user=repl
repl_password=repl4slave
#监控
ping_interval=1
#shutdown_script=""
#日志输出
log_level=debug
#报警脚本
report_script=/etc/masterha/send_report
#切换调用的脚本
master_ip_failover_script= /etc/masterha/master_ip_failover
master_ip_online_change_script= /etc/masterha/master_ip_online_change
app1.cnf
[server default]
#mha manager工作目录
manager_workdir = /var/log/masterha/app1
manager_log = /var/log/masterha/app1/app1.log
remote_workdir = /var/log/masterha/app1
[server1]
hostname=node1
master_binlog_dir = /data/mysql/mysql3306/logs
candidate_master = 0
check_repl_delay = 0
[server2]
hostname=node2
master_binlog_dir=/data/mysql/mysql3306/logs
candidate_master = 1
check_repl_delay = 0
[server3]
hostname=node1
master_binlog_dir = /data/mysql/mysql3306/logs
candidate_master = 0
check_repl_delay = 0
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;
#自定义该组机器的vip
my $vip = "192.168.68.100";
my $if = "ens32";
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
);
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,
);
sub add_vip {
my $output1 = `ssh -o ConnectTimeout=15 -o ConnectionAttempts=3 $orig_master_host /sbin/ip addr del $vip/32 dev $if`;
my $output2 = `ssh -o ConnectTimeout=15 -o ConnectionAttempts=3 $new_master_host /sbin/ip addr add $vip/32 dev $if`;
}
exit &main();
sub main {
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 {
# updating global catalog, etc
$exit_code = 0;
};
if ([email protected]) {
warn "Got Error: [email protected]\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 {
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 "Set read_only=0 on the new master.\n";
$new_master_handler->disable_read_only();
## Creating an app user on the new master
#print "Creating app user on the new master..\n";
#FIXME_xxx_create_user( $new_master_handler->{dbh} );
$new_master_handler->enable_log_bin_local();
$new_master_handler->disconnect();
## Update master ip on the catalog database, etc
&add_vip();
$exit_code = 0;
};
if ([email protected]) {
warn [email protected];
# If you want to continue failover, exit 10.
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
# do nothing
exit 0;
}
else {
&usage();
exit 1;
}
}
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";
}
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;
#添加vip定义
my $vip = "192.168.68.100";
my $if = "ens32";
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,
);
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 drop_vip {
my $output = `ssh -o ConnectTimeout=15 -o ConnectionAttempts=3 $orig_master_host /sbin/ip addr del $vip/32 dev $if`;
#mysql里的连接全部干掉
#FIXME
}
sub add_vip {
my $output = `ssh -o ConnectTimeout=15 -o ConnectionAttempts=3 $new_master_host /sbin/ip addr add $vip/32 dev $if`;
}
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";
print current_time_us() . " drop vip $vip..\n";
#drop_app_user($orig_master_handler);
&drop_vip();
## 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
$exit_code = 0;
};