当前位置:网站首页>MySQL MHA

MySQL MHA

2022-06-21 06:58:00 Lost

One 、 What is? MHA

1、MHA The concept of

  • MHA(MasterHigh Availability) It's an excellent set of MySQL Software for failover and master-slave replication in high availability environment .
  • MHA The emergence of is to solve MySQL ​ Single point problem .
  • MySQL During failover ,MHA Can do ​0-30 Seconds ​ Automate failover operations .
  • MHA In the process of failover ​ Ensure the consistency of data to the greatest extent ​, To achieve real high availability .

2、MHA The composition of

  • MHA Node( Data nodes )
  • MHA Node Running on the ​ Each station MySQL Server .
  • MHA Manager( The management node )
  • MHA Manager It can be deployed separately on a separate machine , Manage multiple master-slave colony ; It can also be deployed in one slave Node .
  • MHA Manager Meeting ​ Regularly detect... In the cluster master node ​. When master Failure time , It can automatically send the latest data to slave Upgrade to a new master, And then put all the other slave Point back to the new master. The entire failover process is completely transparent to the application .

3、MHA Characteristics

  • During automatic failover ,MHA Trying to save binary logs from the down primary server , Ensure that data is not lost to the greatest extent
  • Use semi synchronous replication , Can greatly reduce the risk of data loss , If only one slave Has received the latest binary log ,MHA You can apply the latest binary logs to all other slave Server , Therefore, the data consistency of all nodes can be guaranteed
  • at present MHA Support one master multi-slave architecture , At least three servers , That is, one master and two slaves

4、MySQL Database cluster architecture , How to ensure high availability

  • One master two / Three from , For high availability solutions MHA,MMM.
  • MHA The architecture will be in all MySQL Server installation mha node Components , It will also be on a separate server / Some one slave Install on node mha manager Components .
  • manger The component will detect the in the cluster regularly master Is the node alive , If master fault ,manger It will automatically upgrade one with the latest data slave Become a new in the cluster master, And then the other slave The node points back to the new Master, Complete the new master-slave synchronization , So as to achieve MySQL In the cluster master High availability and data consistency .

Two 、 build MySQL+MHA

MySQL Of MHA_mysql

1、 Get ready

  • MHAmanager:192.168.132.50 MHA node Components 、manager Components
  • Slave1:192.168.132.51 mysql-boost-5.7.20.tar.gz、MHA node Components
  • Slave2:192.168.132.52 mysql-boost-5.7.20.tar.gz、MHA node Components
  • Master:192.168.132.53 mysql-boost-5.7.20.tar.gz、MHA node Components

Turn off firewalls and security

      
      
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
  • 1.
  • 2.
  • 3.

MySQL Of MHA_mysql_02

MySQL Of MHA_ The server _03

2、 modify mysql The hostname of the node

      
      
hostnamectl set - hostname mysql1
su -

hostnamectl set - hostname mysql2
su -

hostnamectl set - hostname mysql3
su -
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

MySQL Of MHA_mysql_04

MySQL Of MHA_perl_05

MySQL Of MHA_perl_06

3、 Modify three MySQL The main profile of the server /etc/my.cnf, And create a command soft link

mysql1:192.168.132.51

      
      
vim / etc / my. cnf
[ mysqld]
server - id = 1
log_bin = master - bin
log - slave - updates = true

systemctl restart mysqld

ln - s / usr / local / mysql / bin / mysql / usr / sbin /
ln - s / usr / local / mysql / bin / mysqlbinlog / usr / sbin /
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

MySQL Of MHA_mysql_07

MySQL Of MHA_mysql_08

mysql2:192.168.132.52

mysql3:192.168.132.53

      
      
vim / etc / my. cnf
server - id = 2
#server - id = 3 mysq3 Then for 3, Three servers server - id It can't be the same
log_bin = master - bin
relay - log = relay - log - bin
relay - log - index = slave - relay - bin. index

systemctl restart mysqld

ln - s / usr / local / mysql / bin / mysql / usr / sbin /
ln - s / usr / local / mysql / bin / mysqlbinlog / usr / sbin /
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

MySQL Of MHA_ The server _09

MySQL Of MHA_ The server _10

4、 To configure MySQL One master and two slaves

mysql1:192.168.132.51

mysql2:192.168.132.52

mysql3:192.168.132.53

1. all MySQL Server run MySQL to grant authorization

      
      
