MHA高可用架构
注意:一主多从=奇数
在做MHA高可用实验前需要做好gtid主从复制或者半同步复制,此次采用gtid主从复制
gtid主从复制链接:https://blog.csdn.net/Aplox/article/details/104670774
gtid主从复制环节
在3台虚拟机上做好gtid主从复制(基于前次实验做读写分离)
systemctl stop mysqld
cd /var/lib/mysql/
rm -fr *
vim /etc/my.cnf
最后写入
server1配置文件写入
server_id=1
gtid_mode=ON
enforce_gtid_consistency=ON
log_slave_updates=ON 开启主从更换
log_bin=binlog 开启二进制日志
server2配置文件写入
server_id=2
gtid_mode=ON
enforce_gtid_consistency=ON
log_slave_updates=ON 开启主从更换
log_bin=binlog 开启二进制日志
server3配置文件写入
server_id=3
gtid_mode=ON
enforce_gtid_consistency=ON
log_slave_updates=ON 开启主从更换
log_bin=binlog 开启二进制日志
安全初始化
systemctl start mysqld
cat /var/log/mysqld.log | grep password
mysql_secure_installation
server1上写入
登陆创建新的用户
mysql -uroot -pAPLOX111fox.
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'172.25.254.%' IDENTIFIED BY 'APLOX111fox.';
创建用户并授权
flush privileges;
show master status;
server2和server3上写入
mysql -uroot -pAPLOX111fox.
CHANGE MASTER TO MASTER_HOST='172.25.254.1',
MASTER_USER='repl',
MASTER_PASSWORD='APLOX111fox.',
MASTER_AUTO_POSITION=1;
start slave;
show slave status\G
MHA环节
server4是一台全新的虚拟机
在server4上:
yum install mha4mysql-manager-0.58-0.el7.centos.noarch.rpm perl-* mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y
生成ssh密钥,管理节点是不需要输入密码,将生成的公钥和私钥传给数据节点
ssh-keygen
ssh-copy-id server1
ssh-copy-id server2
ssh-copy-id server3
scp -r ~/.ssh server1:
scp -r ~/.ssh server2:
scp -r ~/.ssh server3:
测试:实现免密登陆
所有虚拟机实现免密登陆
将节点的安装包传给数据节点
scp -r mha4mysql-node-0.58-0.el7.centos.noarch.rpm server1:/root/
scp -r mha4mysql-node-0.58-0.el7.centos.noarch.rpm server2:/root/
scp -r mha4mysql-node-0.58-0.el7.centos.noarch.rpm server3:/root/
数据节点安装
[root@server1 ~]# yum install -y mha4mysql-node-0.58-0.el7.centos.noarch.rpm
[root@server2 ~]# yum install -y mha4mysql-node-0.58-0.el7.centos.noarch.rpm
[root@server3 ~]# yum install -y mha4mysql-node-0.58-0.el7.centos.noarch.rpm
server4上创建一个目录,编辑配置文
mkdir -p /etc/masterha
cd /etc/masterha/
vim master.cnf
[server default]
manager_workdir=/etc/masterha
manager_log=/var/log/masterha.log # manager 日志文件
master_binlog_dir=/etc/masterha
#master_ip_failover_script= /usr/local/bin/master_ip_failover
#master_ip_online_change_script= /usr/local/bin/master_ip_online_change
password=APLOX111fox.
user=root
ping_interval=1
remote_workdir=/tmp
repl_password=APLOX111fox.
repl_user=repl #复制帐号和密码
#report_script=/usr/local/send_report
#secondary_check_script= /usr/local/bin/masterha_secondary_check -s server 03 -s server02
#shutdown_script=""
ssh_user=root #系统ssh用户
[server1]
hostname=172.25.254.1
port=3306
[server2]
hostname=172.25.254.2
port=3306
candidate_master=1
check_repl_delay=0
[server3]
hostname=172.25.254.3
port=3306
no_master=1 #这个节点不能作为master
ssh检查
masterha_check_ssh --conf=/etc/masterha/master.cnf
检查repl复制功能
masterha_check_repl --conf=/etc/masterha/master.cnf
检查结果is NOT OK
原因是root用户连接没有权限
在server1上给root用户权限
grant all on *.* to root@'%' identified by 'APLOX111fox.';
再检测: 成功
测试能否正常开启
nohup masterha_manager --conf=/etc/masterha/master.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha.log 2>&1 &
关闭
masterha_stop --conf=/etc/masterha/master.cnf
手动切换
死切
[root@server1 ~] systemctl stop mysqld
[root@server4 ~] masterha_master_switch --master_state=dead --conf=/etc/masterha/master.cnf --dead_master_host=172.25.254.1 --dead_master_port=3306 --new_master_host=172.25.254.2 --new_master_port=3306
在server3上测试:
show slave status\G
新的master节点是server2
开启server1,将server1加入到主从复制中去
systemctl start mysqld
mysql -uroot -pAPLOX111fox.
CHANGE MASTER TO MASTER_HOST='172.25.254.1',
MASTER_USER='repl',
MASTER_PASSWORD='APLOX111fox.',
MASTER_AUTO_POSITION=1;
start slave;
show slave status\G
显示新的master节点是server2
活切
删除故障文件
- [root@server4 ~]# cd /etc/masterha/
- [root@server4 masterha]# ls
- [root@server4 masterha]# rm -fr master.failover.complete
- [root@server4 masterha]# ls
输入命令自动替换
masterha_master_switch --conf=/etc/masterha/master.cnf --master_state=alive --new_master_host=172.25.254.1 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=10000
在server2上查看:
show slave status\G
master节点成功切回server1
server3上查看master节点也是server1
半自动
开启在server4后台
- [root@server4 masterha]# nohup masterha_manager --conf=/etc/masterha/zp.cnf &> /dev/null &
在server1上:
关闭掉会自动开启(意外停止)
systemctl stop mysqld
在server4上查看master节点已经是server2
server1:
systemctl start mysqld
mysql -uroot -pAPLOX111fox.
CHANGE MASTER TO MASTER_HOST='172.25.254.1',
MASTER_USER='repl',
MASTER_PASSWORD='APLOX111fox.',
MASTER_AUTO_POSITION=1;
start slave;
show slave status\G
用脚本实现vip漂移
此时在server4上有两个脚本文件:
vim master_ip_failover
vim master_ip_online_change
给两个脚本加上可执行权限
删除上次执行内容,添加脚本执行内容
master_ip_failover_script= /usr/local/bin/master_ip_failover
master_ip_online_change_script= /usr/local/bin/master_ip_online_change
给server2 master配上vip
活切
server4:
masterha_master_switch --conf=/etc/masterha/master.cnf --master_state=alive --new_master_host=172.25.254.1 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=10000
server2 server3:
master节点已经到了server1
server1上:
全自动
server4:
nohup masterha_manager --conf=/etc/masterha/master.cnf &> /dev/null &
server1:
systemctl stop mysqld
server2成为master节点
来源:CSDN
作者:Aplox
链接:https://blog.csdn.net/Aplox/article/details/104728127