千家信息网

mysql MHA 高可用架构部署

发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,MHA简介:即Master High Availability Manager and Tools for MySQL,是日本的一位MySQL专家采用Perl语言编写的一个脚本管理工具,该工具仅适用于
千家信息网最后更新 2025年02月01日mysql MHA 高可用架构部署

MHA简介:即Master High Availability Manager and Tools for MySQL,是日本的一位MySQL专家采用Perl语言编写的一个脚本管理工具,

该工具仅适用于MySQL Replication(二层)环境,目的在于维持Master主库的高可用性。

在 MySQL 故障切换过程中,MHA 能做到 0~30 秒之内自动完成数据库的故障

切换操作,并且在进行故障切换的过程中,MHA 能最大程度上保证数据库的一致性,以达到

真正意义上的高可用。

MHA 由两部分组成:MHA Manager(管理节点)和 MHA Node(数据节点)。MHA Manager

可以独立部署在一台独立的机器上管理多个Master-Slave集群,也可以部署在一台Slave上。

当 Master 出现故障是,它可以自动将最新数据的 Slave 提升为新的 Master,然后将所有其他

的 Slave 重新指向新的 Master。整个故障转移过程对应用程序是完全透明的。

整体环境介绍: master1 :192.168.9.25 master2 : 192.168.9.26 slave1 : 192.168.9.29 slave2 : 192.168.9.30 manager 节点: 192.168.9.27 lvs1: 192.168.9.27 lvs2 : 192.168.9.28 整体搭建流程思路:所有的mysql服务器上都要安装node软件,在manager管理服务器(9.27)上安装manager和node节点。然后所有的mysql数据里要创建mha管理用户,这里我起名字叫 admin
下面演示具体搭建过程:
一:9.27管理节点部署,安装node和manager软件,只要安装上既可以了,不用开启 首先上传node和manager软件安装镜像(我硬盘里有)

[root@lvs-a ~]# rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm (6.5系统)

[root@lvs-a ~]# #rpm -ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm (5.8)


[root@lvs-a ~]# ls /etc/yum.repos.d/

base.repo epel.repo epel-testing.repo rhel-debuginfo.repo

[root@lvs-a ~]# yum -y install perl-DBD-MySQL ncftp

[root@lvs-a tarbag]# tar -zxf mha4mysql-node-0.56..tar.gz -C /usr/local ## node软件解压

[root@lvs-a tarbag]# cd /usr/local/mha4mysql-node-0.56

[root@lvs-a tarbag]# perl Makefile.PL

[root@lvs-a tarbag]# make && make install ## node安装

[root@lvs-a tarbag]# yum -y install perl-Config-Tiny perl-Params-Validate perl-Log-Dispatch perl-Parallel-ForkManager

[root@lvs-a tarbag]# tar -zxf mha4mysql-manager-0.56.tar.gz -C /usr/local/

[root@lvs-a tarbag]# cd /usr/local/mha4mysql-manager-0.56 ##manager软件安装

[root@lvs-a mha4mysql-manager-0.56]# perl Makefile.PL

[root@lvs-a mha4mysql-manager-0.56]# make && make install

二:9.25主节点部署,只安装node即可

[root@master1~]#rpm-ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
[root@master1 ~]# ls /etc/yum.repos.d/
base.repo epel.repo epel-testing.repo rhel-debuginfo.repo
[root@master1 ~]# yum -y install perl-DBD-MySQL ncftp
[root@master1 tarbag]# tar -zxf mha4mysql-node-0.56..tar.gz -C /usr/local
[root@master1 tarbag]# cd /usr/local/mha4mysql-node-0.56
[root@master1 mha4mysql-node-0.56]# perl Makefile.PL
[root@master1 mha4mysql-node-0.56]# make && make install
[root@master1 mha4mysql-node-0.56]# ln -s /usr/local/mysql/bin/* /usr/local/bin/

