MySQL数据库(九)MHA集群概述,部署MHA集群
主机名 主从同步角色 集群角色 IP地址
mysql50 客户端 无 192.168.4.50/24
mysql51 主库 当前主库 192.168.4.51/24
mysql52 从库 备用主库 192.168.4.52/24
mysql53 从库 备用主库 192.168.4.53/24
mysql57 无 管理主机 192.168.4.57/24
无 无 VIP地址 192.168.4.100/24
一、MHA集群概述
二、部署MHA集群
1.安装依赖包在所有主机上
[root@mysql51 ~]# yum install -y perl-*
[root@mysql51 ~]# cd mha-soft-sthdent/
[root@mysql51 mha-soft-sthdent]# yum install -y perl-*.rpm
2.配置ssh密钥认证登陆
数据库服务器之间要实现ssh免密登录
管理主机无密码登录数据库服务器
[root@mysql51 ~]# ssh-keygen
[root@mysql51 ~]# ssh-copy-id root@192.168.4.52
[root@mysql51 ~]# ssh-copy-id root@192.168.4.53
[root@mysql52 ~]# ssh-keygen
[root@mysql52 ~]# ssh-copy-id root@192.168.4.51
[root@mysql52 ~]# ssh-copy-id root@192.168.4.53
[root@mysql53 ~]# ssh-keygen
[root@mysql53 ~]# ssh-copy-id root@192.168.4.51
[root@mysql53 ~]# ssh-copy-id root@192.168.4.52
[root@mysql57 ~]# ssh-keygen
[root@mysql57 ~]# ssh-copy-id root@192.168.4.51
[root@mysql57 ~]# ssh-copy-id root@192.168.4.52
[root@mysql57 ~]# ssh-copy-id root@192.168.4.53
3.数据库服务器配置一主两从
[root@mysql51 ~]# vim /etc/my.cnf
[mysqld]
server_id=51
log_bin=master51
.....
[root@mysql51 ~]# systemctl restart mysqld
[root@mysql51 ~]# mysql -uroot -ptarena
mysql> grant replication slave on *.* to repluser@"%" identified by "123qqq...A";
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master51.000005 | 441 | | | |
+-----------------+----------+--------------+------------------+-------------------+
[root@mysql52 ~]# vim /etc/my.cnf
[mysqld]
server_id=52
....
[root@mysql52 ~]# systemctl restart mysqld
[root@mysql52 ~]# mysql -uroot -ptarena
mysql> change master to
-> master_host="192.168.4.51",
-> master_user="repluser",
-> master_password="123qqq...A",
-> master_log_file="master51.000005",
-> master_log_pos=441;
mysql> start slave;
[root@mysql53 ~]# vim /etc/my.cnf
[mysqld]
server_id=53
....
[root@mysql53 ~]# systemctl restart mysqld
[root@mysql53 ~]# mysql -uroot -ptarena
mysql> change master to
-> master_host="192.168.4.51",
-> master_user="repluser",
-> master_password="123qqq...A",
-> master_log_file="master51.000005",
-> master_log_pos=441;
mysql> start slave;
4.配置管理主机、安装mha软件
[root@mysql57 ~]# cd mha-soft-sthdent/
[root@mysql57 mha-soft-sthdent]# rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm
[root@mysql57 mha-soft-sthdent]# tar -zxf mha4mysql-manager-0.58.tar.gz
[root@mysql57 mha-soft-sthdent]# cd mha4mysql-manager-0.58/
[root@mysql57 mha4mysql-manager-0.58]# perl Makefile.PL
[root@mysql57 mha4mysql-manager-0.58]# make && make install
5. 编写配置文件
[root@mysql57 ~]# mkdir /etc/mha
[root@mysql57 ~]# cp /root/mha-soft-sthdent/mha4mysql-manager-0.58/samples/conf/app1.cnf /etc/mha/
[root@mysql57 ~]# vim /etc/mha/app1.cnf
1 [server default] ==>>管理服务默认配置
2 manager_workdir=/etc/mha ==>>工作目录
3 manager_log=/etc/mha/manager.log ==>>日志文件
4 master_ip_failover_script=/etc/mha/master_ip_failover ==>>故障切换脚本
5
6 ssh_user=root ==>>访问ssh服务用户
7 port=22 ==>>ssh服务端口
8
9 user=root ==>>监控用户
10 password=123qqq...A ==>>密码
11
12 repl_user=repluser ==>>数据同步授权用户
13 repl_password=123qqq...A ==>>密码
14
15 [server1] ==>>指定第一台数据库服务器
16 hostname=192.168.4.51 ==>>服务器ip地址
17 port=3306 ==>>服务端口
18 candidate_master=1 ==>>竞选主服务器
19
20 [server2]
21 hostname=192.168.4.52
22 port=3306
23 candidate_master=1
24
25 [server3]
26 hostname=192.168.4.53
27 port=3306
28 candidate_master=1
[root@mysql57 ~]# vim +35 /etc/mha/master_ip_failover
35 my $vip = "192.168.4.100/24" ; ==>>定义VIP地址
36 my $key = "1" ; ==>>定义地址编号
37 my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip" ; ==>>绑定vip地址
38 my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down" ; ==>>释放vip地址
[root@mysql57 ~]# chmod +x /etc/mha/master_ip_failover
6.把VIP地址部署在当前主从数据库服务器的主服务器上
[root@mysql51 ~]# ifconfig eth0:1 192.168.4.100
[root@mysql51 ~]# ip add show|grep 192.168.4.100
inet 192.168.4.100/24 brd 192.168.4.255 scope global secondary eth0:1
7.配置数据节点
在51、52、53上相同配置
[root@mysql51 ~]# cd mha-soft-sthdent/
[root@mysql51 mha-soft-sthdent]# yum install -y mha4mysql-node-0.58-0.el7.centos.noarch.rpm
[root@mysql51 ~]# mysql -uroot -ptarena
mysql> grant all on *.* to root@"%" identified by "123qqq...A"; ==>>添加配置文件的监控用户
mysql> grant replication slave on *.* to repluser@"%" identified by "123qqq...A"; ==>>添加配置文件的数据同步用户
配置主服务器
都配置半同步复制模式
启用binlog日志
禁止自动删除中继日志文件
[root@mysql51 ~]# vim /etc/my.cnf
[mysqld]
server_id=51
log_bin=master51
plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so" ==>>加载模块
rpl_semi_sync_master_enabled=1 ==>>启用master模块
rpl_semi_sync_slave_enabled=1 ==>>启用slave模块
relay_log_purge=0 ==>>禁止自动删除中继日志文件
.....
[root@mysql51 ~]# systemctl restart mysqld
主服务器52、53修改如下
[root@mysql52 ~]# vim /etc/my.cnf
[mysqld]
server_id=52
log_bin=master52
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
relay_log_purge=0
.....
[root@mysql52 ~]# systemctl restart mysqld
8.测试ssh配置
[root@mysql57 ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf
...
Tue Jul 14 15:33:29 2020 - [info] All SSH connection tests passed successfully.
9.测试主从配置
[root@mysql57 ~]# masterha_check_repl --conf=/etc/mha/app1.cnf
....
MySQL Replication Health is OK!
10.启动管理服务
[root@mysql57 ~]# masterha_manager --conf=/etc/mha/app1.cnf --remove_dead-master_conf --ignore_last_failover
--remove_dead-master_conf 删除宕机主库的配置
--ignore_last_failover 忽略xxx.health文件
[root@mysql57 ~]# masterha_check_status --conf=/etc/mha/app1.cnf ==>>查看状态
app1 (pid:127010) is running(0:PING_OK), master:192.168.4.51
[root@mysql57 ~]# masterha_stop --conf=/etc/mha/app1.cnf ==>>停止服务
11.测试高可用
[root@mysql51 ~]# systemctl stop mysqld
[root@mysql57 ~]# masterha_manager --conf=/etc/mha/app1.cnf --remove_dead-master_conf --ignore_last_failover
[root@mysql52 ~]# ifconfig eth0:1
inet 192.168.4.100/24 brd 192.168.4.255 scope global secondary eth0:1
12.故障服务器修复
数据库服务器
启动mysql服务
与主服务器数据一致
指定主服务器信息
启动slave进程
查看状态信息
[root@mysql51 ~]# systemctl start mysqld
[root@mysql51 ~]# mysql -uroot -ptarena
mysql> change master to
-> master_host="192.168.4.52",
-> master_user="repluser",
-> master_password="123qqq...A",
-> master_log_file="master52.000001",master_log_pos=154;
mysql> start slave;master_password="123qqq...A",
mysql> show slave status \G
管理服务器
修改主配置文件
测试集群环境
重启管理服务
查看服务状态
[root@mysql57 ~]# vim /etc/mha/app1.cnf
......
[server1]
hostname=192.168.4.51
port=3306
candidate_master=1
[server2]
hostname=192.168.4.52
port=3306
candidate_master=1
[server3]
hostname=192.168.4.53
port=3306
candidate_master=1
[root@mysql57 ~]# masterha_stop --conf=/etc/mha/app1.cnf
[root@mysql57 ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf
[root@mysql57 ~]# masterha_check_repl --conf=/etc/mha/app1.cnf
[root@mysql57 ~]# masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:93356) is running(0:PING_OK), master:192.168.4.52
本文地址:https://blog.csdn.net/yy1506438689/article/details/107346864
上一篇: 神策数据宣布获2600万元A轮融资
下一篇: 互联网分层架构的本质