Table of contents
2. Deploy MySQL database MHA high availability
The first step is to turn off all firewalls and security mechanisms
The second step is to modify the main database configuration file
The third step is to modify the configuration file of the slave database
Step 5: Optimize all database paths
Step 6 Authorize all database nodes
Step 7 Set up authorized connection
Step 8: Refresh the permission table
Step 10: Set slave database rules
Step 11: Set the read-only mode from the database
Step 12 View slave database node information
Step 14 Install MHA dependent environment
Step 15 Import Node components and install them
Step 16 Install the Manager component on the MHA Manager node
Step 17 Configure all database nodes for passwordless authentication
Step 18 Configure three databases for passwordless authentication
Step 19 Copy the executable file
Step 21 Modify the script file
Step 22 Modify the content of the script file
Step 23: Create the MHA software directory and copy the configuration file
Step 24 Modify the configuration file
Step 25: Manually enable virtual IP
Step 26 Test SSH passwordless authentication
Step 27: Test the master-slave connection
1. MHA concept
MHA (Master High Availability) is an excellent set of failover and master-slave replication software in a MySQL high-availability environment. The emergence of MHA is to solve the single point problem of MySQL. During the MySQL failover process, MHA can automatically complete the failover operation within 0-30 seconds. MHA can ensure data consistency to the greatest extent during the failover process to achieve true high availability.
Composition of MHA
-
MHA Node (data node) MHA Node runs on every MySQL server.
-
MHA Manager (Management Node) MHA Manager can be deployed on an independent machine to manage multiple master-slave clusters; it can also be deployed on a slave node. MHA Manager will periodically detect the master node in the cluster. When the master fails, it can automatically promote the slave with the latest data to the new master and then redirect all other slaves to the new master. The entire failover process is completely transparent to the application.
Features of MHA
-
During the automatic failover process, MHA attempts to save the binary log from the downed main server to ensure that data is not lost to the greatest extent
-
Using semi-synchronous replication can greatly reduce the risk of data loss. If only one slave has received the latest binary log, MHA can apply the latest binary log to all other slave servers, thus ensuring the data consistency of all nodes.
-
Currently, MHA supports one master and multiple slaves architecture, with at least three services, that is, one master and two slaves.
MHA Manger manages multiple sets of master-slave replication.
How MHA works
The working principle of MHA is summarized as follows:
1. Save binary log events (binlog events) from the crashed master; 2. Identify the slave log containing the latest updates 3. Apply the difference relay log (relay log) to other slaves 4. Apply the binary saved from the master Log event 5. Promote a slave to the new master 6. Make other slaves connected to the master replicate.
2. Deploy MySQL database MHA high availability
The first step is to turn off all firewalls and security mechanisms
Instruction: systemctl stop firewalld
systemctl disable firewalld
setenforce 0
The second step is to modify the main database configuration file
Command: vim /etc/my.cnf
The third step is to modify the configuration file of the slave database
Command: vim /etc/my.cnf
Step 4: Restart the service
Command: systemctl restart mysqld
Step 5: Optimize all database paths
命令:ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
Step 6 Authorize all database nodes
Command: grant replication slave on *.* to 'myslave'@'network segment' identified by 'password';
grant all slave on *.* to 'mha'@'网段' identified by 'manager';
Step 7 Set up authorized connection
命令:grant all privileges on *.* to 'mha'@'master' identified by 'manager';
grant all privileges on *.* to 'mha'@'slave1' identified by 'manager';
grant all privileges on *.* to 'mha'@'slave2' identified by 'manager';
Step 8: Refresh the permission table
Command: flush privileges;
Step 9 View the Master node
Command: show master status;
Step 10: Set slave database rules
Command: change master to master_host='Master database IP address',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=1743;
Step 11: Set the read-only mode from the database
Instruction: set global read_only=1;
Step 12 View slave database node information
Instruction: show slave status\G
Step 13 Install EPLE source
Instruction: yum -y install epel-release --nogpgchec
Step 14 Install MHA dependent environment
命令:yum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN
Step 15 Import Node components and install them
Command: tar xf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make && make install
Step 16 Install the Manager component on the MHA Manager node
命令:tar zxvf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make && make install
Step 17 Configure all database nodes for passwordless authentication
Command: ssh-keygen -t rsa
ssh-copy-id master database IP
ssh-copy-id slave database IP
ssh-copy-id slave database IP
Step 18 Configure three databases for passwordless authentication
Step 19 Copy the executable file
Command: cp -rp /opt/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
Step 20 Copy the script file
命令:cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
Step 21 Modify the script file
Command: vim /usr/local/bin/master_ip_failover
Step 22 Modify the content of the script file
Content: my $vip = '192.168.80.200'; #Specify the address of vip
my $brdc = '192.168.80.255'; #Specify the broadcast address of vip
my $ifdev = 'ens33'; #Specify the network card bound to vip
my $ key = '1'; #Specify the virtual network card serial number bound by vip
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip"; #Represents the value of this variable as ifconfig ens33:1 192.168.80.200
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down"; #Represents the value of this variable as ifconfig ens33:1 192.168.80.200 down
my $exit_code = 0; #Specifies the exit status code is 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: $@\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 {
"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";
}
Step 23: Create the MHA software directory and copy the configuration file
Instruction: mkdir /etc/masterha
cp /opt/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha
Step 24 Modify the configuration file
Command: vim /etc/masterha/app1.cnf
content:
[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
password=manager
ping_interval=1
remote_workdir=/tmp
repl_password=123
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.80.11 -s 192.168.80.12
shutdown_script=""
ssh_user=root
user=mha[server1]
hostname=main database IP
port=3306[server2]
candidate_master=1
check_repl_delay=0
hostname=slave database IP
port=3306[server3]
hostname=slave database IP
port=3306
Step 25: Manually enable virtual IP
Command: /sbin/ifconfig ens33:1 192.168.80.200/24
Step 26 Test SSH passwordless authentication
Instruction: masterha_check_ssh -conf=/etc/masterha/app1.cnf
Step 27: Test the master-slave connection
Instruction: masterha_check_repl -conf=/etc/masterha/app1.cnf
Step 28: Start MHA on Manager
命令: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 &
Step 29 Check MHA status
Instruction: masterha_check_status --conf=/etc/masterha/app1.cnf
Step 30 View MHA logs
Command: cat /var/log/masterha/app1/manager.log | grep "current master"