MHA 一主一从搭建

安装基本环境介绍

一主一从

MHA manager & slave  node2 192.168.56.27
Master机器 node1 192.168.56.26
VIP   192.168.56.25

配置互信

在192.168.56.26 使用ssh-keygen生成key

输入下面的命令,一路回车就行了。最后会在~/.ssh下面产生:id_rsa  id_rsa.pub两个文件。

#ssh-keygen

生成信任文件:

#cd ~/.ssh/

#cat id_rsa.pub >authorized_keys

#chmod 600 *

#cd ~

#scp –r .ssh  192.168.56.27:~/

在ms1 点验证

#  ssh node2 date

在ms2 点验证

#  ssh node1 date

关于MySQL安装

MySQL建议使用MySQL-5.5.X后版本。自行安装,完成一主一从的结构。(主从配置不在描述)

如果使用mysql-5.6建议禁用GTID。

创建相关账号

grant all privileges on *.* to ‘myha‘@‘192.168.56.%‘ identified by ‘oracle‘;

flush privileges;

构建: node20-> node21的复制

在主节点上执行sh /etc/masterha/init_vip.sh 绑定VIP。

MHA相关安装

注意目标机器上文件权限为: 600

MHA相关安装

MHA的安装分为mha manager节点和mha node节点。需文件不太一样。

安装文件可以从https://code.google.com/p/mysql-master-ha   下载。参考打包里的rpm包。

在一主一从的结构中建议两个节点都要安装manger, node包。

安装依赖包

yum install perl-devel -y

yum install perl-CPAN -y

yum install perl-Time-HiRes -y

yum install perl-DBD-MySQL   -y

yum install perl-Params-Validate   -y

yum install perl-Config-Tiny      -y

yum install perl-Log-Dispatch     -y

yum install perl-Parallel-ForkManager   -y

确认安装没有错误。

上述包如果yum源没有,可到 http://rpm.pbone.net/ 网站下载

安装mha软件:

rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm

rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm

或者通过源码安装的方式:

perl Makefile.PL

make && make install

配置

MHA的配置简单,只用在manager节点上进行配置限可。配置文件最少一个,一般可以分成两个这样为了减少一个manager节点管理多个集群时可以少写一点配置。但我们这里一主一从结构,建议配置文件配置完毕后,放置到两台机器上都是一样的。

配置文件如下:

全局级配置文件:/etc/masterha/masterha_default.conf

#MySQL的用户和密码

user=myha

password=oracle

#系统ssh用户

ssh_user=root

#复制用户

repl_user=repl

repl_password= oracle

#监控

ping_interval=1

#shutdown_script=""

#切换调用的脚本

master_ip_failover_script= /etc/masterha/master_ip_failover

master_ip_online_change_script= /etc/masterha/master_ip_online_change

说明shutdown_script 主要用来设置在master进行切换时,要执行的脚本动作,这个动作,可以设置把机器关了来防止脑列,也可以用做一些其它动作(前提那台机器还活着)。

cat /etc/masterha/app1.conf

[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=node10

master_binlog_dir = /data/mysql/3306/logs

candidate_master = 1

check_repl_delay = 0     #用防止master故障时,切换时slave有延迟,卡在那里切不过来。

[server2]

hostname=node5

master_binlog_dir=/data/mysql/3306/logs

candidate_master=1

check_repl_delay=0

配置文件测试

测试ssh ok

# masterha_check_ssh --global_conf=/etc/masterha/masterha_default.conf --conf=/etc/masterha/app1.conf

确认可以看到所有的服务器上ssh测试通过。

查看是不是具备跑masterha_manger, 主从结构是不是OK之类。

#masterha_check_repl --global_conf=/etc/masterha/masterha_default.conf --conf=/etc/masterha/app1.conf

最终可以看到现有master/slave结构。

app1.conf
-------------------------------------------------------------------------------------
[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=node10
master_binlog_dir = /data/mysql/mysql3306/logs
candidate_master = 1
check_repl_delay = 0 #用防止master故障时,切换时slave有延迟,卡在那里切不过来。

[server2]
hostname=node5
master_binlog_dir=/data/mysql/mysql3306/logs
candidate_master=1
check_repl_delay=0

-------------------------------------------------------------------------------------

drop_vip.sh
-------------------------------------------------------------------------------------
vip="192.168.1.100/32"
/sbin/ip addr del $vip dev eth0

-------------------------------------------------------------------------------------

init_vip.sh
-------------------------------------------------------------------------------------
vip="192.168.1.100/32"
/sbin/ip addr add $vip dev eth0

-------------------------------------------------------------------------------------

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.1.100";
my $if = "eth0";
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.1.100";
my $if = "eth0";

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;
};
if ([email protected]) {
warn "Got Error: [email protected]\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";
print current_time_us() . "Add vip $vip on $if..\n";
# create_app_user($new_master_handler);
&add_vip();
$new_master_handler->enable_log_bin_local();
$new_master_handler->disconnect();

## Update master ip on the catalog database, etc
$exit_code = 0;
};
if ([email protected]) {
warn "Got Error: [email protected]\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {

# do nothing
exit 0;
}
else {
&usage();
exit 1;
}
}

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;
}