mysql - uroot - p123456
grant replication slave on *. * to 'myslave' @ '192.168.132.%' identified by '123456';
grant all privileges on *. * to 'mha' @ '192.168.132.%' identified by 'manager';
grant all privileges on *. * to 'mha' @ 'mysql1' identified by 'manager';
grant all privileges on *. * to 'mha' @ 'mysql2' identified by 'manager';
grant all privileges on *. * to 'mha' @ 'mysql3' identified by 'manager';

flush privileges;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

MySQL Of MHA_ The server _11

2. stay Master Node to view binaries and synchronization points

mysql1:192.168.132.51

      
      
show master status;
# Everyone's binary file name or offset may be different , Remember your
  • 1.
  • 2.

MySQL Of MHA_ The server _12

3. stay Slave1、Slave2 The node performs a synchronization operation

mysql2:192.168.132.52

mysql3:192.168.132.53

      
      
change master to master_host = '192.168.132.51', master_user = 'myslave', master_password = '123456', master_log_file = 'master-bin.000001', master_log_pos = 1747;

start slave;

show slave status \G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

# commonly Slave_IO_Running: No The possibility of :
# The Internet is not working
#my. cnf Configuration problem
# password 、file file name 、pos The offset is not right
# The firewall is not closed
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

MySQL Of MHA_ The server _13

4.Slave1、Slave2 The node is set to read-only mode

mysql2:192.168.132.52

mysql3:192.168.132.53

      
      
set global read_only = 1;
# Change back to read-write state set global read_only = 0;
  • 1.
  • 2.

MySQL Of MHA_ The server _14

5. Master slave replication verification

mysql1:192.168.132.51

      
      
create database test;
use test;
create table test( id int);
insert into test values( 1);
  • 1.
  • 2.
  • 3.
  • 4.

MySQL Of MHA_perl_15

mysql2:192.168.132.52

mysql3:192.168.132.53

MySQL Of MHA_ The server _16

5、 install MHA Software

1. All servers have MHA Dependent environment

MHAmanager:192.168.132.50

mysql1:192.168.132.51

mysql2:192.168.132.52

mysql3:192.168.132.53

  • First installation epel Source , need ​ Online source installation
  • Then install... On all servers node Components
      
      
# Install online source
mv / etc / yum. repos. d / bak / CentOS -* / etc / yum. repos. d /
yum list

yum install epel - release -- nogpgcheck - y

yum install - y perl - DBD - MySQL \
perl - Config - Tiny \
perl - Log - Dispatch \
perl - Parallel - ForkManager \
perl - ExtUtils - CBuilder \
perl - ExtUtils - MakeMaker \
perl - CPAN
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

MySQL Of MHA_ The server _17

MySQL Of MHA_ The server _18

MySQL Of MHA_perl_19

MySQL Of MHA_ The server _20

2. install MHA node software package

  • For each operating system, the version is different , here CentOS7.4 Must choose 0.57 edition .
  • Must be on all servers ​ Install first node Components , Last in MHA-manager Install on node manager Components ​, because manager rely on node Components .
      
      
# Put the package mha4mysql - node - 0.57. tar. gz Put in / opt Under the table of contents
cd / opt
tar zxvf mha4mysql - node - 0.57. tar. gz
cd mha4mysql - node - 0.57
perl Makefile. PL
make && make install
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

MySQL Of MHA_perl_21

MySQL Of MHA_perl_22

3. stay MHA manager Install on node manager Components

MHAmanager:192.168.132.50

      
      
# Put the package mha4mysql - manager - 0.57. tar. gz Put in / opt Under the table of contents
cd / opt
tar zxvf mha4mysql - manager - 0.57. tar. gz
cd mha4mysql - manager - 0.57
perl Makefile. PL
make && make install
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

MySQL Of MHA_ The server _23

MySQL Of MHA_ The server _24

manager After the components are installed, install them in /usr/local/bin The following tools will be generated

MySQL Of MHA_mysql_25

  • masterha_check_ssh Check MHA Of SSH Configuration status
  • masterha_check_repl Check MySQL Copy status
  • masterha_manger start-up manager Script for
  • masterha_check_status Detect current MHA Running state
  • masterha_master_monitor testing master Is it down?
  • masterha_master_switch Control failover ( Automatic or manual )
  • masterha_conf_host Add or remove configured server Information
  • masterha_stop close manager

node Components will also be installed in /usr/local/bin Several scripts will be generated below ( These tools are usually made of MHAManager The script triggers , There is no need for human operation )

