版本:5.7.28
1.准备MHA集群环境
准备6台虚拟机,并按照本节规划配置好IP参数
在这些虚拟机之间实现SSH免密登录
在相应节点上安装好MHA相关的软件包
使用6台RHEL 7虚拟机,如图-1所示。准备集群环境,安装依赖包,授权用户,配置ssh密钥对认证登陆,所有节点之间互相以root秘钥对认证登录,管理主机以root密钥对认证登录所有数据节点主机,配置mha集群。
1.1 修改主机名,配置IP
msyql{50..57}
192.168.4.{50..57}
1.2 安装包(51-55)
]# tar -xvf mha56.tar.gz
]# cd mha
]# yum -y install perl-*.rpm
1.3 在管理主机上安装mha_node 和 mha-manager包(56操作)
]# yum -y install perl-DBD-mysql perl-DBI
]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
1.4 配置ssh密钥对认证登陆
所有节点之间可以互相以ssh密钥对方式认证登陆(以51为例)
]# ssh-keygen
]# for i in {50..57}
> do
> ssh-copy-id 192.168.4.$i
> done
]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.4.50 mysql50
192.168.4.51 mysql51
192.168.4.52 mysql52
192.168.4.53 mysql53
192.168.4.54 mysql54
192.168.4.55 mysql55
192.168.4.56 mysql56
192.168.4.57 mysql57
2. 配置MHA集群环境
配置主节点 master51
配置两个备用主节点 master52、master53
配置两个从节点 slave54、slave55
配置管理节点 mgm56
2.1 配置mha集群环境
2.1.1 安装数据库(51-55同样操作,以51为例)
前面有。
2.1.2 master51 数据库服务器配置文件
]# vim /etc/my.cnf(52.53都配置,修改id)
relay_log_purge=off(不自动删除本机的中继日志文件)
plugin-load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl-semi-sync-master-enabled = 1
rpl-semi-sync-slave-enabled = 1
server_id=51
log-bin=mysql51
binlog-format="mixed"
validate_password_policy=0
validate_password_length=6
添加主从同步授权用户(51)
必要时:(从库)
mysql> STOP SLAVE;
mysql> RESET SLAVE;
mysql> grant replication slave on *.* to repluser@"%" identified by "123456";
mysql> RESET MASTER;
mysql> show master status;
mysql> SHOW MASTER STATUS\G
mysql> SHOW MASTER STATUS\G
*************************** 1. row ***************************
File: mysql51.000001
Position: 154
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
2.1.3 master52数据库服务器配置文件(53同样配置)
mysql> RESET MASTER;
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.4.51',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='123456',
-> MASTER_LOG_FILE='mysql51.000001',
-> MASTER_LOG_POS=154;
mysql> START SLAVE;
mysql> SHOW SLAVE STATUS\G
...
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...
2.1.4 slave54 数据库服务器配置文件(55一样)
]# vim /etc/my.cnf
[mysqld]
server_id=54
log-bin=mysql54
binlog-format="mixed"
validate_password_policy=0
validate_password_length=6
mysql> RESET MASTER;
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.4.51',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='123456',
-> MASTER_LOG_FILE='mysql51.000001',
-> MASTER_LOG_POS=154;
mysql> START SLAVE;
mysql> SHOW SLAVE STATUS\G
...
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...
2.2 配置管理主机4.56
# yum -y install perl*(root下)
]# cd mha/
mha]# yum -y install *.rpm
mha]# tar -xvf mha4mysql-manager-0.56.tar.gz
mha]# cd mha4mysql-manager-0.56/
0.56]# perl Makefile.PL
0.56]# make && make install
0.56]# cp bin/* /usr/local/bin/
创建工作目录
]# mkdir /etc/mha_manager
建立样板文件
]# cp samples/conf/app1.cnf /etc/mha_manager/
]# vim /etc/mha_manager/app1.conf
//编辑主配置文件app1.cnf
[server default]
manager_workdir=/etc/mha_manager
manager_log=/etc/mha_manager/manager.log
master_ip_failover_script=/usr/local/bin/master_ip_failover
ssh_user=root
ssh_port=22
repl_user=repluser
repl_password=123456
user=root
password=123456
[server1]
hostname=192.168.4.51
port=3306
[server2]
hostname=192.168.4.52
port=3306
candidate_master=1
[server3]
hostname=192.168.4.53
port=3306
candidate_master=1
[server4]
hostname=192.168.4.54
no_master=1
[server5]
hostname=192.168.4.55
no_master=1
创建故障切换的脚本
]# vim samples/scripts/master_ip_failover
35 my $vip='192.168.4.100/24';
36 my $key='1';
37 my $ssh_start_vip = '/sbin/ifconfig eth0:$key $vip';
38 my $ssh_stop_vip = '/sbin/ifconfig eth0:$key down';
39
40 GetOptions(在这上面添加上面几行)
]# cp samples/scripts/master_ip_failover /usr/local/bin/
]# chmod +x /usr/local/bin/master_ip_failover
51上部署eth0
51 ~]# ifconfig eth0:1 192.168.4.100/24
]# ifconfig eth0:1
eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.4.100 netmask 255.255.255.0 broadcast 192.168.4.255
ether 52:54:00:d6:46:46 txqueuelen 1000 (Ethernet)
3.测试MHA集群
查看MHA集群状态
测试节点之间的SSH登录
测试集群VIP的故障切换功能
3.1 验证配置
检查配置环境,在主机 52-55 检查是否有同步数据的用户repluser
mysql> select user,host from mysql.user where user="repluser";
mysql> show grants for repluser@"%";
在51的主机上做root的授权,其他的会同步(如果不做,在验证数据节点的主从同步配置时会出错)
mysql> grant all on *.* to root@"%" identified by "123456";
(50-55查看)
mysql> select user,host from mysql.user where user="root";
+------+-----------+
| user | host |
+------+-----------+
| root | % |
| root | localhost |
+------+-----------+
验证ssh 免密登陆数据节点主机(56)
-0.56]# cd /usr/local/bin/
bin]# masterha_check_ssh --conf=/etc/mha_manager/app1.conf
...
All SSH connection tests passed successfully.
3.2 启动管理服务MHA_Manager
--remove_dead_master_conf //删除宕机主库配置
--ignore_last_failover //忽略xxx.health文件
bin]# masterha_manager --conf=/etc/mha_manager/app1.conf \
--remove_dead_master_conf --ignore_last_failover
查看状态(另开一个终端)
]# masterha_check_status --conf=/etc/mha_manager/app1.conf
停止服务
]# masterha_stop --conf=/etc/mha_manager/app1.cnf
3.3 测试故障转移
启动服务
bin]# masterha_manager --conf=/etc/mha_manager/app1.conf \
--remove_dead_master_conf --ignore_last_failover
查看状态
]# masterha_check_status --conf=/etc/mha_manager/app1.conf
...master:192.168.4.52
验证数据节点的主从同步配置
bin]# masterha_check_repl --conf=/etc/mha_manager/app1.conf
来源:https://www.cnblogs.com/luwei0915/p/12258326.html