本节主要学习了Mariadb高可用MHA的概述,案例如何构建MHA
提示:以下是本篇文章正文内容,下面案例可供参考
MHA(MasterHigh Availability)是一套优秀的MySQL高可用环境下故障切换和主从复制的软件。
MHA 的出现就是解决MySQL 单点的问题。
MySQL故障切换过程中,MHA能做到0-30秒内自动完成故障切换操作。
MHA能在故障切换的过程中最大程度上保证数据的一致性,以达到真正意义上的高可用。
MHA manager 管理节点
MHA node 数据节点
每个节点上都需要安装
自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据不丢失
使用半同步复制,可以大大降低数据丢失的风险,如果只有一个slave已经收到了最新的二进制日志,MHA可以将最新的二进制日志应用于其他所有的slave服务器上,因此可以保证所有节点的数据一致性
目前MHA支持一主多从架构,最少三台服务,即一主两从
从宕机崩溃的master 保存二进制日志事件(binlog events);
识别含有最新的更新slave日志
应用差异的中继日志(relay log)到其他的slave
应用从master保存的二进制日志事件
提升一个slave为新的master
使其他的slave连接新的master进行复制
192.168.6.3 mgt
192.168.115.3 mgt
192.168.115.4 master
192.168.115.5 slave1
192.168.115.6 slave2
在mgt内配置/etc/hosts
并传输给其他服务器
slave1
slave2
ssh-keygen
for i in 4 5 6;do ssh-copy-id [email protected].$i;done
for i in 6 4 5;do ssh 192.168.6.$i hostname;done或手动
6: for i in 3 4 5;do ssh 192.168.6.$i hostname;done
5: for i in 6 4 3;do ssh 192.168.6.$i hostname;done
4: for i in 6 3 5;do ssh 192.168.6.$i hostname;done
把 yes 都确定掉
for i in 4 5 6;do ssh 192.168.6.$i yum install -y mariadb mariadb-server mariadb;done
vim /etc/my.cnf
[mysqld]
server-id = 20
log-bin = master-bin
log-slave-updates = true
mysql -e "grant replication slave on *.* to 'myslave'@'192.168.6.%' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.6.%' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.6.3' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.6.5' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.6.6' identified by '123.com';"
mysql -e "show master status"
slave1
server-id=30
log-bin=master-bin
relay-log=relay-log-bin
relay-log-index=relay-log-bin.index
mysql -e "grant replication slave on *.* to 'myslave'@'192.168.6.%' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.6.%' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.6.3' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.6.5' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.6.6' identified by '123.com';"
slave2
server-id=31
log-bin=master-bin
relay-log=relay-log-bin
relay-log-index=relay-log-bin.index
mysql -e "grant replication slave on *.* to 'myslave'@'192.168.6.%' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.6.%' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.6.3' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.6.5' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.6.4' identified by '123.com';"
mysql -e "change master to master_host='192.168.115.4',master_user='myslave',master_password='123.com',master_log_file='master-bin.000003',master_log_pos=1166;"
yum install epel-release -y
yum -y install perl-DBD-MySQL perl-ExtUtils-MakeMaker perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes perl-CPAN
tar xf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL && make && make install
cd /usr/local/bin—看到脚本就OK
master_ip_failover 自动切换时 VIP 管理的脚本
master_ip_online_change 在线切换时 vip 的管理
power_manager 故障发生后关闭主机的脚本
send_report 因故障切换后发送报警的脚本
mkdir /etc/masterha
vim /etc/masterha/app1.cnf
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 &
停用master的mariadb服务
地址出现
修改app1.cnf启动配置 添加master主机配置信息
分别在slave上重新指定master主机及binlog日志同步信息