准备服务器阶段
准备四台虚拟机,关闭防火墙及selinux
192.168.181.130 manager
192.168.181.142 mysql主
192.168.181.168 slave1
192.168.181.169 slave2
免密操作
130,142,168,169
ssh-keygen
ssh-copy-id
mysql安装及配置文件的修改
142,168,169
yum -y install mariadb mariadb-server
142(master)配置文件的修改
vim /etc/my.cnf
168,169(slave1,slave2)的修改
开启mariadb
systemctl start mariadb
做主从复制
142(master)
168,169(slave1,slave2)
142,168,169(master,slaves)
grant all privileges on . to ‘tom’@‘192.168.181.%’ identified by ‘123’;
时间同步
130,142,168,169(all)
node节点安装
130,142,168,169(all)
yum -y install perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-DBD-MySQL perl-devel perl-CPAN
上传mha的node源码包
mha4mysql-node-0.57.tar.gz (可去自行下载)
mkdir /etc/mha
tar zxf mha4mysql-node-0.57.tar.gz -C /etc/mha
mv /etc/mha/mha4mysql-node-0.57/ /etc/mha/node
cd /etc/mha/node
perl Makefile.PL
make && make install
注意:编译安装之前做好时间同步,不然会报错
manager节点安装
130(manager)
yum -y install epel-release --nogpgcheck
yum -y install perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes
上传mha的manager的源码包
mha4mysql-manager-0.57.tar.gz (自行下载)
tar zxf mha4mysql-manager-0.57.tar.gz -C /etc/mha
mv /etc/mha/mha4mysql-manager-0.57/ /etc/mha/manager
cd /etc/mha/manager
perl Makefile.PL
make && make install
修改manager的配置文件
[server default]
manager_workdir=/etc/mha/app1
manager_log=/etc/mha/app1/manager.log
master_binlog_dir="/var/lib/mysql"
remote_workdir=/etc/mha/app1
master_ip_failover_script=/etc/mha/master_ip_failover
master_ip_online_change_script=/etc/mha/master_ip_online_change
report_script=/etc/mha/send_report
user=tom
password=123
repl_user=tom
repl_password=123
ping_interval=1
secondary_check_script= masterha_secondary_check -s 192.168.181.168 -s 192.168.181.169
[server1]
hostname=192.168.181.142
port=3306
ssh_port=22
[server2]
hostname=192.168.181.168
port=3306
ssh_port=22
candidate_master=1
check_repl_delay=0
[server3]
hostname=192.168.181.169
port=3306
no_master=1
ssh_port=22
脚本
168,169(slaves)
#!/bin/bash
user=root
passwd=123
port=3306
log_dir=’/var/lib/mysql/’
work_dir=’/var/lib/mysql/logs1’
purge=’/usr/local/bin/purge_relay_logs’
if [ ! -d $log_dir ]
then
mkdir $log_dir -p
fi
$purge --user=$user --password=$passwd --disable_relay_log_purge --port= $port --host=localhost --workdir=$work_dir >> $log_dir/purge_relay_logs.log 2>&1
测试
purge_relay_logs --user=root --host=localhost --port=3306 --password=123 -disable_relay_log_purge --workdir=/var/lib/mysql/
做个定时任务
130,142,168,169(all)
自动切换脚本
vim /etc/mha/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.181.111/24';
my $key = '0';
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$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,
);
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";
}
手动切换脚本
vim /etc/mha/master_ip_online_change
#!/usr/bin/env perl
use strict;
use warnings FATAL =>'all';
use Getopt::Long;
my $vip = '192.168.181.111/24'; # Virtual IP
my $key = "0";
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
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";
}
报警脚本
vim /etc/mha/send_report
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use Mail::Sender;
use Getopt::Long;
#new_master_host and new_slave_hosts are set only when recovering master succeeded
my ( $dead_master_host, $new_master_host, $new_slave_hosts, $subject, $body );
my $smtp='smtp.qq.com';
my $mail_from='[email protected]';
my $mail_user='[email protected]';
my $mail_pass='xxxxxxxx';
my $mail_to=['[email protected]'];
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;
manager检测及开启
130(manager)
ssh免密检测
/etc/mha/manager/bin/masterha_check_ssh --conf=/etc/mha/app1.cnf
检测mysql主从是否正常
/etc/mha/manager/bin/masterha_check_repl --conf=/etc/mha/app1.cnf
出现以下情况说明正常
142(master)
ifconfig ens33:0 192.168.181.111 netmask 255.255.255.0
130(manager)
启动manager
nohup /etc/mha/manager/bin/masterha_manager --conf=/etc/mha/app1.cnf --ignore_last_failover >/tmp/mha_manager.log < /dev/null 2>&1 &
查看主的状态
/etc/mha/manager/bin/masterha_check_status --conf=/etc/mha/app1.cnf
测试
关闭142的mariadb
systemctl stop mariadb
在130上查看manager的日志
tail -100f /etc/mha/app1/manager.log
168(old slave→new master)
Vip漂移到168机器,成为了新主,然后启动旧主142的mariadb,为142和168做主从
142(old master → new slave)
130(manager)
修改配置文件
启动manager
nohup /etc/mha/manager/bin/masterha_manager --conf=/etc/mha/app1.cnf --ignore_last_failover >/tmp/mha_manager.log < /dev/null 2>&1 &
查看状态,此时主为168
/etc/mha/manager/bin/masterha_check_status --conf=/etc/mha/app1.cnf
此时再去关闭168的mariadb,去142的服务器上查看ip,就可以看到VIP又回到了142,142又变回了主
之后再去开启168的mariadb,与142做主从,重复以上步骤,看vip是否漂移就欧克啦!