MySQL Of MHA_mysql_26

  • save_binary_logs Save and copy master Binary log
  • apply_diff_relay_logs Identify differentiated relay log events and apply their differentiated events to other slave
  • filter_mysqlbinlog Remove unnecessary ROLLBACK event (MHA This tool is no longer used )
  • purge_relay_logs Clear relay logs ( It won't block SQL Threads )

6、 Configure password less authentication on all servers

1. stay manager Configure password less authentication to all database nodes on the node

MHAmanager:192.168.132.50

      
      
ssh - keygen - t rsa # Press enter all the way
ssh - copy - id 192.168 .132 .51
ssh - copy - id 192.168 .132 .52
ssh - copy - id 192.168 .132 .53
  • 1.
  • 2.
  • 3.
  • 4.

MySQL Of MHA_ The server _27

2. stay mysql1 Configuration to the database node mysql2 and mysql3 No password authentication ssh-keygen -t rsa

mysql1:192.168.132.51

      
      
ssh - keygen - t rsa
ssh - copy - id 192.168 .132 .52
ssh - copy - id 192.168 .132 .53
  • 1.
  • 2.
  • 3.

3. stay mysql2 Configuration to the database node mysql1 and mysql3 No password authentication

mysql2:192.168.132.52

      
      
ssh - keygen - t rsa
ssh - copy - id 192.168 .132 .51
ssh - copy - id 192.168 .132 .53
  • 1.
  • 2.
  • 3.

4. stay mysql3 Configuration to the database node mysql1 and mysql2 No password authentication

mysql3:192.168.132.53

      
      
ssh - keygen - t rsa
ssh - copy - id 192.168 .132 .51
ssh - copy - id 192.168 .132 .52
  • 1.
  • 2.
  • 3.

MySQL Of MHA_ The server _28

7、 stay manager Configuration on node MHA

MHAmanager:192.168.132.50

1. stay manager Copy related scripts on node to /usr/local/bin Catalog

      
      
cp - rp / opt / mha4mysql - manager - 0.57 / samples / scripts / usr / local / bin

# After copying, there will be four execution files
ll / usr / local / bin / scripts /
  • 1.
  • 2.
  • 3.
  • 4.

MySQL Of MHA_ The server _29

  • master_ip_failover When switching automatically VIP Managed scripts
  • master_ip_online_change When switching online vip Management of
  • power_manager Script to shut down the host after the failure
  • send_report Because the script that sends the alarm after the failover

2. When copying the above automatic switching VIP Manage scripts to /usr/local/bin Catalog , Use here master_ip_failover Scripts to manage VIP And failover

      
      
cp / usr / local / bin / scripts / master_ip_failover / usr / local / bin
  • 1.

MySQL Of MHA_ The server _30

      
      
# Empty the original content first
echo '' > / usr / local / bin / master_ip_failover

# Directly copy and modify vip Related parameters
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
);
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # Add content section # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
my $vip = '192.168.132.200'; # Appoint vip The address of
my $brdc = '192.168.132.255'; # Appoint vip The address of
my $ifdev = 'ens33'; # Appoint vip Bound network card
my $key = '1'; # Appoint vip Serial number of the bound virtual network card
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip"; # Represents that the value of this variable is ifconfig ens33: 1 192.168 .132 .200
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down"; # Represents that the value of this variable is ifconfig ens33: 1 down
my $exit_code = 0; # Specify the exit status code as 0
#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: [email protected]\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";
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.

MySQL Of MHA_perl_31

3. establish MHA Software directory and copy configuration files , Use here app1.cnf Configuration files to manage mysql Node server

      
      
mkdir / etc / masterha
cp / opt / mha4mysql - manager - 0.57 / samples / conf / app1. cnf / etc / masterha

vim / etc / masterha / app1. cnf
[ server default]
manager_log =/ var / log / masterha / app1 / manager. log
manager_workdir =/ var / log / masterha / app1
master_binlog_dir =/ usr / local / mysql / data
master_ip_failover_script =/ usr / local / bin / master_ip_failover
master_ip_online_change_script =/ usr / local / bin / master_ip_online_change
user = mha
password = manager
ping_interval = 1
remote_workdir =/ tmp
repl_user = myslave
repl_password = 123456
secondary_check_script =/ usr / local / bin / masterha_secondary_check - s 192.168 .132 .52 - s 192.168 .132 .53
shutdown_script = ""
ssh_user = root

[ server1]
hostname = 192.168 .132 .51
port = 3306

[ server2]
candidate_master = 1
check_repl_delay = 0
hostname = 192.168 .132 .52
port = 3306

