[MYSQL] MHA Building Process

Posted by imawake on Tue, 30 Jul 2019 21:05:48 +0200

Environmental Introduction

IP role
172.16.20.105 master
172.16.20.106 slave1(manage)
172.16.20.107 slave2

Install mysql

Download the mha installation package

https://code.google.com/archive/p/mysql-master-ha/downloads?page=1

mha installation

## Install dependency packages (all nodes)

yum install perl-DBD-MySQL

Install node(all node)

cd /home/software/mha4mysql-node-0.54
perl Makefile.PL
make && make install

Install manager

#Installing dependency packages
#Dependent on Packet Download Address
https://rpmfind.net/linux/rpm2html/search.php?
https://metacpan.org/pod/Log::Dispatch
https://github.com/riywo/MHA-Node

YAML
DBI
DBD::mysql
Time::HiRes
Config::Tiny
Log::Dispatch
Parallel::ForkManager
MHA::NodeConst

yum install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes
yum install perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN.noarch


#Check the installed perl module
instmodsh
#Installation manager
cd /home/software/mha4mysql-manager-0.54
perl Makefile.PL
make && make install

Configuration of mutual trust

https://www.cnblogs.com/jytx/p/7272860.html

Configure trust between manager and node s

Building Master and Subordinate

#Backup of main library
mysqldump -uroot  --master-data=2 --single-transaction -R --triggers -A > all.sql
#- - - View binlog information
head -n 30 all.sql | grep 'CHANGE MASTER TO'
#Create duplicate accounts
grant replication slave on *.* to 'repl'@'172.16.20.%' identified by '123456';
flush privileges;
#Create monitoring users (subsequently configure mha for use in master execution)
grant all privileges on *.* to 'root'@'172.16.20.%' identified  by '123456';
#Copy backup files to slave libraries and restore them
scp all.sql root@172.16.20.106:/tmp/
scp all.sql root@172.16.20.107:/tmp/
chmod 707 /tmp/all.sql
su - mysql
mysql -uroot < /tmp/all.sql
mysql -uroot 
CHANGE MASTER TO MASTER_HOST='172.16.20.105',MASTER_USER='repl', MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000004',MASTER_LOG_POS=531; 
start slave;
#Configure read only
mysql -uroot  -e "set global read_only=1"
mysql -uroot -e "show variables like  'read_only'"

Configure mha

#Create configuration directories and files
mkdir -p /etc/mha
cp /home/software/mha4mysql-manager-0.54/samples/conf/app1.cnf  /etc/mha/
mkdir -p /var/log/mha/app1

#Modification of configuration files
cat /etc/masterha/app1.cnf 
[server default]
manager_workdir=/var/log/mha/app1.log                           //Setting up the manager's working directory
manager_log=/var/log/mha/app1/manager.log                       //Setting up the manager's log
master_binlog_dir=/data/mysql                                        //Set up the location where the master saves the binlog so that MHA can find the master's log, which is the data directory of mysql.
#Master_ip_failover_script=/usr/local/bin/master_ip_failover// Set the handover script when automatic failover occurs
#Master_ip_online_change_script=/usr/local/bin/master_ip_online_change// Set the handover script for manual handover
password=123456                                                        //Set the password of the root user in mysql, which is the password created previously to monitor the user
user=root               Setting up monitoring users root
ping_interval=1                                                 //Set up monitor main library, send ping package time interval, default is 3 seconds, try three times without response automatically railover
remote_workdir=/tmp                                             //Set the location of binlog for remote mysql when handover occurs
repl_password=123456                                            //Setting the password of the replicated user
repl_user=repl                                                  //Setting the replication user name in the replication environment
report_script=/usr/local/bin/send_report                            //Set the script for alarm sent after handover
secondary_check_script= /usr/local/bin/masterha_secondary_check -s server03 -s server02 --user=root --master_host=server02 --master_ip=192.168.2.128 --master_port=3306                                                                          //Once there is a problem between MHA and server 02 monitoring, MHA Manager will try to log in from server 03 to server 02.
shutdown_script=""                                             //Set up the failure host script to close after the failure occurs (the main function of the script is to close the host and put it in the case of brain fissure, which is not used here)
ssh_user=root                                                  //Setting ssh login username

[server1]
hostname=192.168.2.128
port=3306

[server2]
hostname=192.168.2.129
port=3306
candidate_master=1
                                                             //Set it as a candidate master, and if this parameter is set, the slave library will be elevated to the master library after master-slave switching occurs, even if the master library is not the latest slave of events in the cluster.
check_repl_delay=0                                           //By default, if a slave lags behind relay logs of master 100M, MHA will not choose the slave as a new master because recovery of the slave takes a long time, by setting check_repl_delay.=0,MHA Trigger handover in selecting a new master The replication delay is ignored. This parameter is set to candidate_master=1 The host is very useful because the candidate must be new in the process of switching. master

[server3]
hostname=192.168.2.130
port=3306

#------- Setting the relay log clearance mode (slave)
mysql -uroot  -e "set global relay_log_purge=0"
mysql -uroot -e "show variables like 'relay_log_purge'"

#- Write slave scripts for cleaning up relay regularly

#- Check ssh configuration
masterha_check_ssh --conf=/etc/mha/app1.cnf
#- Check the replication environment
masterha_check_repl --conf=/etc/mha/app1.cnf
//Build a Soft Chain on Slve after Error Reporting
(cant exec mysqlbinlog: no such file or directory at /usr/local/perl5/...... line 99)
ln -s /Data/mysql/my3306/bin/mysql /usr/bin/mysql
ln -s /Data/mysql/my3306/bin/mysqlbinlog /usr/bin/mysqlbinlog
#- Check manager status
masterha_check_status --conf=/etc/mha/app1.cnf
#Open mha monitoring
mkdir -p /var/log/mha/app1
nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover </dev/null > /var/log/mha/app1/manager.log 2>&1 &

//Start-up parameter description
--remove_dead_master_conf      //This parameter represents that when master-slave switching occurs, the ip of the old master library will be removed from the configuration file.
--manger_log                   //Location of log storage
--ignore_last_failover         //By default, Failover will not occur if MHA detects a continuous downtime with less than eight hours between downtimes. The reason for this limitation is to avoid ping-pong effect. This parameter means ignoring the file generated by the last MHA trigger switch. By default, when MHA switch occurs, app1.failover.complete file will be generated in the log directory, that is, the / data I set up above. If this file exists in the directory next time, it will not be allowed to trigger switch unless it is found in the first time. After switching, the file is deleted. For convenience, it is set to ignore_last_failover.

#- Check manager status
masterha_check_status --conf=/etc/mha/app1.cnf

#Close mha monitoring
masterha_stop --conf=/etc/mha/app1.cnf

#VIP Management
#Environmental testing

Switchover test

1. Close the original main library and observe the handover

2. Start the original main library and restore the environment to its original state.

After failover is found in manager.log, the log and pos of the new master node. Execute change master on the original master library to make it a slave.

Add the server information of the original master to / etc/mha/app1.cnf.

[server1]
hostname=172.16.20.105
port=3306

Hand-over

masterha_master_switch  --conf=/etc/mha/app1.cnf --master_state=alive --new_master_host=172.16.20.105 --new_master_port=3306 --orig_master_is_new_slave

Topics: MySQL SQL yum mysqlbinlog