一、MHA 简介
二、前期环境部署
五台主机:server01 server02 server03 server04 server05
配置映射所有主机
[root@server05 ~]# vim /etc/hosts
192.168.200.111 server01
192.168.200.112 server02
192.168.200.113 server03
192.168.200.114 server04
192.168.200.115 server05
传给其他主机
scp /etc/hosts 192.168.200.111:/etc/
关闭防火墙
systemctl stop firewalld
setenforce 0
iptables -F
二、前期环境部署
五台主机:server01 server02 server03 server04 server05
配置映射所有主机
[root@server05 ~]# vim /etc/hosts
192.168.200.111 server01
192.168.200.112 server02
192.168.200.113 server03
192.168.200.114 server04
192.168.200.115 server05
传给其他主机
scp /etc/hosts 192.168.200.111:/etc/
关闭防火墙
systemctl stop firewalld
setenforce 0
iptables -F
安装MHA node
--------------------------------------------------
rpm -ivh epel-release-latest-7.noarch.rpm
yum install -y perl-DBD-MySQL.x86_64 perl-DBI.x86_64 perl-CPAN perl-ExtUtils-CBuilder
perl-ExtUtils-MakeMaker
----------------------------------------------------
所有主机安装NODE
---------------------------------
tar xf mha4mysql-node-0.56.tar.gz
cd mha4mysql-node-0.56/
perl Makefile.PL
make && make install
------------------------------------------------
--------------------------------------------------
rpm -ivh epel-release-latest-7.noarch.rpm
yum install -y perl-DBD-MySQL.x86_64 perl-DBI.x86_64 perl-CPAN perl-ExtUtils-CBuilder
perl-ExtUtils-MakeMaker
----------------------------------------------------
所有主机安装NODE
---------------------------------
tar xf mha4mysql-node-0.56.tar.gz
cd mha4mysql-node-0.56/
perl Makefile.PL
make && make install
------------------------------------------------
给第五台机器,安装Manger
yum install -y perl perl-Log-Dispatch perl-Parallel-ForkManager perl-DBD-MySQL perl-DBI
perl-Time-HiRes
yum -y install perl-Config-Tiny-2.14-7.el7.noarch.rpm
----------------------------------------------------------------
安装bao
tar xf mha4mysql-manager-0.56.tar.gz
cd mha4mysql-manager-0.56/
perl Makefile.PL
make && make install
-----------------------------------------------------------
进行密钥对验证
yum install -y perl perl-Log-Dispatch perl-Parallel-ForkManager perl-DBD-MySQL perl-DBI
perl-Time-HiRes
yum -y install perl-Config-Tiny-2.14-7.el7.noarch.rpm
----------------------------------------------------------------
安装bao
tar xf mha4mysql-manager-0.56.tar.gz
cd mha4mysql-manager-0.56/
perl Makefile.PL
make && make install
-----------------------------------------------------------
进行密钥对验证
5号:ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.200.111
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.200.112
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.200.113
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.200.114
ssh server01
ssh server02
ssh server03
ssh server04
其余各对其他机器进行密钥,除第五台,同上
---------------------------------------------------------------------------------
安装mysql
yum -y install mariadb mariadb-server mariadb-devel
systemctl start mariadb
netstat -lnpt | grep :3306
------------------------------------------------
mysqladmin -u root password 123456
----------------------------------------------------
修改主机配置文件
第一台
vim /etc/my.cnf
[mysqld]
server-id = 1
log-bin=master-bin
log-slave-updates=true
relay_log_purge=0
systemctl restart mariadb
第二台
vim /etc/my.cnf
[mysqld]
server-id=2
log-bin=master-bin
log-slave-updates=true
relay_log_purge=0
systemctl restart mariadb
第三台
vim /etc/my.cnf
[mysqld]
server-id=3
log-bin=mysql-bin
relay-log=slave-relay-bin
log-slave-updates=true
relay_log_purge=0
systemctl restart mariadb
第四台
vim /etc/my.cnf
[mysqld]
server-id=4
log-bin=mysql-bin
relay-log=slave-relay-bin
log-slave-updates=true
relay_log_purge=0
systemctl restart mariadb
--------------------------------------------------------------
四台主机分别复制授权(进入mysql)
grant replication slave on *.* to 'repl'@'192.168.200.%' identified by '123456';
flush privileges;
看第一台主机
MariaDB [(none)]> show master status;
其余主机
stop slave;
CHANGE MASTER TO
MASTER_HOST='192.168.200.111',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='master-bin.000001',
MASTER_LOG_POS=474;
start slave;
show slave statusG------------------------------------------------------------
给三台主机分别输入
set global read_only=1
在前4台主机上配置
grant all privileges on *.* to 'root'@'192.168.200.%' identified by '123456';
flush privileges;
grant all privileges on *.* to 'root'@'自己主机名' identified by '123456';
flush privileges;
----------------------------------------------------------------------------------------
第五台机
创建目录,解压
mkdir /etc/masterha
cp mha4mysql-manager-0.56/samples/conf/app1.cnf /etc/masterha
修改app1.cnf配置文件
vim /etc/masterha/app1.cnf--------------------------------------------------------------
[server default]
#设置 manager 的工作日志
manager_workdir=/var/log/masterha/app1
#设置 manager 的日志,这两条都是默认存在的
manager_log=/var/log/masterha/app1/manager.log
#设置 master 默认保存 binlog 的位置,以便 MHA 可以找到 master 日志
master_binlog_dir=/var/lib/mysql
#设置自动 failover 时候的切换脚本
master_ip_failover_script= /usr/local/bin/master_ip_failover
#设置 mysql 中 root 用户的密码
password=123456
QQ:981343876 16 / 50 Tel:13651028933
user=root
#ping 包的时间间隔
ping_interval=1
#设置远端 mysql 在发生切换时保存 binlog 的具体位置
remote_workdir=/tmp
#设置复制用户的密码和用户名
repl_password=123456
repl_user=repl
[server1]
hostname=server01
port=3306
[server2]
hostname=server02
candidate_master=1
port=3306
check_repl_delay=0
[server3]
hostname=server03
port=3306
[server4]
hostname=server04
port=3306
----------------------------------------------------------------------------------------
配置故障转移脚本
vim /usr/local/bin/master_ip_failover
#!/usr/bin/env perl ---------------------------------------------------------------------------------------------
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.200.111
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.200.112
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.200.113
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.200.114
ssh server01
ssh server02
ssh server03
ssh server04
其余各对其他机器进行密钥,除第五台,同上
---------------------------------------------------------------------------------
安装mysql
yum -y install mariadb mariadb-server mariadb-devel
systemctl start mariadb
netstat -lnpt | grep :3306
------------------------------------------------
mysqladmin -u root password 123456
----------------------------------------------------
修改主机配置文件
第一台
vim /etc/my.cnf
[mysqld]
server-id = 1
log-bin=master-bin
log-slave-updates=true
relay_log_purge=0
systemctl restart mariadb
第二台
vim /etc/my.cnf
[mysqld]
server-id=2
log-bin=master-bin
log-slave-updates=true
relay_log_purge=0
systemctl restart mariadb
第三台
vim /etc/my.cnf
[mysqld]
server-id=3
log-bin=mysql-bin
relay-log=slave-relay-bin
log-slave-updates=true
relay_log_purge=0
systemctl restart mariadb
第四台
vim /etc/my.cnf
[mysqld]
server-id=4
log-bin=mysql-bin
relay-log=slave-relay-bin
log-slave-updates=true
relay_log_purge=0
systemctl restart mariadb
--------------------------------------------------------------
四台主机分别复制授权(进入mysql)
grant replication slave on *.* to 'repl'@'192.168.200.%' identified by '123456';
flush privileges;
看第一台主机
MariaDB [(none)]> show master status;
其余主机
stop slave;
CHANGE MASTER TO
MASTER_HOST='192.168.200.111',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='master-bin.000001',
MASTER_LOG_POS=474;
start slave;
show slave statusG------------------------------------------------------------
给三台主机分别输入
set global read_only=1
在前4台主机上配置
grant all privileges on *.* to 'root'@'192.168.200.%' identified by '123456';
flush privileges;
grant all privileges on *.* to 'root'@'自己主机名' identified by '123456';
flush privileges;
----------------------------------------------------------------------------------------
第五台机
创建目录,解压
mkdir /etc/masterha
cp mha4mysql-manager-0.56/samples/conf/app1.cnf /etc/masterha
修改app1.cnf配置文件
vim /etc/masterha/app1.cnf--------------------------------------------------------------
[server default]
#设置 manager 的工作日志
manager_workdir=/var/log/masterha/app1
#设置 manager 的日志,这两条都是默认存在的
manager_log=/var/log/masterha/app1/manager.log
#设置 master 默认保存 binlog 的位置,以便 MHA 可以找到 master 日志
master_binlog_dir=/var/lib/mysql
#设置自动 failover 时候的切换脚本
master_ip_failover_script= /usr/local/bin/master_ip_failover
#设置 mysql 中 root 用户的密码
password=123456
QQ:981343876 16 / 50 Tel:13651028933
user=root
#ping 包的时间间隔
ping_interval=1
#设置远端 mysql 在发生切换时保存 binlog 的具体位置
remote_workdir=/tmp
#设置复制用户的密码和用户名
repl_password=123456
repl_user=repl
[server1]
hostname=server01
port=3306
[server2]
hostname=server02
candidate_master=1
port=3306
check_repl_delay=0
[server3]
hostname=server03
port=3306
[server4]
hostname=server04
port=3306
----------------------------------------------------------------------------------------
配置故障转移脚本
vim /usr/local/bin/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.200.100'; # 写入VIP
my $key = "1"; #非keepalived方式切换脚本使用的
my $ssh_start_vip = "/sbin/ifconfig ens32:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens32:$key down"; #那么这里写服务的开关命令
$ssh_user = "root";
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 " IN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip=== ";
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 "Disabling the VIP on old master: $orig_master_host ";
# &stop_vip();
# $exit_code = 0;
#};
eval {
print "Disabling the VIP on old master: $orig_master_host ";
#my $ping=`ping -c 1 10.0.0.13 | grep "packet loss" | awk -F',' '{print $3}' | awk '{print $1}'`;
#if ( $ping le "90.0%"&& $ping gt "0.0%" ){
#$exit_code = 0;
#}
#else {
&stop_vip();
# updating global catalog, etc
$exit_code = 0;
#}
};
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.200.100'; # 写入VIP
my $key = "1"; #非keepalived方式切换脚本使用的
my $ssh_start_vip = "/sbin/ifconfig ens32:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens32:$key down"; #那么这里写服务的开关命令
$ssh_user = "root";
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 " IN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip=== ";
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 "Disabling the VIP on old master: $orig_master_host ";
# &stop_vip();
# $exit_code = 0;
#};
eval {
print "Disabling the VIP on old master: $orig_master_host ";
#my $ping=`ping -c 1 10.0.0.13 | grep "packet loss" | awk -F',' '{print $3}' | awk '{print $1}'`;
#if ( $ping le "90.0%"&& $ping gt "0.0%" ){
#$exit_code = 0;
#}
#else {
&stop_vip();
# updating global catalog, etc
$exit_code = 0;
#}
};
if ($@) {
warn "Got Error: $@ ";
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 "Enabling the VIP - $vip on the new master - $new_master_host ";
&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 ";
`ssh $ssh_user@$orig_master_ip " $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 $ssh_user@$new_master_host " $ssh_start_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 "; }
-------------------------------------------------------------------------------------------------------------------------------
warn "Got Error: $@ ";
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 "Enabling the VIP - $vip on the new master - $new_master_host ";
&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 ";
`ssh $ssh_user@$orig_master_ip " $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 $ssh_user@$new_master_host " $ssh_start_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 {
"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 "; }
-------------------------------------------------------------------------------------------------------------------------------
在中间server 三台机器做清除
mysql -uroot -p123456 -e 'set global relay_log_purge=0;'
=====================================================
查看ssh 通信状态
[root@server05 ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf
查看整个集群的状态是否OK
[root@server05 ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
mysql -uroot -p123456 -e 'set global relay_log_purge=0;'
=====================================================
查看ssh 通信状态
[root@server05 ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf
查看整个集群的状态是否OK
[root@server05 ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
--------------------------------------------------------------------------------------------------------
模拟故障==============================
=进行vip分配
5号机
grep -n 'master_ip_failover_script' /etc/masterha/app1.cnf
9:master_ip_failover_script= /usr/local/bin/master_ip_failover
在第一台master主机上查看vip
第五台开启监控
nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dea
d_master_conf --ignore_last_failover< /dev/null >/var/log/masterha/app1/manager.log 2>&1
&
查看监控是否正常
masterha_check_status --conf=/etc/masterha/app1.cnf
cat /var/log/masterha/app1/manager.log
模拟故障
关闭主master一号机 mariadb
查看vip netstat -lnpt|grep 3306
同时看server机上mysql里状态
show slave statusG
故障修复
开启mariadb
查看端口
进入Mysql里
>stop slave;
>CHANGE MASTER TO MASTER_HOST='192.168.200.112',MASTER_USER='repl',MASTER_PASSWORD='123456';
>start slave;
>show slave statusG
>exit
-----------------------------------------------
换第五台机器
vim /etc/masterha/app1.cnf
[server01]
hostname=server01
port=3306
查看集群状态
masterha_check_repl --conf=/etc/masterha/app1.cnf
注意 主从master变位置了
再将第二台机器,关闭开启
同上修复
vim /etc/masterha/app1.cnf
[server2]
hostname=server02
candidate_master=1
port=3306
check_repl_delay=0
-----------------------------------------------------------------