目录
一、概述
一、概念
二、组成
三、特点
四、工作原理
二、案例
三、构建MHA
一、基础环境
二、ssh免密登录
三、主从复制
master
slave1
四、MHA安装
一、环境
二、安装node
三、安装manager
一、概述
一、概念
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.100.3 manager(MHA) mgt
192.168.100.4 master master
192.168.100.5 slave slave1
192.168.100.6 slave slave2
三、构建MHA
一、基础环境
关闭防火墙
设置selinux宽容模式
配置静态IP
192.168.100.3 manager(MHA) mgt
192.168.100.4 master master
192.168.100.5 slave slave1
192.168.100.6 slave slave2
修改主机名
192.168.100.3 mgt
192.168.100.4 master
192.168.100.5 slave1
192.168.100.6 slave2
二、ssh免密登录
192.168.100.3
ssh-keygen
for i in 4 5 6;do ssh-copy-id root@192.168.100.$i;done
192.168.100.4
ssh-keygen
for i in 3 5 6;do ssh-copy-id root@192.168.100.$i;done
192.168.100.5
ssh-keygen
for i in 4 3 6;do ssh-copy-id root@192.168.100.$i;done
192.168.100.6
ssh-keygen
for i in 4 5 3;do ssh-copy-id root@192.168.100.$i;done
所有节点配置hosts
vim /etc/hosts
for i in 4 5 6 ;do scp/etc/hosts 192.168.100.$i:/etc;done
192.168.100.3 mgt
192.168.100.4 master
192.168.100.5 slave1
192.168.100.6 slave2
测试
for i in 3 4 5;do ssh 192.168.100.$i ;done (在192.168.100.3测试 )
for i in master slave1 slave2;do ssh $i ;done
三、主从复制
master
vim /etc/my.cnf
[mysqld]
server-id = 1
log-bin = master-bin
log-slave-updates = true
ystemctl restart mariadb
mysql -e "grant replication slave on *.* to 'myslave'@'192.168.100.%' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.100.%' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.100.3' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.100.5' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.100.6' identified by '123.com';"
mysql -e "show master status";
slave1
server-id=2
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.100.%' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.100.%' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.100.3' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.100.4' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.100.6' identified by '123.com';"
mysql -e "change master to master_host='192.168.100.4',master_user='myslave',master_password='123.com',master_log_file='master-bin.000003',master_log_pos=1007;"mysql -e “slave start”
slave2
server-id=3
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.100.%' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.100.%' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.100.3' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.100.4' identified by '123.com';"
mysql -e "grant all privileges on *.* to 'mha'@'192.168.100.5' identified by '123.com';"
mysql -e "change master to master_host='192.168.100.4',master_user='myslave',master_password='123.com',master_log_file='master-bin.000003',master_log_pos=1007;"mysql -e “slave start”
四、MHA安装
一、环境
所有节点安装perl环境
yum install epel-release -y &&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
二、安装node
所有节点安装node(已经下载的源码包)
tar xf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL && make && make install
cd /usr/local/bin (发现存在文件)
yum install epel-release -y && 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 && ls
三、安装manager
tar xf mha4mysql-manager-0.57.tar.gz
cd /root/mha4mysql-manager-0.57
perl Makefile.PL && make && make install
cp sample/scripts/master_ip_failover /usr/local/bin/
cp sample/scripts/master_ip_online_change /usr/local/bin/cd /usr/local/bin
vim master_ip_failover
脚本说明(master_ip_failover)
master_ip_failover 自动切换时 VIP 管理的脚本
master_ip_online_change 在线切换时 vip 的管理
power_manager 故障发生后关闭主机的脚本
send_report 因故障切换后发送报警的脚本
配置文件建立
mkdir /etc/masterha
vim /etc/masterha/app1.cnf
mkdir /var/log/masterha/app1
测试MHA
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不显示vip——ip
停止master后 slave1 会出现vip-ip
查看slave1状态
slave2
实现MySQL高可用MHA