1. Environment preparation

1.1 System software List

No The host name role IP
1 mha1 master 192.168.8.211
2 mha2 slave 192.168.8.212
3 mha3 slave 192.168.8.213
4 mha1 monitoring 192.168.8.211
5 mha1 Monitoring the user mha
6 * Copy the user repli

1.2 the hostname

hostnamectl set-hostname mha1 --static
hostnamectl set-hostname mha2 --static
hostnamectl set-hostname mha3 --static
Copy the code

1.3 Network Settings

vi /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO="static" # DHCP static instead
ONBOOT="yes" # Enable this configuration on startupIPADDR = 192.168.8.211Static IP 192.168.8.212/192.168.8.213GATEWAY = 192.168.8.2# default gatewayNETMASK = 255.255.255.0# Subnet maskDNS1 = 114.114.114.114# the DNS configurationDNS2 = 8.8.8.8# the DNS configuration
Copy the code

1.4 Firewall Opening

Firewall-cmd --zone=public --list-ports// Check the status of all open ports firewall-cmd --zone=public --add-port=3306/ TCP --permanent firewall-cmd --zone=public --add-port=22/tcp --permanent firewall-cmd --reloadCopy the code

2. The mysql configuration

2.1 the mysql installation

yum install wget
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
yum localinstall mysql57-community-release-el7-8.noarch.rpm
yum repolist enabled | grep "mysql.*-community.*"
yum install -y  mysql-community-server
Copy the code

2.2 Checking the running Status of MySQL

systemctl status mysqld.service
Copy the code

2.3 Searching for and changing the Initial Password

grep "password" /var/log/mysqld.log
mysql -u root -p
Enter password:

mysql>set global validate_password_policy=0;
mysql>set global validate_password_length=6;
mysql>ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Copy the code

2.4 the mysql configuration

mha1

vi /etc/my.cnf
[mysqld]
log_bin
server-id=1
gtid_mode=on
enforce_gtid_consistency=on
Copy the code

mha2

vi /etc/my.cnf
[mysqld]
log_bin
server-id=2
gtid_mode=on
enforce_gtid_consistency=on
Copy the code

mha3

vi /etc/my.cnf
[mysqld]
log_bin
server-id=3
gtid_mode=on
enforce_gtid_consistency=on
Copy the code

2.5 restart mysql

systemctl restart mysqld
Copy the code

2.6 mha1 login to mysql and create primary/secondary replication user (repli)

mysql -uroot -p
mysql>use mysql;
mysql>select user,host from user;
mysql>set global validate_password_policy=0;
mysql>set global validate_password_length=6;
mysql> create user repli@The '%' identified by '123456';
mysql> grant all privileges on *.* to repli@The '%' identified by '123456';

Copy the code

2.7 Master slave replication is then performed on mHA2 and MHA3 machines

mysql -uroot -p
mysql> change master to master_host='192.168.8.211',master_user='repli',master_password='123456',master_auto_position=1;
mysql> start slave;
mysql> show slave status\G;
Copy the code

Mysql > create monitor user (mHA)

mysql -uroot -p
mysql>set global validate_password_policy=0;
mysql>set global validate_password_length=6;
mysql> create user mha@The '%' identified by '123456';
mysql> grant all privileges on *.* to mha@The '%' identified by '123456';

Copy the code

3 Password-free Login (*)

Vi /etc/hosts 192.168.8.211 mha1 192.168.8.212 mha2 192.168.8.213 mha3 ssh-keygen -t rsa // Confirm all the way to.ssh]# ssh-copy-id -i id_rsa.pub root@mha1
.ssh]# ssh-copy-id -i id_rsa.pub root@mha2
.ssh]# ssh-copy-id -i id_rsa.pub root@mha3
Copy the code

4 installed MHA

4.1 Installing an EPEL Source (*)

yum -y install epel-release
wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
rpm -Uvh remi-release-7.rpm
Copy the code

4.2 Installing a Node node (*)

yum install -y perl-DBD-MySQL ncftp perl-DBI.x86 wget https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58-0.el7.centos.noarch.rpm RPM - the ivh mha4mysql - node - 0.58-0. El7. Centos. Noarch. RPMCopy the code

