Machine role assignment

Machine 1: Master Machine 2: Slave1 Standby Master machine 3: Slave2 Machine 4: Manager

Configure the encryption-free login

Machine four: Generate the key

ssh-keygen -t rsa
#Press enter until the following image appears
Copy the code
<! -- Note: all three machines are executed -->Copy the code

Machine four: Sends the public key to machine one, machine two, machine three (including the local machine)

Cat/root /. SSH/id_rsa. Pub | SSH [email protected] 'cat > >. SSH/authorized_keys';#Enter yes
#Enter the server passwordCat/root /. SSH/id_rsa. Pub | SSH [email protected] 'cat > >. SSH/authorized_keys'; Cat/root /. SSH/id_rsa. Pub | SSH [email protected] 'cat > >. SSH/authorized_keys';Copy the code

<! Note: same as above, machine 1, machine 2, machine 3 also send public key to other machines (excluding machine 4).Copy the code

Enable bin_log and gtid_mode

The second machine

vim /etc/my.cnf
Copy the code

#If the secondary library is set to read-only, the read-only master cannot write to the configuration file because it may become master. The non-standby master can write to the configuration file
set global read_only=1;

#Disable automatic cleanup of transfer logs because the MHA recovery from the library depends on information about transfer logs
set global relay_log_purge=off;

#Restarting the mysql service
systemctl restart mysqld
Copy the code
<! Set gtid_mode to server id=3 -->Copy the code

Building Lord

Machine 1: Create a master/slave replication account and an MHA management account

#Creating a replication user
create user replication identified by "kaikeba100";

#The user used for replication
grant all privileges on *.* to replication@'%' identified by 'kaikeba100';

#Monitor the user
grant all privileges on *.* to mha@'%' identified by 'kaikeba100';   

flush privileges;
Copy the code
<! Create master/slave account and mHA management account for mHACopy the code

Machine 2 and machine 3 copy MySQL from machine 1

Change the master to master_host = '192.168.43.175, master_user =' replication ', master_password='kaikeba100',master_auto_position=1; start slave; show slave status \GCopy the code

Install the epl 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

Install the MHA manager and node

Install Node on all machines

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

Install Manager on machine four

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

Configuration MHA

Machine 4: Create a working directory

mkdir /etc/masterha
mkdir -p /var/log/masterha/
Copy the code

Machine 4: Create failover scripts

vim /usr/local/bin/master_ip_failover
Copy the code
<! Copy and paste the following -->Copy the code
#! /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 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# $vip,$brdc,$ifdev(Nic) Needs to be modifiedMy VIP = $' 192.168.43.88 '; My $BRDC = '192.168.43.255'; my $ifdev = 'eth1'; 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
chmod 777 /usr/local/bin/master_ip_failover
Copy the code

Machine 4: Create an MHA profile

vim /etc/masterha/mha.conf
Copy the code
<! Copy and paste the following -->Copy the code
## MHA configuration
[server default]
#Monitoring the user
user=mha
#Monitoring User Passwords
password=kaikeba100
#SSH Login User
ssh_user=root
#Copy the user
repl_user=replication
#Copy user password
repl_password=kaikeba100
#Working directory
manager_workdir=/etc/masterha
#Log directorymanager_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.43.175 -s 192.168.43.99 ping_interval=3 master_ip_failover_script=/usr/local/bin/master_ip_failover report_script=/script/masterha/send_master_failover_mail [server2] hostname=192.168.43.99 candidate_master=1 [server3] The hostname = 192.168.43.89 no_master = 1Copy the code

** Check SSH connections between the management Node and all nodes **

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

** Check the replication environment **

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

Start the MHA

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

Check the MHA status

masterha_check_status --conf=/etc/masterha/mha.conf
Copy the code
<! -- Note: Disable management node monitoring --> <! -- masterha_stop --conf=/etc/masterha/mha.conf -->Copy the code