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

MHA,把已经故障的master主机,重新加入

程序员文章站 2022-04-20 08:36:12
第一次正常实验,70为主,80为备主,90为从[root@centos-60 ~]# vim /usr/local/bin/master_ip_failover虚拟ip为192.168.0.66 网卡名为ens33[root@centos-70 ~]# vim /etc/my.cnf[root@centos-80 ~]# vim /etc/my.cnf[root@centos-90 ~]# vim /etc/my.cnf[root@centos-70 ~]# systemctl r...

第一次正常实验,70为主,80为备主,90为从

部署 MHA 高可用集群
192.168.0.70 centos-70 master  mha4mysql-node
192.168.0.80 centos-80 slave1,Candidate master  mha4mysql-node
192.168.0.90 centos-90 slave2  mha4mysql-node mha4mysql-manage

[root@centos-70 ~]# ssh-keygen
[root@centos-80 ~]# ssh-keygen
[root@centos-80 ~]# ssh-keygen

[root@centos-70 ~]# ssh-copy-id 192.168.0.70
[root@centos-70 ~]# ssh-copy-id 192.168.0.80
[root@centos-70 ~]# ssh-copy-id 192.168.0.90

[root@centos-80 ~]# ssh-copy-id 192.168.0.70
[root@centos-80 ~]# ssh-copy-id 192.168.0.80
[root@centos-80 ~]# ssh-copy-id 192.168.0.90

[root@centos-90 ~]# ssh-copy-id 192.168.0.70
[root@centos-90 ~]# ssh-copy-id 192.168.0.80
[root@centos-90 ~]# ssh-copy-id 192.168.0.90

70-80-90执行以下命令
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager libappstream-glib deltarpm patchutils
rpm -ivh mha4mysql-node-0.57-0.el7.noarch.rpm

90执行以下命令
yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN
rpm -ivh mha4mysql-manager-0.57-0.el7.noarch.rpm
 

70-80-90执行以下命令
systemctl start mysqld
vim /etc/my.cnf 
validate-password=OFF
systemctl restart mysqld
grep 'password' /var/log/mysqld.log
mysql -u root -p'buL.UJp!T2Od'  
set password for root@localhost = password('123456');
flush privileges;

70执行以下命令
mysql -u root -p123456
create database ha;
use ha;
create table test(id int,name varchar(20));
insert into test values (1,"jack");
exit
vim /etc/my.cnf
log-bin=mysql-bin-master
server-id=1
binlog-do-db=ha
binlog-ignore-db=mysql
systemctl restart mysqld
mysql -u root -p123456
grant replication slave on *.* to repl@'192.168.0.%' identified by '123456';
flush privileges;
show master status;
exit
mysqldump -u root -p123456 -B ha >ha.sql
scp ha.sql root@192.168.0.80:/root
scp ha.sql root@192.168.0.90:/root

80执行以下命令
mysql -u root -p123456 <ha.sql
vim /etc/my.cnf
log-bin=mysql-slave1
server-id=2
binlog-do-db=ha
binlog-ignore-db=mysql
log_slave_updates=1
systemctl restart mysqld
mysql -u root -p123456
grant replication slave on *.* to 'repl'@'192.168.0.%' identified by '123456';
flush privileges;
stop slave;
change master to master_host='192.168.0.70',master_user='repl',master_password='123456';
start slave;
show slave status\G
exit

90执行以下命令
mysql -u root -p123456 <ha.sql
vim /etc/my.cnf
log-bin=mysql-slave2
server-id=3
binlog-do-db=ha
binlog-ignore-db=mysql
log_slave_updates=1
systemctl restart mysqld
mysql -u root -p123456
grant replication slave on *.* to 'repl'@'192.168.0.%' identified by '123456';
flush privileges;
stop slave;
change master to master_host='192.168.0.70',master_user='repl',master_password='123456';
start slave;
show slave status\G
exit

测试 mysql 主从数据同步
70执行以下命令
mysql -uroot -p123456
use ha;
insert into test values(2,"man");
80执行以下命令
mysql -uroot -p123456
use ha;
select * from test;

80-90执行以下命令
mysql -u root -p123456 -e 'set global read_only=1'

70-80-90执行以下命令
mysql -uroot -p123456
grant all privileges on *.* to 'mha'@'localhost' identified by '123456';
grant all privileges on *.* to 'mha'@'192.168.0.%' identified by '123456';
flush privileges;
exit

90执行以下命令
mkdir -p /etc/masterha
mkdir -p /var/log/masterha/app1
cd /etc/masterha/
rz 上传app1.cnf
vim /etc/masterha/app1.cnf

[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/manager.log
master_binlog_dir=/var/lib/mysql
master_ip_failover_script=/usr/local/bin/master_ip_failover
password=123456
user=mha
ping_interval=1
remote_workdir=/tmp
repl_password=123456
repl_user=repl
ssh_user=root

[server1]
hostname=192.168.0.70
port=3306

[server2]
hostname=192.168.0.80
port=3306
candidate_master=1
check_repl_delay=0

[server3]
hostname=192.168.0.90
port=3306

rz 上传 master_ip_failover
mv master_ip_failover /usr/local/bin/
chmod +x /usr/local/bin/master_ip_failover
vim /usr/local/bin/master_ip_failover
MHA,把已经故障的master主机,重新加入

80-90执行以下命令
mysql -uroot -p123456 -e 'set global relay_log_purge=0'

90执行以下命令
masterha_check_ssh --conf=/etc/masterha/app1.cnf
masterha_check_repl --conf=/etc/masterha/app1.cnf
nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &

90新开ssh执行以下命令
tailf /var/log/masterha/app1/manager.log

测试故障转移
70执行以下命令
systemctl stop mysqld
90执行以下命令
mysql -u root -p123456
show slave status\G
查看主库是否变更为80
80执行以下命令
insert into ha.test values(3,'test');
90执行以下命令
select * from ha.test;
查看是否同步80的数据

故障恢复后重新加入 mysql 主从集群
70执行以下命令
systemctl start mysqld
mysql -uroot -p123456 -e 'set global read_only=1'
mysql -uroot -p123456 -e ' set global relay_log_purge=0'
mysql -uroot -p123456
select * from ha.test;
查看下当前数据
stop slave;
change master to master_host='192.168.0.80',master_user='repl',master_password='123456';
flush privileges;
start slave;
show slave status\G
select * from ha.test;
查看70宕机后,80新增的数据是否同步过来,数据是否一致。
exit

90执行以下命令
vim /etc/masterha/app1.cnf

[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/manager.log
master_binlog_dir=/var/lib/mysql
master_ip_failover_script=/usr/local/bin/master_ip_failover
password=123456
user=mha
ping_interval=1
remote_workdir=/tmp
repl_password=123456
repl_user=repl
ssh_user=root

[server1]
candidate_master=1
check_repl_delay=0
hostname=192.168.0.70
port=3306

[server2]
hostname=192.168.0.80
port=3306

[server3]
hostname=192.168.0.90
port=3306

masterha_check_repl --conf=/etc/masterha/app1.cnf
nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &

80执行以下命令
systemctl stop mysqld
 

90查看日志,是否master切换回70

70执行以下命令
select * from ha.test; 查看数据和90的数据是否一致。
insert into ha.test values (4,'abc');
exit
ip a
查看虚拟ip是否为192.168.0.66

90执行以下命令
select * from ha.test; 查看数据是否和70一致。


整个流程走完,数据一致,没有问题,虚拟ip,也会跟随master漂移。

 

本文地址:https://blog.csdn.net/workey/article/details/108755672

相关标签: linux