欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MySQL高可用架构之MHA

程序员文章站 2022-07-01 08:22:47
...

关于MHA

     MHA (Master HA) 是一款开源的MySQL的高可用程序,它为MySQL主从复制架构提供了automating master failover 功能。MHA在监控到master 节点故障时,会提高其中拥有最新数据库的slave 节点成为新的master 节点,在此期间,MHA会通过于其他从节点获取额外信息来避免一致性方向的问题。MHA还提供了master节点的在线切换功能,即按需切换master/slave节点。
     MHA服务有两种角色,MHA Manager (管理节点)和MHA Node (数据节点):
     1、MHA Manager: 通过单独部署在一*立机器上管理多个master/slave 集群,每个master/slave 集群称作一个application;
     2、MHA node:运行在每台MySQL服务器上(master/slave/manager),它通过监控具备解析和清理logs功能的脚本来加速故障转移。

安装配置MHA

本实验环境共四个节点,其角色分配如下
node1:MHA Manager
node2:Mariadb master
node3: mariadb slave
node4:mariadb slave

各节点的/etc/hosts 文件配置内容添加:
192.168.243.7 node1
192.168.243.8 node2
192.168.243.9 node3
192.168.243.10 node4

系统版本 centos7.3 ,要求时间同步,selinux 关闭
 


准备基于ssh 互信通信环境

MHA集群中的各节点彼此之间需要基于ssh互信通信,以实现远程控制及数据管理功能
下面的操作在manager节点操作即可

[root@node1 .ssh]#ssh-****** -t rsa -P ''
[root@node1 .ssh]#ls
id_rsa  id_rsa.pub  known_hosts
[root@node1 .ssh]#ssh-copy-id  -i ./id_rsa.pub root@node1
[root@node1 .ssh]#ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts
[root@node1 .ssh]#scp id_rsa id_rsa.pub authorized_keys root@node2:/root/.ssh/
[root@node1 .ssh]#scp id_rsa id_rsa.pub authorized_keys root@node3:/root/.ssh/
[root@node1 .ssh]#scp id_rsa id_rsa.pub authorized_keys root@node4:/root/.ssh/

 

主节点master配置
[[email protected] ~]#vim /etc/my.cnf.d/server.cnf

9 [server]
10 innodb_file_per_table=ON
11 skip_name_resolve=ON
12 max_connections=2000
13 log_bin=master-log
14 relay_log=relay-log
15 server_id=1

 

两台slave同样配置,不过在另外一台slave(R4)上要把server_id=3

[[email protected] ~]#vim /etc/my.cnf.d/server.cnf
9 [server]
10 innodb_file_per_table=ON
11 skip_name_resolve=ON
12 
13 server_id=2
14 log_bin=master-log
15 relay_log=relay-log
16 relay_log_purge=off
17 read_only=on

 

master节点:

MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'192.168.243.%' identified by 'CENTOS';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> grant all on *.* to 'mhaadmin'@'192.168.243.%' identified by 'CENTOS';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000003 |      641 |              |                  |
+-------------------+----------+--------------+------------------+

 

slave(node3)

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.243.8',MASTER_USER='repluser',MASTER_PASSWORD='CENTOS',MASTER_LOG_FILE='master-log.000003',MASTER_LOG_POS=641;
MariaDB [(none)]> start slave;
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> show slave status\G;    确保Slave_IO_Running: Yes   Slave_SQL_Running: Yes

 

slave(node4)

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.243.8',MASTER_USER='repluser',MASTER_PASSWORD='CENTOS',MASTER_LOG_FILE='master-log.000003',MASTER_LOG_POS=641;
MariaDB [(none)]> start slave;
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> show slave status\G;

 

安装 MHA manager (R1)
初始化配置MHA

[[email protected] ~]#yum -y install mha4mysql-node-0.56-0.el6.noarch.rpm mha4mysql-manager-0.56-0.el6.noarch.rpm
[[email protected] ~]#mkdir /etc/masterha/
[[email protected] ~]#vim /etc//masterha/app1.cnf

  1 [server default]
  2 user=mhaadmin
  3 password=CENTOS
  4 manager_workdir=/data/masterha/app1
  5 manager_log=/data/masterha/app1/manager.log
  6 remote_workdir=/data/masterha/app1
  7 ssh_user=root
  8 ssh_port=22
  9 repl_user=repluser
 10 repl_password=CENTOS
 11 ping_interval=1
 12 
 13 [server1]
 14 hostname=192.168.243.8
 15 candidate_master=1
 16 
 17 [server2]
 18 hostname=192.168.243.9
 19 candidate_master=1
 20 
 21 [server3]
 22 hostname=192.168.243.10
 23 candidate_master=1

 

安装MHA master(R2)、slave(R3)、slave(R4)

[root@node3 ~]#yum -y install mha4mysql-node-0.56-0.el6.noarch.rpm
[root@node4 ~]#yum -y install  mha4mysql-node-0.56-0.el6.noarch.rpm

 

在mha主机上验证:

1、检测各节点之间ssh 互信通信配置是否OK:

[root@mha ~]#masterha_check_ssh --conf=/etc/masterha/app1.cnf

输出信息最后一行类似如下信息,表示其通过检测
[info] All SSH connection tests passed successfully.

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

[root@mha ~]#masterha_check_repl --conf=/etc/masterha/app1.cnf

输出信息最后一行为 MySQL Replication Health is OK. 检查显示这个结果为通过
 

启动MHA

[root@node1 ~]#masterha_manager --conf=/etc/masterha/app1.cnf 

验证实验效果:

[root@node2 ~]#systemctl  stop mariadb.service   关闭主节点模拟宕机了
在检查node3 从节点 变为主节点了
MariaDB [(none)]> show slave status\G                   
Empty set (0.00 sec) 
MariaDB [(none)]> show master status\G
检查node4从节点,它的主已经指向node3MariaDB [(none)]> show slave status\G
 Master_Host: 192.168.243.9

主节点node2宕机了,R3变成主节点,我们需要把原来的主节点node2修复为从节点继续工作
因此需要更改下

[root@node2 ~]#vim /etc/my.cnf.d/server.cnf  需要在原来的配置上加上下两项
 16 relay_log_purge=off
 17 read_only=on
[root@node2 ~]#systemctl  start mariadb

先检查下新的主节点node3的

MariaDB [(none)]> show master status\G;
*************************** 1. row ***************************
            File: master-log.000002
        Position: 620

然后在node2上执行

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.243.9',MASTER_USER='repluser',MASTER_PASSWORD='CENTOS',MASTER_LOG_FILE='master-log.000002',MASTER_LOG_POS=620;
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G; 检查状态