安装mysql软件 具体过程略过,参考: MySQL 主从同步复制架构搭建 文档

创建mha管理账号
Mysql> grant all on *.* to 'admin'@'%' identified by '123456';
Mysql> flush privileges;
创建mysql主从管理账号
Mysql> grant replication slave on *.* to 'repl'@'%' identified by '123456';
Mysql> flush privileges;


三、9.26主备节点部署(步骤同主库一样)

[root@master2~]#rpm-ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
[root@master2 ~]# ls /etc/yum.repos.d/
base.repo epel.repo epel-testing.repo rhel-debuginfo.repo
[root@master2 ~]# yum -y install perl-DBD-MySQL ncftp
[root@master2 tarbag]# tar -zxf mha4mysql-node-0.56..tar.gz -C /usr/local
[root@master2 tarbag]# cd /usr/local/mha4mysql-node-0.56
[root@master2 mha4mysql-node-0.56]# perl Makefile.PL
[root@master2 mha4mysql-node-0.56]# make && make install
[root@master2 mha4mysql-node-0.56]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
创建mha管理账号 其实可以不建立的,因为开始之前你要做让master1去恢复别的mysql,只要master1上有即可。
Mysql> grant all on *.* to 'admin'@'%' identified by '123456';
Mysql> flush privileges;
创建mysql主从管理账号
Mysql> grant replication slave on *.* to 'repl'@'%' identified by '123456';
Mysql> flush privileges;

四:9.29 从节点部署

[root@slave1~]#rpm-ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
[root@slave1 ~]# ls /etc/yum.repos.d/
base.repo epel.repo epel-testing.repo rhel-debuginfo.repo
[root@slave1 ~]# yum -y install perl-DBD-MySQL ncftp
[root@slave1 tarbag]# tar -zxf mha4mysql-node-0.56..tar.gz -C /usr/local
[root@slave1 tarbag]# cd /usr/local/mha4mysql-node-0.56
[root@slave1 mha4mysql-node-0.56]# perl Makefile.PL
[root@slave1 mha4mysql-node-0.56]# make && make install
[root@slave1 mha4mysql-node-0.56]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
创建mha管理账号
Mysql> grant all on *.* to 'admin'@'%' identified by '123456';
Mysql> flush privileges;

五:9.30从库操作参考9.29.


六:至此需要安装的软件,已经基本完成,下面具体展示如何配置manager管理节点,mha上层可以通过keepalive部署VIP,程序连接数据库使用VIP,从而实现后台数据库的故障切换透明化。

[root@lvs-a ~]# mkdir -p /etc/masterha

[root@lvs-a ~]# mkdir -p /masterha/app1

[root@lvs-a mha4mysql-manager-0.56]# cp samples/conf/* /etc/masterha/

[root@lvs-a ~]# cat /etc/masterha/app1.cnf
[server default]
manager_workdir=/masterha/app1
manager_log=/masterha/app1/manager.log
user=admin
password=123456
ssh_user=root
repl_user=repl
repl_password=123456
ping_interval=1 #每秒ping一次
shutdown_script=""
master_ip_failover_script="/usr/local/bin/master_ip_failover"   ##这个脚本接下来会创建         
master_ip_online_change_script=""
report_script=""
[server1]
hostname=192.168.9.26
master_binlog_dir=/mysql/data/log
candidate_master=1              ##主库要有这个参数
[server2]
hostname=192.168.9.25
master_binlog_dir=/mysql/data/log
candidate_master=1
[server3]
hostname=192.168.9.29
master_binlog_dir=/data/log
[server4]
hostname=192.168.9.30
master_binlog_dir=/data/log/ #对应路径

[root@lvs-a u01]# cat /usr/local/bin/master_ip_failover  
#!/bin/bash
#--------------第一部分:变量及函数定义-----------------#
###Begin Variables define###
ssh_port=22
cmd=/sbin/ifconfig
vip=192.168.9.232
device=eth0:0
netmk=255.255.255.0
start_vip="${cmd} ${device} ${vip} netmask ${netmk} up"
stop_vip="${cmd} ${device} ${vip} netmask ${netmk} down"
###End Variables define###

