MHA:Master High Availability
实现功能为对主节点进行监控,可实现自动故障转移至其他从节点;通过提升某一从节点称为新的主节点,基于主从复制,需要客户端配合实现,目前MHA主要支持一主多从架构
MHA管理端安装
[root@localhost ~]# yum install mha4mysql-manager-0.58-0.el7.centos.noarch.rpm mha4mysql-node-0.58-0.el7.centos.noarch.rpm
[root@localhost ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:bFw1TVu4gjKkzqSD/W9xRRM8uUvrPkxeTTTyde12/dk root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| .+=..o|
| . .*oo+=|
| o .o +++=|
| ooo.. = ..*|
| o = So o + +=|
| . + o.. . + ..E|
| o o = . |
| . . = |
| o. ... |
+----[SHA256]-----+
[root@localhost ~]# ssh-copy-id 192.168.103.71
[root@localhost ~]# rsync -av .ssh 192.168.103.72:/root/
[root@localhost ~]# rsync -av .ssh 192.168.103.73:/root/
[root@localhost ~]# rsync -av .ssh 192.168.103.74:/root/
manage管理端
[root@localhost ~]# mkdir /etc/mastermha/
[root@localhost ~]# mkdir /data/mastermha/app1/ -pv
mkdir: created directory ‘/data/mastermha’
mkdir: created directory ‘/data/mastermha/app1/’
[root@localhost ~]# vim /etc/mastermha/app1.cnf
[server default]
user=mhauser
password=ptg123
manager_workdir=/data/mastermha/app1/
manager_log=/data/mastermha/app1/manager.log
remote_workdir=/data/mastermha/app1/
ssh_user=root
repl_user=repluser
repl_password=ptg123
ping_interval=1
[server1]
hostname=192.168.103.72
candidate_master=1
[server2]
hostname=192.168.103.73
candidate_master=1
[server3]
hostname=192.168.103.74
MASTER主节点
[mysqld]
log-bin
server_id=1
skip_name_resolve=1
mysql> show master logs
-> ;
+--------------------+-----------+
| Log_name | File_size |
+--------------------+-----------+
| mariadb-bin.000001 | 245 |
+--------------------+-----------+
1 row in set (0.00 sec)
mysql> grant replication slave on *.* to repluser@'192.168.103.%' identified by 'ptg123';
Query OK, 0 rows affected (0.00 sec)
mysql > grant all on *.* to mhauser@'192.168.103.%' identified by 'ptg123';
Query OK, 0 rows affected (0.00 sec)
slave节点配置,所有节点master必须一致并且都得创建账号
server_id=2
log-bin
read_only
relay_log_purge=0
skip_name_resolve=1
mysql> CHANGE MASTER TO MASTER_HOST='192.168.103.72', MASTER_USER='repluser',MASTER_PASSWORD='ptg123', MASTER_LOG_FILE='mariadb
mysql> grant replication slave on *.* to repluser@'192.168.103.%' identified by 'ptg123';
Query OK, 0 rows affected (0.00 sec)
mysql > grant all on *.* to mhauser@'192.168.103.%' identified by 'ptg123';
Query OK, 0 rows affected (0.00 sec)bin.000001',MASTER_LOG_POS=542;
Query OK, 0 rows affected (0.01 sec)
在MHA上利用前台监视master DB
[root@localhost ~]# masterha_manager --conf=/etc/mastermha/app1.cnf
Sun Apr 25 20:41:10 2021 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sun Apr 25 20:41:10 2021 - [info] Reading application default configuration from /etc/mastermha/app1.cnf..
Sun Apr 25 20:41:10 2021 - [info] Reading server configuration from /etc/mastermha/app1.cnf..
![image.png](https://upload-images.jianshu.io/upload_images/25265206-fd41e33d8cf173c4.png?
主服务down了后会自动退出
imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)