4.3 Installing the Manager Node (MHA1)

yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager wget https://github.com/yoshinorim/mha4mysql-manager/releases/download/v0.58/mha4mysql-manager-0.58-0.el7.centos.noarch.rpm The RPM - the ivh mha4mysql - manager - 0.58-0. El7. Centos. Noarch. RPMCopy the code

5 Failover Script (MHA1)

vi /usr/local/bin/master_ip_failover
chmod 777 /usr/local/bin/master_ip_failover

vi /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
);
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # to add content part # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# $BRDC, $ifdev need to be changed
my $vip = '192.168.8.66';
my $brdc = '192.168.8.255';
my $ifdev = 'ens33';
my $key = '1';
my $ssh_start_vip = "/usr/sbin/ip addr add $vip/24 brd $brdc dev $ifdev label $ifdev:$key; /usr/sbin/arping -q -A -c 1 -I$ifdev $vip; iptables -F;";
my $ssh_stop_vip = "/usr/sbin/ip addr del $vip/24 dev $ifdev label $ifdev:$key";
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
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\ "`; }# 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\n";
}
Copy the code

6 MHA configuration file

6.1 MHA Configuration File – mha.conf(mha1)

mkdir -p /var/log/masterha

mkdir -p /etc/masterha

vi /etc/masterha/mha.conf
# # MHA configuration
[server default]
# monitor user
user=mha
# monitor user password
password=123456
SSH login user
ssh_user=root
# duplicate user
repl_user=repli
Duplicate the user password
repl_password=123456
# Working directory
manager_workdir=/etc/masterha
# log directory
manager_log=/var/log/masterha/manager.log
master_binlog_dir=/var/lib/mysql
remote_workdir=/data/log/masterha
secondary_check_script=masterha_secondary_check -s 192.168.8.211 -s 192.168.8.212 -s 192.168.8.213
ping_interval=3
master_ip_failover_script=/usr/local/ bin/master_ip_failover report_script = / script/masterha/send_master_failover_mail [server1] hostname = 192.168.8.211 Candidate_master =1 [server2] hostname=192.168.8.212 candidate_master=1 [server3] hostname=192.168.8.213 candidate_master=1Copy the code

6.1 Authentication Mechanism (MHA1)

masterha_check_ssh --conf /etc/masterha/mha.conf
masterha_check_repl --conf /etc/masterha/mha.conf
Copy the code

6.2 MHA – Startup (MHA1)

nohup masterha_manager --conf=/etc/masterha/mha.conf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/manager.log 2>&1 &

Copy the code

6.3 MHA: Observe the status

masterha_check_status --conf /etc/masterha/mha.conf
Copy the code

6.4 Manually Adding a VIP 66(MHA1)

Ifconfig ens33:1 192.168.8.66/24 [root@mha1. SSH]# ip a1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host LO valid_lft forever preferred_lft forever Inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0 C :29: F3: A6:8B BRD FF: FF: FF: FF: FF: FF INET 192.168.8.211/24 BRD 192.168.8.255 Scope Global noPrefixRoute ENS33 Valid_lft forever preferred_lft forever inet 192.168.8.66/24 BRD 192.168.8.255 scope Global Secondary ens33:1 VALID_Lft forever preferred_lft forever inet6 fd15:4ba5:5a2b:1008:c794:c9e0:1526:a08b/64 scope global noprefixroute dynamic valid_lft 86373sec preferred_lft 14373sec inet6 fe80::a82a:9fcf:2247:7771/64 scope link noprefixroute valid_lft forever preferred_lft foreverCopy the code

6.5 Demonstrate the effect of MHA

[root@mha1 .ssh]#systemctl stop mysqld
[root@mha2 .ssh]# ip a1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host LO valid_lft forever preferred_lft forever Inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0 C: 29:36:D4:4e BRD FF :ff:ff:ff: FF: FF inet 192.168.8.212/24 BRD 192.168.8.255 Scope Global ENS33 VALID_lft forever Preferred_lft forever inet 192.168.8.66/24 BRD 192.168.8.255 scope Global Secondary ens33:1 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe36:d44e/64 scope link valid_lft forever preferred_lft foreverCopy the code