[ server3]
hostname = 192.168 .132 .53
port = 3306

# -------------------------- Profile explanation --------------------------------------------------------------------------
[ server default]
manager_log =/ var / log / masterha / app1 / manager. log    #manager journal
manager_workdir =/ var / log / masterha / app1. log     #manager working directory
master_binlog_dir =/ usr / local / mysql / data /        #master preservation binlog The location of , The path here has to do with master Internally configured binlog The path is the same , In order to MHA Can find
master_ip_failover_script =/ usr / local / bin / master_ip_failover    # Set auto failover When the switch script , That's the script above
master_ip_online_change_script =/ usr / local / bin / master_ip_online_change   # Set the switch script for manual switching
user = mha # Set up monitoring users root
password = manager # Set up mysql in root User's password , This password is the one that created the monitoring user in the previous article
ping_interval = 1 # Set up the main monitoring library , send out ping Time interval between packages 1 second , The default is 3 second , Try three times when there is no response failover
remote_workdir =/ tmp # Set the remote end mysql When a switch occurs binlog Where to save
repl_user = myslave # Set the user of the copy user
repl_password = 123456 # Set the password of the copy user
report_script =/ usr / local / send_report   # Set the script of the alarm sent after switching
secondary_check_script =/ usr / local / bin / masterha_secondary_check - s 192.168 .132 .52 - s 192.168 .132 .53 # Specify the slave server to check IP Address
shutdown_script = "" # Set to close the failed host script after the failure occurs ( The main function of the script is to shut down the host and prevent brain crack , It's not used here )
ssh_user = root # Set up ssh Login user name of

[ server1]
hostname = 192.168 .132 .51
port = 3306

[ server2]
hostname = 192.168 .132 .52
port = 3306
candidate_master = 1
# Set as candidate master, After setting this parameter , After the master-slave switch, the slave database will be promoted to the master database , Even if the main library is not the latest in the cluster slave
check_repl_delay = 0
# By default, if one slave backward master exceed 100M Of relay logs Words ,MHA Will not choose the slave As a new master, Because for this slave It takes a long time to recover ; By setting check_repl_delay = 0 ,MHA Trigger switch in selecting a new master The replication delay will be ignored , This parameter is set for candidate_master = 1 Is very useful , Because the candidate must be new in the process of switching master

[ server3]
hostname = 192.168 .132 .53
port = 3306
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.

MySQL Of MHA_mysql_32

8、 The first configuration needs to be in Master Manually turn on the virtual server on the node IP

mysql1:192.168.132.51

      
      
/ sbin / ifconfig ens33: 1 192.168 .132 .200 / 24
  • 1.

MySQL Of MHA_mysql_33

9、 stay manager Testing on nodes ssh No password authentication

MHAmanager:192.168.132.50

      
      
masterha_check_ssh - conf =/ etc / masterha / app1. cnf
# If it's normal, it will output successfully;
# If it fails, you can go to the place where the server has no password authentication to see if there is a problem
  • 1.
  • 2.
  • 3.

MySQL Of MHA_ The server _34

10、 stay manager Testing on nodes mysql Master-slave connection

MHAmanager:192.168.132.50

      
      
masterha_check_repl - conf =/ etc / masterha / app1. cnf
# Last appearance MySQL Replication Health is OK The words indicate normal ;
# appear MySQL Replication Health is NOT OK ! Of , You can have a look mysql Whether there are fewer soft links on the server --> Article location :2、 Modify three MySQL The main profile of the server / etc / my. cnf, And create a command soft link
  • 1.
  • 2.
  • 3.

MySQL Of MHA_perl_35

MySQL Of MHA_ The server _36

11、 stay manager Start on the node MHA

MHAmanager:192.168.132.50

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


# ------------------------ Component interpretation ----------------------------------------------------------------------------------
-- remove_dead_master_conf: This parameter represents when a master-slave switch occurs , Old master library ip Will be removed from the configuration file .
-- manger_log: Log storage location .
-- ignore_last_failover: By default , If MHA Continuous downtime detected , And the interval between two downtime is not enough 8 In an hour , It won't go on Failover, The reason for this restriction is to avoid ping - pong effect . This parameter means to ignore the last MHA Trigger the file generated by switching , By default ,MHA After switching, the directory will be recorded in the log , That is, the log set above app1. failover. complete file , The next time you switch again, if you find the file in the directory, you will not be allowed to trigger the switch , Unless you receive a delete file after the first switch , For convenience , I'm going to set it to -- ignore_last_failover.
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