-------------------------------------------------------------------------------------

masterha_default.conf
-------------------------------------------------------------------------------------
[server default]
#MySQL的用户和密码
user=wubx
password=wubxwubx

#系统ssh用户
ssh_user=root

#复制用户
repl_user=repl
repl_password= repl4slave

#监控
ping_interval=1
#shutdown_script=""

#切换调用的脚本
master_ip_failover_script= /etc/masterha/master_ip_failover
master_ip_online_change_script= /etc/masterha/master_ip_online_change

-------------------------------------------------------------------------------------

时间: 2024-10-13 03:09:09

MHA 一主一从搭建的相关文章

mysql之 mysql 5.6不停机双主一从搭建(活跃双主一从基于日志点复制)

环境说明:版本 version 5.6.25-log 主1库ip: 10.219.24.25主2库ip: 10.219.24.22从1库ip:10.219.24.26os 版本: centos 6.7已安装热备软件:xtrabackup 防火墙已关 双主一从架构图 补充:主从复制原理: http://blog.csdn.net/zhang123456456/article/details/72972701mysql 5.6安装 :http://blog.csdn.net/zhang1234564

MySQL MHA高可用环境搭建

一.安装MHA基本环境 1. 安装MHA node (1) 基本环境说明,本文参考互联网文章学习,搭建MHA与测试如下. 参考文档:http://www.cnblogs.com/xuanzhi201111/p/4231412.html 角色                IP地址            主机名    =============================================    Master              192.168.1.121     node

13、mha高可用架构搭建

各节点架构: 192.168.1.20(mysql5.5) master主库 192.168.1.21(mysql5.5) slave1,目标:主库宕可提升为主库 192.168.1.22(mysql5.5) slave2,目标:主库宕不可提升为主库 192.168.1.25(percona5.6) slave3.mha-manager.binlog server,目标:主库宕不可提升为主库 配置各节点ssh信任,在其中一台执行: # cd ~/.ssh # cat id_rsa.pub > a

mysql MHA集群的搭建

MHA集群介绍与安装 MHA ( Master High Availability ) – 由日本 DeNA 公司 youshimaton (现就职于 Facebook 公司)开发 – 是一套优秀的作为 MySQL 高可用性环境下故障切换和主从提升的高可用软件. – 目前在 MySQL 高可用方面是一个相对成熟的解决方案. – 在 MySQL 故障切换过程中, MHA 能做到在 0~30 秒之内自动完成数据库的故障切换操作 – 并且在进行故障切换的过程中, MHA 能在最大程度上保证数据的一致性

mysql 架构篇系列 2 复制架构一主一从搭建(异步复制)

一. 环境准备 1.1 主库环境(172.168.18.201) 环境 说明 查看脚本 操作系统版本 CentOS Linux release 7.4.1708 (Core) cat /etc/redhat-release 操作系统用户名和密码 root  js*2015 IP地址 172.168.18.201 ip addr 网关Gateway 172.168.18.1 cat /etc/sysconfig/network-scripts DNS 172.168.16.11 mysql 版本

各种报错,搭建Mysql MHA高可用集群时踩的各种坑

mha下载地址,需要翻墙 https://code.google.com/p/mysql-master-ha/ 管理软件 mha4mysql-manager-0.52-0.noarch.rpm 节点软件 mha4mysql-node-0.52-0.noarch.rpm 环境介绍 Centos6.7 X64 192.168.30.210 monitor 192.168.30.211 db1 (master) 192.168.30.212 db2  (备master) 192.168.30.213 

mysql之 mysql 5.6不停机主从搭建(一主一从基于GTID复制)

环境说明:版本 version 5.6.25-log 主库ip: 10.219.24.25从库ip:10.219.24.22os 版本: centos 6.7已安装热备软件:xtrabackup 防火墙已关 补充: 主从复制原理: http://blog.csdn.net/zhang123456456/article/details/72972701GTID 复制原理: http://blog.csdn.net/zhang123456456/article/details/73002216mys

mysql之 mysql 5.6不停机主从搭建(一主一从)

环境说明:版本 version 5.6.25-log 主库ip: 10.219.24.25从库ip:10.219.24.22os 版本: centos 6.7已安装热备软件:xtrabackup 防火墙已关 补充:mysql 5.6安装 :http://blog.csdn.net/zhang123456456/article/details/53608554xtrabackup 安装: http://blog.csdn.net/zhang123456456/article/details/728

MYSQL MHA

MYSQL MHA 简介: MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于 Facebook公司)开发,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件.在MySQL故障切换过程中,MHA能做到在 0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程中,MHA能在最大程度上保证数据的一致性,以达到真正意义上的高可用. 该软件由两部分组成:MHA