MHA是目前比较成熟的mysql高可用集群方式之一.
一、参考文档:
1.官方文档:【 https://github.com/yoshinorim/mha4mysql-manager/wiki 】
2.个人博客: 好多
3.下载地址:
(1)manager:【 https://github.com/yoshinorim/mha4mysql-manager/releases 】
(2)node: 【 https://github.com/yoshinorim/mha4mysql-node/releases/tag/v0.58 】
二、基础环境
1.操作系统:Centos 7.4
2.mysql版本:percona-mysql 5.7.22
3.MHA版本:0.58
4.服务器信息
三、部署流程
1.安装node
# master,slave-01
(1)下载安装包
mkdir -p /opt/upload/mha_packges cd opt/upload/mha_packges wget 'https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58-0.el7.centos.noarch.rpm'
(2)安装node
yum -y install perl-DBD-MySQL -y yum -y localinstall mha4mysql-node-0.58-0.el7.centos.noarch.rpm
2.安装manager
#172.18.6.14
(1)下载安装包
mkdir -p /opt/upload/mha_packges cd /opt/upload/mha_packges wget 'https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58-0.el7.centos.noarch.rpm' wget 'https://github.com/yoshinorim/mha4mysql-manager/releases/download/v0.58/mha4mysql-manager-0.58-0.el7.centos.noarch.rpm'
(2)安装manager及node
yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager yum -y localinstall mha4mysql-node-0.58-0.el7.centos.noarch.rpm yum -y localinstall mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
3.添加hosts
#master-01,slave-01,slave-02操作
cat <<EOF>> /etc/hosts 172.18.6.15 mysql_master-01 172.18.6.13 mysql_slave-01 172.18.6.14 mysql_slave-02 EOF
4.配置免密登陆
(1)配置三台服务器免密登陆
(2)测试命令:masterha_check_ssh --conf=/opt/ mha/conf/default.cnf
5.创建manager工作目录
mkdir -p /opt/mha/{manager_data,remote_data,manager_logs,conf}
6.生成manager配置文件
cat <<EOF> /opt/mha/conf/default.cnf [server default] # mha_directory # manager_workdir=/opt/mha/manager_data manager_log=/opt/mha/manager_logs/manager.log remote_workdir=/opt/mha/remote_data # mha_setting # ping_interval=1 shutdown_script="" master_ip_failover_script="/usr/bin/master_ip_failover" master_ip_online_change_script="/usr/bin/master_ip_online_change" # DATABASE # user=mha password=dingkai123 #最好不要用特殊字符,mha难以识别 # SSH # ssh_user=root ssh_port=10022 repl_user=repl #mysql主从复制账号 repl_password=dingkai.123 #mysql主从复制账号密码 master_binlog_dir=/opt/mysql/mysql_log #binlog目录 [server1] hostname= mysql_master-01 port=3306 master_binlog_dir=/opt/mysql/mysql_log candidate_master=1 check_repl_delay=0 [server2] hostname=mysql_slave-01 port=3306 master_binlog_dir=/opt/mysql/mysql_log candidate_master=1 check_repl_delay=0 [server3] hostname=mysql_slave-02 port=3306 master_binlog_dir=/opt/mysql/mysql_log no_master=1 [binlog1] hostname=mysql_master-01 EOF
5.在master创建mha管理账号
grant all privileges on *.* to mha@172.18.6.% identified by 'MFdb@2018!';
6.修改‘master_ip_failover’ ,'master_ip_online_change'脚本(这两个脚本在manager源码中的 mha4mysql-manager-0.58/samples/scripts 下)
(1)修改master_ip_failover
添加
my $vip = '172.18.6.251/32'; my $key = '0'; my $ssh_port = '10022'; my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip"; my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
(2)修改master_ip_online_change
#添加以下内容
my $vip = '172.18.6.251/32'; my $ssh_port = '10022'; my $if = "eth0";
(3)添加脚本到 /usr/bin/下
cp -a master_ip_failover master_ip_online_change /usr/bin/ chmod +x /usr/bin/master_ip_failover chmod +x /usr/bin/master_ip_online_change
6.配置虚拟网卡
#master实例操作
ifconfig eth0:0 172.18.6.201/32
7.检查复制情况
#slave-02执行
masterha_check_repl --conf=/opt/mha/conf/default.cnf #检查复制状态
masterha_manager --conf=/opt/mha/conf/default.cnf #启动mha
masterha_check_status --conf=/opt/mha/conf/default.cnf #查看mha运行状态
8.使用脚本控制 (1)创建mha控制脚本 cat <<EOF> /usr/bin/mha.sh #!/usr/bin/bash #filename: mha.sh #用途:启动关闭MHA #VARIABLES config_file="/opt/mha/conf/default.cnf " base_path="/opt/mha " log_path="/opt/mha/manager_logs" masterha_manager="/usr/bin/masterha_manager" masterha_stop="/usr/bin/masterha_stop" masterha_check_status="/usr/bin/masterha_check_status" mha_status=$(masterha_check_status --conf=/opt/mha/conf/default.cnf |grep -w 'pid'|awk -F" " '{print $2}'|awk -F":" '{print $2}'|awk -F")" '{print $1}') #functions start(){ if [ -z ${mha_status} ] then /usr/bin/nohup ${masterha_manager} --conf=${config_file} > ${log_path}/nohup.log 2>&1 & else echo "MHA is Running!" fi } stop(){ if [ ! -z ${mha_status} ] then ${masterha_stop} --conf=${config_file} else echo "MHA is not running!" fi }
(2)脚本命令
mha.sh start #启动mha
mha.sh stop #停止mha
mha.sh status #查看mha状态
注意:当master宕机后,mha会自动将一个slave节点提升为主,随后mha自动停止运行,在手动恢复数据库之前,mha都是不可用的