MySQL MHA high availability configuration and failover

Article Directory

一.MySQL MHA

1. What is MHA

(1) MHA (Master High Availability) is an excellent set of software for failover and master-slave replication in a MySQL high availability environment.
(2) The emergence of MHA is to solve the problem of MySQL single point.
(3) During the MySQL failover process, MHA can automatically complete the failover operation within 0-30 seconds.
(4) MHA can ensure data consistency to the greatest extent during the failover process, so as to achieve high availability in the true sense.

2. Composition of MHA

2.1 MHA Node (data node)

MHA Node runs on each MySQL server.

2.2MHA 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 regularly 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.

3. Features of MHA

(1) During the automatic failover process, MHA tries to save the binary log from the downtime master server to ensure that the data is not lost to the greatest extent.

(2) Using semi-synchronous replication can greatly reduce the risk of data loss

(3) At present, MHA supports a multi-slave architecture with at least three servers, that is, one master and two slaves

4. The working principle of MHA

(1) Save binary log events (binlog events) from the crashed master

(2) Identify the latest updated slave log

(3) Apply the differential relay log (relay log) to other slaves

(4) Apply the binary log events saved from the master

(5) Promote a slave to the new master

(6) Make the master of other slave connection line copy.

5. Planning MySQL MHA construction

MHA: In order to solve the problem of failover, data preservation as much as possible, and the consistency of all node logs

5.1 Experiment ideas

(1) MHA architecture

  • database installation
  • One master and two slaves
  • MHA build

(2) Fault simulation

  • Main library failure
  • Alternate main library becomes main library
  • The original failed master library is restored and rejoined to MHA to become a slave library

5.2 Experimental environment

System, mysql version, IP, component

MHA manager node server: CentOS7.6 (64 bits) manager/192.168.198.12, install MHA node and manager components  
Master node server: CentOS7.6 (64 bits) mysql1/192.168.198.13, install mysql5.7, MHA node components        
Slave1 node server: CentOS7.6 ( 64 bit) mysql2/192.168.198.14, install mysql5.7, MHA node component Slave2 node server: CentOS7.6 (64 bit) mysql3/192.168.198.15       
, install mysql5.7, MHA node component

two. Build MySQL MHA

1. Planning master, slave1, slave2

1.1 Turn off the firewall and security mechanism

systemctl stop firewalld
systemctl disable firewalld
setenforce 0

1.2 Install mysql5.7 on master, slave1, slave2 nodes

#检测是否安装mysql
mysql --version
mysql  Ver 14.14 Distrib 5.7.20, for Linux (x86_64) using  EditLine wrapper

If it is not installed, please refer to the following installation steps

https://blog.csdn.net/Katie_ff/article/details/131255481?spm=1001.2014.3001.5501

1.3 Modify the host names of the Master, Slave1, and Slave2 nodes

hostnamectl set-hostname Master
hostnamectl set-hostname Slave1
hostnamectl set-hostname Slave2

1.4 Modify the Mysql main configuration file /etc/my.cnf of the Master, Slave1, and Slave2 nodes

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

systemctl restart mysqld
#Slave1 节点#
vim /etc/my.cnf
#三台服务器的 server-id 不能一样
server-id = 2 						
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index

systemctl restart mysqld
#Slave2 节点#
vim /etc/my.cnf
#三台服务器的 server-id 不能一样
server-id = 3 
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index

systemctl restart mysqld

1.5 Create two soft links on the Master, Slave1, and Slave2 nodes

#Master 节点#
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
#Slave1 节点#
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
#Slave2 节点#
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/

2. Configure mysql one master and two slaves

2.1 All database nodes (one master and two slaves) perform mysql authorization

mysql -uroot -p
#从数据库同步使用
grant replication slave on *.* to 'myslave'@'192.168.198.%' identified by '123456';		
grant all privileges on *.* to 'mha'@'192.168.198.%' identified by 'manager';		

#manager 使用
#防止从库通过主机名连接不上主库
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';

flush privileges;

blog.csdnimg.cn/6f844a2c962448769ed2f081bbf6059d.png)

2.2 View binary files and synchronization points on the Master node

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |     1747 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

2.3 Perform synchronization operations on Slave1 and Slave2 nodes

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

2.4 View data synchronization results on Slave1 and Slave2 nodes

show slave status\G;	
//确保 IO 和 SQL 线程都是 Yes,代表同步正常。
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.198.13
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 1747
               Relay_Log_File: relay-log-bin.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.198.13
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 1747
               Relay_Log_File: relay-log-bin.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

2.5 Two slave libraries must be set to read-only mode:

#Slave1节点
set global read_only=1;
#Slave2节点
set global read_only=1;

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-E7Qw11RY-1690195083228) (C:\Users\zhao\AppData\Roaming\Typora\typora-user-images\image-20230724155809406.png)]