MySQL Of MHA_perl_37

12、 Check the status

MHAmanager:192.168.132.50

1. see MHA state , You can see the current master yes Mysql1 node .

      
      
masterha_check_status -- conf =/ etc / masterha / app1. cnf
  • 1.

MySQL Of MHA_mysql_38

2. see MHA journal , Also to see the current master yes 192.168.163.11

      
      
cat / var / log / masterha / app1 / manager. log | grep "current master"
  • 1.

MySQL Of MHA_ The server _39

3. see Mysql1 Of VIP Address , see Mysql1 Of VIP Address 192.168.163.200 Whether there is , This VIP The address is not because manager Nodes stop MHA Service and disappear .

mysql1:192.168.132.51

      
      
ifconfig
  • 1.

MySQL Of MHA_ The server _40

Add : To shut down manager service , You can use the following command .

      
      
masterha_stop -- conf =/ etc / masterha / app1. cnf
Or you can directly use kill process ID The way to turn off .
  • 1.
  • 2.

3、 ... and 、 fault simulation

1、 fault simulation

1. stay manager Monitoring observation logging on the node

MHAmanager:192.168.132.50

      
      
tail - f / var / log / masterha / app1 / manager. log
  • 1.

MySQL Of MHA_perl_41

2. stay Master node Mysql1 Stop on mysql service

mysql1:192.168.132.51

      
      
systemctl stop mysqld
or
pkill - 9 mysql
  • 1.
  • 2.
  • 3.

After normal automatic switching once ,MHA The process will exit .HMA Will automatically modify app1.cnf The contents of the document , Will be down mysql1 The node to delete .

MySQL Of MHA_ The server _42

3. see mysql2 Take over VIP

mysql2:192.168.132.52

      
      
ifconfig
  • 1.

MySQL Of MHA_perl_43

4. go back to manager Monitoring observation logging on the node

MHAmanager:192.168.132.50

MySQL Of MHA_mysql_44

      
      
Algorithm of failover alternative master database :
1 、 In general, the judgment from the database is from (position / GTID) Judge the pros and cons , The data are different , Closest to master Of slave, Become a candidate .
2 、 When the data is consistent , In the order of configuration files , Select an alternate master library .
3 、 Set weights (candidate_master = 1 ), Mandatory assignment of alternate masters by weight .
(1) By default, if one slave backward master 100 M Of relay logs Words , Even with weight , It's going to fail .
(2) If check_repl_delay = 0 Words , Even behind a lot of logs , It is also mandatory to choose it as the alternative host .
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

2、 Troubleshooting steps

1. Repair mysql

mysql1:192.168.132.51

      
      
systemctl restart mysqld
netstat - natp | grep 3306
  • 1.
  • 2.

MySQL Of MHA_perl_45

2. Fix the master-slave

1) In the current main database server Mysql2 View binaries and synchronization points

mysql2:192.168.132.52

      
      
mysql - uroot - p123456 - e 'show master status;'
# Execute... In the database show master status;
  • 1.
  • 2.

MySQL Of MHA_ The server _46```

2) In the original master database server mysql1 Perform synchronous operation

mysql1:192.168.132.51

      
      
change master to master_host = '192.168.132.52', master_user = 'myslave', master_password = '123456', master_log_file = 'master-bin.000001', master_log_pos = 1899;

start slave;
show slave status \G
  • 1.
  • 2.
  • 3.
  • 4.

MySQL Of MHA_ The server _47

3. stay manager Modify the configuration file on the node app1.cnf

MHAmanager:192.168.132.50

Add this record to it , Because it will automatically disappear when it fails to detect

      
      
vim / etc / masterha / app1. cnf
……
secondary_check_script =/ usr / local / bin / masterha_secondary_check - s 192.168 .132 .51 - s 192.168 .132 .53
......
[ server1]
hostname = 192.168 .132 .52
port = 3306

[ server2]
candidate_master = 1
check_repl_delay = 0
hostname = 192.168 .132 .51
port = 3306

[ server3]
hostname = 192.168 .132 .53
port = 3306
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

MySQL Of MHA_perl_48

4. stay manager Start on the node MHA

      
      
masterha_stop -- conf =/ etc / masterha / app1. cnf

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

masterha_check_status -- conf =/ etc / masterha / app1. cnf
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

MySQL Of MHA_perl_49

MySQL Of MHA_mysql_50

原网站

版权声明
本文为[Lost]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221543032854.html