Centos7——MHA部署

目录

    • 基础操作
    • master操作
    • slave1操作
    • slave2操作
    • manager操作
    • 测试 MHA 故障转移
    • 连续漂移

实现主宕机,从切换为主,保证业务正常运行

环境准备
CentOS Linux release 7.5.1804 (Core)

角色 节点 ip 说明
manager node1 192.168.174.130 管理
master node2 192.168.174.131
slave1 node3 192.168.174.132
slave2 node4 192.168.174.133

基础操作

修改主机名和hosts文件(四台)

[root@localhost ~]# hostname node1
[root@localhost ~]# hostname node2
[root@localhost ~]# hostname node3
[root@localhost ~]# hostname node4

四台相同操作

[root@localhost ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.174.130 node1
192.168.174.131 node2
192.168.174.132 node3
192.168.174.133 node4

Centos7——MHA部署_第1张图片

关闭防火墙(四台)

[root@node1 ~]# systemctl stop firewalld;setenforce 0
[root@node2 ~]# systemctl stop firewalld;setenforce 0
[root@node3 ~]# systemctl stop firewalld;setenforce 0
[root@node4 ~]# systemctl stop firewalld;setenforce 0

上传安装包
manager包

链接: https://pan.baidu.com/s/1Jrq1Y2CKC4dxfq_m7BvM9g 提取码: b8w2 复制这段内容后打开百度网盘手机App,操作更方便哦

node包

链接: https://pan.baidu.com/s/1awh55tSQvP3KjZHIugUMyA 提取码: sc2p 复制这段内容后打开百度网盘手机App,操作更方便哦
ssh免密登陆

[root@node1 ~]# ssh-keygen -t rsa
[root@node2 ~]# ssh-keygen -t rsa
[root@node3 ~]# ssh-keygen -t rsa
[root@node4 ~]# ssh-keygen -t rsa

Centos7——MHA部署_第2张图片

[root@node1 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@node1

Centos7——MHA部署_第3张图片

[root@node2 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@node1
[root@node3 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@node1
[root@node4 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@node1
[root@node1 ~]# cat /root/.ssh/authorized_keysau

在这里插入图片描述
将authorized_keysau文件发送给node2/3/4

[root@node1 ~]# scp .ssh/authorized_keys root@node2:.ssh/
[root@node1 ~]# scp .ssh/authorized_keys root@node3:.ssh/
[root@node1 ~]# scp .ssh/authorized_keys root@node4:.ssh/

测试是否免密登陆其他主机

[root@node1 ~]# ssh node2

[root@node1 ~]# ssh node3

[root@node1 ~]# ssh node4

Centos7——MHA部署_第4张图片

master操作

[root@node2 ~]# yum -y install epel-release
[root@node2 ~]# mariadb mariadb-server
[root@node2 ~]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm 

修改配置文件

[root@node2 ~]# vim /etc/my.cnf
server-id = 1
log-bin = master-log
relay-log = relay-log
skip_name_resolve

Centos7——MHA部署_第5张图片

[root@node2 ~]# systemctl restart mariadb #重启mysql
[root@node2 ~]# mysql

#授权用户给从库使用
MariaDB [(none)]> grant replication slave on *.* to slave@'192.168.174.%' identified by '123'; 
Query OK, 0 rows affected (0.04 sec)

#授权用户给manager管理master使用
MariaDB [(none)]> grant all on *.* to mha@'192.168.174.%' identified by '123'; 
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000003 |      529 |              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

slave1操作

[root@node3 ~]# yum -y install mariadb mariadb-server epel-release
[root@node3 ~]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm 

主从同步

[root@node3 ~]# vim /etc/my.cnf
server-id = 2                
relay-log = relay-log         
log-bin = master-log         
read_only = ON               
relay_log_purge = 0           
skip_name_resolve              
log_slave_updates = 1
[root@node3 ~]# mysql

MariaDB [(none)]> change master to master_host='192.168.174.131',master_user='slave',master_password='123',master_log_file='master-log.000003',master_log_pos=529;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> show slave status \G;

Centos7——MHA部署_第6张图片

#授权用户给manager管理slave使用
MariaDB [(none)]> grant all on *.* to slave@'192.168.174.%' identified by '123';
Query OK, 0 rows affected (0.00 sec)

slave2操作

[root@node4 ~]# yum -y install mariadb mariadb-server epel-release
[root@node4 ~]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm 

主从同步

[root@node3 ~]# vim /etc/my.cnf
server-id = 3               
relay-log = relay-log         
log-bin = master-log         
read_only = ON               
relay_log_purge = 0           
skip_name_resolve              
log_slave_updates = 1
[root@node3 ~]# mysql

MariaDB [(none)]> change master to master_host='192.168.174.131',master_user='slave',master_password='123',master_log_file='master-log.000003',master_log_pos=529;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> show slave status \G;

Centos7——MHA部署_第7张图片

#授权用户给manager管理slave使用
MariaDB [(none)]> grant all on *.* to slave@'192.168.174.%' identified by '123';
Query OK, 0 rows affected (0.00 sec)

manager操作

[root@node1 ~]# yum -y install mariadb mariadb-server epel-release
[root@node1 ~]# yum -y install mha4mysql-node-0.56-0.el6.noarch.rpm mha4mysql-manager-0.56-0.el6.noarch.rpm 

创建manager配置文件

[root@node1 ~]# mkdir /etc/mha_master
[root@node1 ~]# vim /etc/mha_master/mha.cnf
[server default]
user=mha
password=123
manager_workdir=/etc/mha_master/app1
manager_log=/etc/mha_master/manager.log
remote_workdir=/mydata/mha_master/app1
ssh_user=root
repl_user=slave
repl_password=123
ping_interval=1
[server1]
hostname=192.168.174.131
ssh_port=22
candidate_master=1
[server2]
hostname=192.168.174.132
ssh_port=22
candidate_master=1
[server3]
hostname=192.168.174.133
ssh_port=22
candidate_master=1

Centos7——MHA部署_第8张图片
对各个节点进行检测

[root@node1 ~]# masterha_check_ssh --conf=/etc/mha_master/mha.cnf

Centos7——MHA部署_第9张图片

检查管理的MySQL复制集群的连接配置参数是否OK

[root@node1 ~]# masterha_check_repl --conf=/etc/mha_master/mha.cnf

Centos7——MHA部署_第10张图片
如果出错,在master重新授权一下,再次执行

MariaDB [(none)]> grant all on *.* to mha@'192.168.174.%' identified by '123';
Query OK, 0 rows affected (0.00 sec)

Centos7——MHA部署_第11张图片
Centos7——MHA部署_第12张图片
启动mha

[root@node1 ~]# nohup masterha_manager -conf=/etc/mha_master/mha.cnf &> /etc/mha_master/manager.log &                                                                                      
[1] 13110
[root@node1 ~]# masterha_check_status -conf=/etc/mha_master/mha.cnf
mha (pid:13110) is running(0:PING_OK), master:192.168.174.131

测试 MHA 故障转移

在master操作

[root@node2 ~]# yum -y install psmisc
[root@node2 ~]# systemctl stop mariadb

在manger节点查看日志

[root@node1 ~]# tail -50 /etc/mha_master/manager.log

Centos7——MHA部署_第13张图片
注意,故障转移完成后,manager将会自动停止
在slave2节点上查看

Centos7——MHA部署_第14张图片

连续漂移

manager操作

#启动mha
[root@localhost mha_master]# nohup masterha_manager -conf=/etc/mha_master/mha.cnf &> /etc/mha_master/manager.log & 
[1] 18826
#查看master主机地址
[root@localhost mha_master]# masterha_check_status -conf=/etc/mha_master/mha.cnf
mha monitoring program is now on initialization phase(10:INITIALIZING_MONITOR). Wait for a while and try checking again.
[root@localhost mha_master]# masterha_check_status -conf=/etc/mha_master/mha.cnf
mha (pid:18826) is running(0:PING_OK), master:192.168.174.131
[root@localhost ~]# cd /etc/mha_master

[root@localhost mha_master]# rm -rf app1/

master操作131

#停止mysql
[root@localhost ~]# systemctl stop mariadb

在slave2(133)查看
Centos7——MHA部署_第15张图片
132变成master

MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000003 |      245 |              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

将原来131主设置为从

#重新mysql
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# mysql
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [(none)]> change master to master_host='192.168.174.132',master_user='slave',master_password='123',master_log_file='master-log.000003',master_log_pos=245;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

Centos7——MHA部署_第16张图片
参考博客:https://blog.csdn.net/qq_49296785/article/details/109326517

你可能感兴趣的:(数据库,linux,mysql,mariadb)