一.准备一套搭建好的MHA系统
步骤,参考下面地址:
http://blog.itpub.net/15412087/viewspace-2153879/
二.下载keepalived
下载地址:http://www.keepalived.org/download.html
上传至mysql1(master) 和 mysql3(candidate)
三.安装keepalived
1.安装依赖包
yum install -y gcc* openssl-devel popt-devel
yum install ipvsadm
2.编译安装
./configure --prefix=/usr/local/keepalived
make && make install
3.拷贝文件并且加入系统服务
cp /tmp/keepalived-2.0.4/keepalived/etc/init.d/keepalived /etc/init.d/
chmod +x /etc/init.d/keepalived
mkdir -p /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /tmp/keepalived-2.0.4/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
四.配置文件
vim /etc/keepalived/keepalived.conf
1.主节点
-
global_defs {
-
notification_email {
-
277106621@qq.com
-
}
-
notification_email_from 305854332@qq.com
-
smtp_server smtp.qq.com
-
smtp_connect_timeout 30
-
router_id MYSQL_MHA
-
vrrp_skip_check_adv_addr
-
vrrp_strict
-
vrrp_garp_interval 0
-
vrrp_gna_interval 0
-
}
-
vrrp_instance VI_1 {
-
state BACKUP
-
interface eth0
-
virtual_router_id 51
-
priority 100
-
advert_int 1
-
nopreempt
-
authentication {
-
auth_type PASS
-
auth_pass 1111
-
}
-
virtual_ipaddress {
-
192.168.1.207/24
-
}
-
}
2.从节点
-
! Configuration File
for keepalived
-
-
global_defs {
-
notification_email {
-
277106621@qq
.com
-
}
-
notification_email_from 305854332@qq
.com
-
smtp_server smtp
.qq
.com
-
smtp_connect_timeout 30
-
router_id MYSQL_MHA
-
vrrp_skip_check_adv_addr
-
vrrp_strict
-
vrrp_garp_interval 0
-
vrrp_gna_interval 0
-
}
-
-
vrrp_instance VI_1 {
-
state BACKUP
-
interface eth0
-
virtual_router_id 51
-
priority 90
-
advert_int 1
-
nopreempt
-
authentication {
-
auth_type PASS
-
auth_pass 1111
-
}
-
virtual_ipaddress {
-
192
.168
.1
.207/24
-
}
-
}
这里master服务器的state不配置成MASTER,且配置nopreempt,是期望在master宕机后再恢复时,不主动将MASTER状态抢过来,避免MySQL服务的波动。
这里没有配置vrrp_script,在后面会让mha实现vip的自动漂移。
参数文件说明:
https://blog.csdn.net/weiyuefei/article/details/78106202
五.启动keepalived服务
先启动主节点,后启动从节点keepalived服务
# service keepalived start
Starting keepalived: [ OK ]
# service keepalived status
keepalived (pid 30030) is running...
完成后,在主节点发现vip
测试:停止master服务器keepalived,检查VIP是否切换到备选master服务器(用ip addr命令验证即可)
六.修改MHA ip 迁移脚本
-
# cat /etc/mha/mhamanager/app1/scripts/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.168.1.207/24'
;
-
my $ssh_start_vip
=
"/etc/init.d/keepalived start"
;
-
my $ssh_stop_vip
=
"/etc/init.d/keepalived stop"
;
-
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
;
-
}
-
elsif
( $command
eq
"start"
) {
-
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"
;
-
exit 0
;
-
}
-
else {
-
&usage
(
)
;
-
exit 1
;
-
}
-
}
-
sub start_vip
(
) {
-
`ssh $ssh_user\@$new_master_host \
" $ssh_start_vip \"
`
;
-
}
-
sub stop_vip
(
) {
-
return 0 unless
(
$ssh_user
)
;
-
`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"
;
-
}
-
# cat /etc/mha/mhamanager/app1/scripts/master_ip_online_change
-
#!/usr/bin/env perl
-
use strict;
-
use warnings FATAL =>'all';
-
use Getopt::Long;
-
my $vip = '192.168.1.207/24'; # Virtual IP
-
my $ssh_start_vip = "/etc/init.d/keepalived start";
-
my $ssh_stop_vip = "/etc/init.d/keepalived stop";
-
my $exit_code = 0;
-
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 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 "\n\n\n***************************************************************\n";
-
print "Disabling the VIP - $vip on old master: $orig_master_host\n";
-
print "***************************************************************\n\n\n\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 "\n\n\n***************************************************************\n";
-
print "Enabling the VIP - $vip on new master: $new_master_host \n";
-
print "***************************************************************\n\n\n\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 $orig_master_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 $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_failover -command=start|stop|stopssh|status -orig_master_host=host -orig_master_ip=ip -
-
orig_master_port=po
-
rt -new_master_host=host -new_master_ip=ip -new_master_port=port\n";
-
}
七.配置report脚本
-
# cat /etc/mha/mhamanager/app1/scripts/send_report
-
#!/usr/bin/perl
-
# Copyright (C) 2011 DeNA Co.,Ltd.
-
#
-
# This program is free software; you can redistribute it and/or modify
-
# itunder 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.,
-
# 51Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-
## Note: This is a sample script and is notcomplete. Modify the script based on your environment.
-
use strict;
-
use warnings FATAL => 'all';
-
use Mail::Sender;
-
use Getopt::Long;
-
#new_master_host and new_slave_hosts areset only when recovering master succeeded
-
my ( $dead_master_host, $new_master_host, $new_slave_hosts,$subject, $body );
-
my $smtp='smtp.163.com';
-
my $mail_from='www26454285@163.com';
-
my $mail_user='www26454285@163.com';
-
my $mail_pass='26454285wwj';
-
my $mail_to=['277106621@qq.com'];
-
GetOptions(
-
'orig_master_host=s' => \$dead_master_host,
-
'new_master_host=s' =>\$new_master_host,
-
'new_slave_hosts=s' =>\$new_slave_hosts,
-
'subject=s' =>\$subject,
-
'body=s' => \$body,
-
);
-
mailToContacts($smtp,$mail_from,$mail_user,$mail_pass,$mail_to,$subject,$body);
-
sub mailToContacts {
-
my ( $smtp, $mail_from, $user, $passwd, $mail_to, $subject, $msg ) = @_;
-
open my $DEBUG, "> /tmp/monitormail.log"
-
or die "Can't open the debug file:$!\n";
-
my $sender = new Mail::Sender {
-
ctype => 'text/plain;charset=utf-8',
-
encoding => 'utf-8',
-
smtp => $smtp,
-
from => $mail_from,
-
auth => 'LOGIN',
-
TLS_allowed => '0',
-
authid => $user,
-
authpwd => $passwd,
-
to => $mail_to,
-
subject => $subject,
-
debug => $DEBUG
-
};
-
$sender->MailMsg(
-
{ msg => $msg,
-
debug => $DEBUG
-
}
-
)or print $Mail::Sender::Error;
-
return 1;
-
}
-
# Do whatever you want here
-
exit 0;
八.启动mha
nohup masterha_manager --conf=
/etc/mha/mhamanager/app1/conf/app1.cnf
--ignore_last_failover
--remove_dead_master_conf
> /tmp/mha_manager.log < /dev/null 2>&1 &
检查MHA状态
# masterha_check_status --conf=/etc/mha/mhamanager/app1/conf/app1.cnf
测试ssh
masterha_check_ssh --conf=/etc/mha/mhamanager/app1/conf/app1.cnf
测试主从复制
masterha_check_repl --conf=/etc/mha/mhamanager/app1/conf/app1.cnf
九.测试failover
1.关闭master节点
# pkill mysql
2.现象
mha服务停止
master节点keepalived服务停止
vip 迁移至backup节点
/etc/mha/mhamanager/app1/conf/app1.cnf [server1] 内容被删除
master 迁移至109.115.1.205
3.将原master恢复
-
重新启动原主库
mysqld_safe --defaults-file=/etc/my5.7_3306.cnf --user=mysql &
- 设置同步信息
CHANGE MASTER TO MASTER_HOST='192.168.1.205', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000009', MASTER_LOG_POS=234,MASTER_USER='REPL_USER', MASTER_PASSWORD='repl1234';
start slave;
- 开启原master keepalived
service keepalived status
- 重新添加mha配置文件原master信息
[server1]
hostname=192.168.1.203
port=3306
ssh_port=22
candidate_master=1
master_binlog_dir=/mysql/mysql3306/mysql3306
- 启动mha
nohup masterha_manager --conf=
/etc/mha/mhamanager/app1/conf/app1.cnf
--ignore_last_failover
--remove_dead_master_conf
> /tmp/mha_manager.log < /dev/null 2>&1 &
# masterha_check_status --conf=/etc/mha/mhamanager/app1/conf/app1.cnf
app1 (pid:32584) is running(0:PING_OK), master:192.168.1.205
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/15412087/viewspace-2157231/
,如需转载,请注明出处,否则将追究法律责任。