2.6 Insert data to test database synchronization

#在 Master 主库插入条数据,测试是否同步#
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database blue;
Query OK, 1 row affected (0.05 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| blue               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use blue;
Database changed

mysql> create table test(id int);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into test(id) values (1);
Query OK, 1 row affected (0.01 sec)

mysql> select * from test;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

# Check if the slave data exists

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-hZnTwHz0-1690195083228) (C:\Users\zhao\AppData\Roaming\Typora\typora-user-images\image-20230724160414795.png)]

3. Install all components of MHA

3.1 Install MHA software

3.1.1 Install the MHA-dependent environment on all servers, first install the epel source

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

3.1.2 To install the MHA software package, you must first install the node component on all servers

#对于每个操作系统版本不一样,这里 CentOS7.6选择 0.57 版本。
#在所有服务器上必须先安装 node 组件,最后在 MHA-manager 节点上安装 manager 组件,因为 manager 依赖 node 组件。
cd /opt
tar zxvf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make && make install

3.1.3 Install the manager component on the MHA manager node

cd /opt
tar zxvf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make && make install

3.1.4 Explanation on the use of manager and node tools

#manager 组件安装后在/usr/local/bin 下面会生成几个工具,主要包括以下几个:
masterha_check_ssh 检查 MHA 的 SSH 配置状况
masterha_check_repl 检查 MySQL 复制状况
masterha_manger 启动 manager的脚本
masterha_check_status 检测当前 MHA 运行状态
masterha_master_monitor 检测 master 是否宕机
masterha_master_switch 控制故障转移(自动或者 手动)
masterha_conf_host 添加或删除配置的 server 信息
masterha_stop  关闭manager

#node 组件安装后也会在/usr/local/bin 下面会生成几个脚本(这些工具通常由 MHAManager 的脚本触发,无需人为操作)主要如下:
save_binary_logs 保存和复制 master 的二进制日志
apply_diff_relay_logs 识别差异的中继日志事件并将其差异的事件应用于其他的 slave
filter_mysqlbinlog 去除不必要的 ROLLBACK 事件(MHA 已不再使用这个工具)
purge_relay_logs 清除中继日志(不会阻塞 SQL 线程)

3.1.5 Configure passwordless authentication on all servers

#在 manager 节点上配置到所有数据库节点的无密码认证
#一路按回车键
ssh-keygen -t rsa 				
ssh-copy-id 192.168.198.13
ssh-copy-id 192.168.198.14
ssh-copy-id 192.168.198.15

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-E5eYQoH6-1690195083229) (C:\Users\zhao\AppData\Roaming\Typora\typora-user-images\image-20230724163030113.png)]

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-Qt8dGrMv-1690195083229) (C:\Users\zhao\AppData\Roaming\Typora\typora-user-images\image-20230724163337131.png)]

#在 master 上配置到数据库节点 slave1 和 slave2 的无密码认证
ssh-keygen -t rsa
ssh-copy-id 192.168.198.14
ssh-copy-id 192.168.198.15
#在 slave1 上配置到数据库节点 master 和 slave2 的无密码认证
ssh-keygen -t rsa
ssh-copy-id 192.168.198.13
ssh-copy-id 192.168.198.15
#在 slave2 上配置到数据库节点 master 和 slave1 的无密码认证
ssh-keygen -t rsa
ssh-copy-id 192.168.198.13
ssh-copy-id 192.168.198.14

4. Configure MHA on the manager node

4.1 Copy relevant scripts to the /usr/local/bin directory on the manager node

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

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-YrSrwUnT-1690195083229) (C:\Users\zhao\AppData\Roaming\Typora\typora-user-images\image-20230724164054910.png)]

Notes:

master_ip_failover #Script of VIP management during automatic switching
master_ip_online_change #Management of vip during online switching
power_manager #Script to shut down the host after failure occurs
send_report #Script to send alarm after failover

4.2 Copy the above-mentioned VIP management script during automatic switching to the /usr/local/bin directory, here use the master_ip_failover script to manage VIP and failover

cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin

4.3 The modified content is as follows: (delete the original content, 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
);
#添加内容部分
my $vip = '192.168.10.200';									#指定vip的地址
my $brdc = '192.168.10.255';								#指定vip的广播地址
my $ifdev = 'ens33';										#指定vip绑定的网卡
my $key = '1';												#指定vip绑定的虚拟网卡序列号
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";		#代表此变量值为ifconfig ens33:1 192.168.10.200
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";		#代表此变量值为ifconfig ens33:1 192.168.10.200 down
my $exit_code = 0;											#指定退出状态码为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 {
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";
}

4.4 Create the MHA software directory and copy the configuration file, here use the app1.cnf configuration file to manage the mysql node server

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