###Begin Status Funciont###
status()
{
exit 0
}
###End Status Funciont###

###Begin Stop Or Stopssh Funciont###
stop()
{
exit 0
}
###End Stop Or Stopssh Funciont###

###Begin Start Funciont###
start()
{
/usr/bin/ssh -p ${ssh_port} ${ssh_user}@${orig_master_host} ""${stop_vip}""
/usr/bin/ssh -p ${ssh_port} ${ssh_user}@${new_master_host} ""${start_vip}""
exit 0
}
###End Start Funciont###
#--------------第一部分:变量及函数定义-----------------#

#--------------第二部分:命令行参数-----------------#
###Begin Get The Command-Line Parameters###
###eval set -- "`getopt -a -q -o n -l command::,ssh_user:,orig_master_host:,orig_master_ip:,orig_master_port:,new_master_host:,new_master_ip:,new_master_port:,new_master_user:,new_master_password: -- "$@"`"
eval set -- "`getopt -a -q -o n -l command::,ssh_user:,orig_master_host:,orig_master_ip:,new_master_host:,new_master_ip: -- "$@"`"

if [ $? != 0 ] ; then echo "Terminating..." >&2 ;exit 1;fi

while true
do
case "$1" in
--command)
command="${2}";
shift;;
--ssh_user)
ssh_user="${2}";
shift;;
--orig_master_host)
orig_master_host="${2}";
shift;;
--orig_master_ip)
orig_master_ip="${2}";
shift;;
--new_master_host)
new_master_host="${2}";
shift;;
--new_master_ip)
new_master_ip="${2}";
shift;;
--)
shift;
break;;
esac
shift
done
###End Get The Command-Line Parameters###
#--------------第二部分:命令行参数-----------------#

#--------------第三部分:函数调用-----------------#
if [ "${command}" == "status" ];
then
status;
fi
if [ "${command}" == "stop" ] || [ "${command}" == "stopssh" ] ;
then
stop;
fi
if [ "${command}" == "start" ];
then
start;
fi
七:建立互信,注意要涵盖所有服务器。下面仅展示一个服务器的。

[root@lvs-a ~]# ssh-keygen -t rsa

[root@lvs-a ~]#ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.9.25

[root@lvs-a ~]#ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.9.26

[root@lvs-a ~]#ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.9.29

[root@lvs-a ~]#ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.9.30

八:检查mha配置是否正确。

1.检查ssh是否配置成功

[root@lvs-a ~]#masterha_check_ssh --conf=/etc/masterha/app1.cnf

最后显示如下字段说明成功:

Tue Jun 30 01:58:05 2015 - [info] All SSH connection tests passed successfully.

2.检查mysql主从复制状态是否成功

[root@lvs-a ~]#masterha_check_repl --conf=/etc/masterha/app1.cnf

最后出现MySQL Replication Health is OK.字段说明成功。

3. 启动管理节点进程

[root@lvs-a ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf > /tmp/mha_manager.log < /dev/null 2>&1 &

[1] 10085

4. 检查mha主从状态

[root@lvs-a ~]# masterha_check_status --conf=/etc/masterha/app1.cnf

显示如下

app1 (pid:10085) is running(0:PING_OK), master:192.168.153.147

5. 停止mha

[root@lvs-a ~]# masterha_stop --conf=/etc/masterha/app1.cnf

九:安装lvs+keepalived的具体过程参考,文档 LVS+keepalived的安装部署 http://blog.itpub.net/29654823/viewspace-1844282/

十:验证环境的正确性:

关闭现在的主(9.25),vip9.232 会自动飘向主备(9.26),从而实现了主的高可用,这时候,manger(9.27)进程也就死了,需要手动起来。





0