vim /etc/masterha/app1.cnf	
#删除原有内容,直接复制并修改节点服务器的IP地址
[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=123456
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.198.14 -s 192.168.198.15
shutdown_script=""
ssh_user=root
user=mha

[server1]
hostname=192.168.198.13
port=3306

[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.198.14
port=3306

[server3]
hostname=192.168.198.15
port=3306

Notes:

[server default]
manager_log=/var/log/masterha/app1/manager.log      #manager日志
manager_workdir=/var/log/masterha/app1            #manager工作目录
master_binlog_dir=/usr/local/mysql/data/         #master保存binlog的位置,这里的路径要与master里配置的binlog的路径一致,以便MHA能找到
master_ip_failover_script=/usr/local/bin/master_ip_failover  #设置自动failover时候的切换脚本,也就是上面的那个脚本
master_ip_online_change_script=/usr/local/bin/master_ip_online_change  #设置手动切换时候的切换脚本
password=manager			#设置mysql中root用户的密码,这个密码是前文中创建监控用户的那个密码
ping_interval=1				#设置监控主库,发送ping包的时间间隔,默认是3秒,尝试三次没有回应的时候自动进行failover
remote_workdir=/tmp			#设置远端mysql在发生切换时binlog的保存位置
repl_password=123		    #设置复制用户的密码
repl_user=myslave			#设置复制用户的用户
report_script=/usr/local/send_report     #设置发生切换后发送的报警的脚本
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.198.14 -s 192.168.198.15	#指定检查的从服务器IP地址
shutdown_script=""			#设置故障发生后关闭故障主机脚本(该脚本的主要作用是关闭主机防止发生脑裂,这里没有使用)
ssh_user=root				#设置ssh的登录用户名
user=mha					#设置监控用户root

[server1]
hostname=192.168.198.13
port=3306

[server2]
hostname=192.168.198.14
port=3306
candidate_master=1
#设置为候选master,设置该参数以后,发生主从切换以后将会将此从库提升为主库,即使这个从库不是集群中最新的slave

check_repl_delay=0
#默认情况下如果一个slave落后master 超过100M的relay logs的话,MHA将不会选择该slave作为一个新的master, 因为对于这个slave的恢复需要花费很长时间;通过设置check_repl_delay=0,MHA触发切换在选择一个新的master的时候将会忽略复制延时,这个参数对于设置了candidate_master=1的主机非常有用,因为这个候选主在切换的过程中一定是新的master

[server3]
hostname=192.168.198.15
port=3306

4.5 The first configuration needs to manually open the virtual IP on the Master node

/sbin/ifconfig ens33:1 192.168.198.200/24

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-Ywzqlah7-1690195083229) (C:\Users\zhao\AppData\Roaming\Typora\typora-user-images\image-20230724165428605.png)]

4.6 Test ssh on the manager node

#在 manager 节点上测试 ssh 无密码认证,如果正常最后会输出 successfully,如下所示。
masterha_check_ssh -conf=/etc/masterha/app1.cnf

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-rzj8VGEP-1690195083230) (C:\Users\zhao\AppData\Roaming\Typora\typora-user-images\image-20230724165601637.png)]

4.7 Test the mysql master-slave connection on the manager node

#在 manager 节点上测试 mysql 主从连接情况,最后出现 MySQL Replication Health is OK 字样说明正常。如下所示。
masterha_check_repl -conf=/etc/masterha/app1.cnf

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-kxH9nCsY-1690195083230) (C:\Users\zhao\AppData\Roaming\Typora\typora-user-images\image-20230724165836847.png)]

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-nP25p1YG-1690195083230) (C:\Users\zhao\AppData\Roaming\Typora\typora-user-images\image-20230724165945915.png)]

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-FxxZfjBO-1690195083230) (C:\Users\zhao\AppData\Roaming\Typora\typora-user-images\image-20230724170044268.png)]

4.8 Start MHA on the manager node

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 &  

Notes:

#生产中java 服务启动的方式
nohup  java -jar  微服务名称-( war jar)   & 

remove_dead_master_conf:该参数代表当发生主从切换后,老的主库的 ip 将会从配置文件中移除。
--manger_log:日志存放位置。
--ignore_last_failover:在缺省情况下,如果 MHA 检测到连续发生宕机,且两次宕机间隔不足 8 小时的话,则不会进行 Failover, 之所以这样限制是为了避免 ping-pong 效应。该参数代表忽略上次 MHA 触发切换产生的文件,默认情况下,MHA 发生切换后会在日志记录,也就是上面设置的日志app1.failover.complete文件,下次再次切换的时候如果发现该目录下存在该文件将不允许触发切换,除非在第一次切换后收到删除该文件,为了方便,这里设置为--ignore_last_failover。

4.9 View MHA status

Check the MHA status, you can see that the current master is the master node.

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

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-QKVDjBNn-1690195083231) (C:\Users\zhao\AppData\Roaming\Typora\typora-user-images\image-20230724170950362.png)]

4.10 Check MHA log

Check the MHA log to see that the current master is 192.168.198.13, as shown below.

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

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-88jyEkOa-1690195083231) (C:\Users\zhao\AppData\Roaming\Typora\typora-user-images\image-20230724171101430.png)]

4.11 View master VIP

#查看master 的 VIP 地址 192.168.198.200 是否存在,这个 VIP 地址不会因为 manager 节点停止 MHA 服务而消失。
ifconfig

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-Zdze7bED-1690195083231) (C:\Users\zhao\AppData\Roaming\Typora\typora-user-images\image-20230724171225958.png)]

4.12 To close the manager service

#若要关闭 manager 服务,可以使用如下命令。
masterha_stop --conf=/etc/masterha/app1.cnf
或者可以直接采用 kill 进程 ID 的方式关闭。

5. MHA simulated failure

#在 manager 节点上监控观察日志记录
tail -f /var/log/masterha/app1/manager.log

#在 Master 节点 master 上停止mysql服务
systemctl stop mysqld
或
pkill -9 mysql

#正常自动切换一次后,MHA 进程会退出。HMA 会自动修改 app1.cnf 文件内容,将宕机的 master 节点删除。查看 slave1 是否接管 VIP
ifconfig

The manager node looks at the log

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-EBPYRUBv-1690195083232) (C:\Users\zhao\AppData\Roaming\Typora\typora-user-images\image-20230724180400319.png)]

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-6CJtv1A9-1690195083232) (C:\Users\zhao\AppData\Roaming\Typora\typora-user-images\image-20230724180843515.png)]

Note: Algorithm for failover of the standby main library:
1. Generally, the slave library is judged from (position/GTID) to judge whether it is good or bad, and the data is different. The slave closest to the master becomes the candidate master.
2. If the data is consistent, select an alternative main library according to the order of the configuration files.
3. Set the weight (candidate_master=1), and the candidate master is forced to be designated according to the weight.
(1) By default, if a slave lags behind the master's relay logs by 100M, it will fail even if it has weight.
(2) If check_repl_delay=0, even if there are many logs behind, it is forced to be selected as the backup master.

5.1 Troubleshooting steps

5.1.1 Repair mysql
systemctl restart mysqld
5.1.2 Repair master-slave
#在现主库服务器 Mysql2 查看二进制文件和同步点
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |     1747 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)


#在原主库服务器 mysql1 执行同步操作(从地址)
change master to master_host='192.168.198.14',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=1747;

start slave;
5.1.3 Modify the configuration file app1.cnf on the manager node (add this record, because it will disappear automatically when it detects the failure)
vi /etc/masterha/app1.cnf
......
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.10.14 -s 192.168.10.15
......
[server1]
hostname=192.168.198.13
port=3306

[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.198.14
port=3306

[server3]
hostname=192.168.198.15
port=3306
5.1.4 Start MHA on the manager node
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 &

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-vwyNyF20-1690195083232) (C:\Users\zhao\AppData\Roaming\Typora\typora-user-images\image-20230724181950434.png)]

5.1.5 Solve the problem of incompatibility of Chinese and English characters
dos2unix /usr/local/bin/master_ip_failover 

total

1. The role of MHA

mysgl high availability + failover

2. The core part of MHA:

MHA component: manager: the main function is to do MHA startup and shutdown management and detection of various health statuses of mysq1

node: When a failure occurs again, save the binary log as much as possible and implement failover

3. MHA configuration files that need to be modified (2)

master tp failover; life tool, defined based on VIP detection and failover (VIP from master—new master)

appl.cnf: The main configuration file of MHA, which mainly defines the working directory, log, and mysq1 binary log location of MHA. The user and password used to log in to mysg1 using MHA are the same as the account and password of the master from the server.

4. Failover is what mha will do:

(1) mha will try to detect the survival status of the master multiple times

(2) mha will try many times to save the master's binary log as much as possible

(3) mha will be based on APP1. In the configuration part of cnf, the screening and switching from the slave server to the master server are carried out (4) mha Finally, the VIP address of the original master will be switched to the position of the slave server

(5) After mha reselects the new master, it will execute the change master operation on the remaining slaves, pointing to the new master, to ensure the health of the imysql cluster

5. mha pay attention to the problem

(1) Soft links must be done

(2) mysq1 configuration file, it is recommended to comment out the Chinese character set

(3) ssh password-free interaction must be done

(4) The memory of mha is recommended to be 4G+ (otherwise it may cause the VIP address to fail to float)

Saving...(img-vwyNyF20-1690195083232)]

5.1.5 Solve the problem of incompatibility of Chinese and English characters
dos2unix /usr/local/bin/master_ip_failover 

Guess you like

Origin blog.csdn.net/Katie_ff/article/details/131902627