MHA高可用群集基本部署(纯实战)

ShiShuo 2019-12-23

MHA高可用群集基本部署

MHA概述

MHA目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Facebook公司)开发。

MySQL高可用性环境下故障切换和主从提升的高可用软件。在MySQL故障切换过程中,MHA能做到在0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程中,MHA能在最大程度上保证数据的一致性,以达到真正意义上的高可用。

MHA还提供在线主库切换的功能,能够安全地切换当前运行的主库到一个新的主库中(通过将从库提升为主库),大概0.5-2秒内即可完成。

基本部署实验流程

一、实验前期准备

名称角色地址
centos7-2master192.168.142.203
centos7-3slave1192.168.142.132
centos7-minslave2192.168.142.172
centos7-4manger(监控端)192.168.142.136

二、开始实验

1、所有服务器环境准备

安装epel源(不进行检查)

[ ~]# yum -y install epel-release --nogpgcheck

安装环境包

[ ~]# yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parellel-ForkManager perl-ExtUtils-CBuider perl-Extutils-MakeMaker perl-CPAN

其中,

perl-DBD-MySQL:针对mysql的Perl模块

perl-Config-Tiny:Perl模块的配置文件

perl-Log-Dispatch:日志文件

perl-Parellel-ForkManager:Perl的多线程管理

perl-ExtUtils-CBuider:管理工具

perl-Extutils-MakeMaker:管理工具

perl-CPAN:Perl模块中的数据库

注意:

? MHA 软件包对于每个操作系统版本不一样,这里Cent0S7.4必须选择0. 57版本,

node组件:所有服务器上都要装(包括manager自身)
manager组件:仅需要在manager端上安装

安装node组件

[ mha4mysql-node-0.57]# tar zxvf mha4mysql-node-0.57.tar.gz -C /opt/
[ mha4mysql-node-0.57]# perl Makefile.PL
[ mha4mysql-node-0.57]# make
[ mha4mysql-node-0.57]# make install

2、主、从服务器安装mysql

解压软件包、安装软件

[ ~]# yum install gcc gcc-c++ ncurses-devel perl-Module-Install -y
[ ~]# tar zxf cmake-2.8.6.tar.gz -C /opt
[ ~]# cd /opt/cmake-2.8.6
[ cmake-2.8.6]# gmake && gmake install
[ ~]# tar zxf mysql-5.6.36.tar.gz -C /opt/
[ ~]# cd /opt/mysql-5.6.36

//配置并编译安装
[ mysql-5.6.36]# ./configure
[ mysql-5.6.36]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc
[ mysql-5.6.36]# make && make install

提权,配置环境变量

[ mysql-5.6.36]# cp support-files/my-default.cnf /etc/my.cnf              
//使用模板文件进行覆盖
[ mysql-5.6.36]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[ mysql-5.6.36]# chmod +x /etc/rc.d/init.d/mysqld
[ mysql-5.6.36]# chkconfig --add mysqld
[ mysql-5.6.36]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[ mysql-5.6.36]# source /etc/profile
[ mysql-5.6.36]# useradd -M -s /sbin/nologin mysql                  //建立程序性用户
[ mysql-5.6.36]# chown -R mysql.mysql /usr/local/mysql

//初始化数据库
[ mysql-5.6.36]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql

修改mysql配置文件并开启

[ ~]# vim /etc/my.cnf
//主服务器上
[mysqld]
server-id = 10
log-slave-updates = true
log-bin = master-bin

//从服务器上
[mysqld]
server-id = 11           //两台从服务器的id不能一样
log-bin = master-bin
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index

//建立软链接方便电脑识别
[ ~]# ln -s /usr/local/mysql/bin/mysql /usr/local/sbin/
[ ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/local/sbin/

//安全开启服务
[ ~]# /usr/local/mysql/bin/mysqld_safe --user=mysql &

在每台数据库中进行提权

[ ~]# mysql -uroot -p
#主从同步账户权限
mysql > grant replication slave on *.* to ‘myslave‘@‘192.168.142.%‘ identified by ‘123123‘;
#manger监管用户权限
mysql > grant all privileges on *.* to ‘mha‘@‘192.168.142.%‘ identified by ‘mysql‘;

#mha用户在每个库中进行授权,否则从库通过主机名连接主库会报错
mysql > grant all privileges on *.* to ‘mha‘@‘master‘ identified by ‘mysql‘;
mysql > grant all privileges on *.* to ‘mha‘@‘slave1‘ identified by ‘mysql‘;
mysql > grant all privileges on *.* to ‘mha‘@‘slave2‘ identified by ‘mysql‘;

部署主从同步

#主服务器查看二进制文件及节点号
mysql > show master status;

#从服务器开启主从
mysql > change master to master_host=‘192.168.142.203‘,master_user=‘myslave‘,master_password=‘123123‘,master_log_file=‘master-bin.000001‘,master_log_pos=1335;
mysql > start slave;
mysql > set global read_only=1;    #从库为只读模式

3、配置manager端

安装manager组件(务必保证node组件已完成安装)

[ mha]# tar zxf mha4mysql-manager-0.57.tar.gz -C /opt/
[ mha]# cd /opt/mha4mysql-manager-0.57/
[ mha4mysql-manager-0.57]# perl Makefile.PL
[ mha4mysql-manager-0.57]# make
[ mha4mysql-manager-0.57]# make install

其中,manager和node中存在着许多工具

manager工具(/usr/local/bin)

masterha_manager:启动脚本

masterha_master_monitor:检测master是否宕机

masterha_master_switch:控制故障转移(自动/手动)

masterha_check_repl:检查mysql的复制情况

masterha_check_ssh:检查MHA的SSH配置情况

masterha_check_status:检查MHA当前运行情况(节点是否健康)

masterha_conf_host:添加/删除配置的serer信息

masterha_stop:关闭manager

node脚本(/usr/local/bin)

apply_diff_relay_logs:识别差异的中继日志

purge_relay_logs:清除中继日志

save_binary_logs:用于保存/复制master的二进制日志文件

filter_mysqlbinlog:去除不必要的rollback事件

4、 所有主机之间实现免密钥登陆

//manager端→所有数据库免密钥(密码:空)
[ ~]# ssh-keygen -t rsa                //生成密钥对
[ ~]# ssh-copy-id 192.168.142.203           //推送至master
[ ~]# ssh-copy-id 192.168.142.132           //推送至slave1
[ ~]# ssh-copy-id 192.168.142.172           //推送至slave2

//master端→连接的从数据库
[ ~]# ssh-keygen -t rsa                 //生成密钥对
[ ~]# ssh-copy-id 192.168.142.132          //推送至slave1
[ ~]# ssh-copy-id 192.168.142.172          //推送至slave2

//slave1→master&slave2
[ ~]# ssh-keygen -t rsa
[ ~]# ssh-copy-id 192.168.142.203
[ ~]# ssh-copy-id 192.168.142.172

//slave2→master&slave1
[ ~]# ssh-keygen -t rsa
[ ~]# ssh-copy-id 192.168.142.203
[ ~]# ssh-copy-id 192.168.142.132

5、Manager端配置MHA

复制MHA脚本

[ scripts]# cp -ra /opt/mha4mysql-manager-0.57/samples/scripts /usr/local/bin/

[ scripts]# ll /usr/local/bin/scripts/
总用量 32
-rwxr-xr-x. 1 1001 1001  3648 5月  31 2015 master_ip_failover                //实现故障转移,VIP的浮动
-rwxr-xr-x. 1 1001 1001  9870 5月  31 2015 master_ip_online_change            //在线切换
-rwxr-xr-x. 1 1001 1001 11867 5月  31 2015 power_manager             //故障发生后关闭主机
-rwxr-xr-x. 1 1001 1001  1360 5月  31 2015 send_report                  //发生故障后报警

[ scripts]# cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin/

修改故障转移脚本

[ scripts]# vim /usr/local/bin/master_ip_failover
##原内容全部删除,新增
#!/usr/bin/env perl
use strict;
use warnings FATAL => ‘all‘;

use Getopt::Long;

my (
$command, $ssh_user, $orig_master_host, $orig_master_ip,
$orig_master_port, $new_master_host, $new_master_ip, $new_master_port
);
#############################添加内容部分#########################################
my $vip = ‘192.168.142.100‘;                    #飘逸地址
my $brdc = ‘192.168.142.255‘;                #广播地址
my $ifdev = ‘ens33‘;                   #网卡名
my $key = ‘1‘;                #虚拟网卡的序列号
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";                #使用虚拟地址进行
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
my $exit_code = 0;
#my $ssh_start_vip = "/usr/sbin/ip addr add $vip/24 brd $brdc dev $ifdev label $ifdev:$key;/usr/sbin/arping -q -A -c 1 -I $ifdev $vip;iptables -F;";
#my $ssh_stop_vip = "/usr/sbin/ip addr del $vip/24 dev $ifdev label $ifdev:$key";
##################################################################################
GetOptions(
‘command=s‘ => \$command,
‘ssh_user=s‘ => \$ssh_user,
‘orig_master_host=s‘ => \$orig_master_host,
‘orig_master_ip=s‘ => \$orig_master_ip,
‘orig_master_port=i‘ => \$orig_master_port,
‘new_master_host=s‘ => \$new_master_host,
‘new_master_ip=s‘ => \$new_master_ip,
‘new_master_port=i‘ => \$new_master_port,
);

exit &main();

sub main {

print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";

if ( $command eq "stop" || $command eq "stopssh" ) {

my $exit_code = 1;
eval {
print "Disabling the VIP on old master: $orig_master_host \n";
&stop_vip();
$exit_code = 0;
};
if () {
warn "Got Error: \n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) {

my $exit_code = 10;
eval {
print "Enabling the VIP - $vip on the new master - $new_master_host \n";
&start_vip();
$exit_code = 0;
};
if () {
warn ;
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
print "Checking the Status of the script.. OK \n";
exit 0;
}
else {
&usage();
exit 1;
}
}
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}

sub usage {
print
"Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}

创建MHA软件目录并修改配置文件

[ bin]# mkdir /etc/masterha
[ bin]# cp /opt/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/             #从挂载目录拷贝配置文件

[ bin]# vim /etc/masterha/app1.cnf
#删除原有,新增
[server default]
    #manager日志文件位置
manager_log=/var/log/masterha/app1/manager.log
    #manager工作目录
manager_workdir=/var/log/masterha/app1
    #master保存binlog位置,位置要与master的设置相同
master_binlog_dir=/usr/local/mysql/data
    #设置failover时自动切换脚本
master_ip_failover_script=/usr/local/bin/master_ip_failover
    #设置手动切换时切换的脚本
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
    #监管用户mha的密码
password=mysql
    #监管用户
user=mha
    #监控主控,发送ping包间隔(默认3s),三次无回应视为failover
ping_interval=1
    #设置远端mysql在发送切换时binlog的保存位置
remote_workdir=/tmp
    #复制用户密码(之前在数据库中设置的)
repl_password=123123
    #复制用户
repl_user=myslave
    #指定需要检查到的从服务器地址
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.142.132 -s 192.168.142.172
    #设置故障发生后关闭故障主机的脚本“”表示没有使用
shutdown_script=""
    #设置ssh登陆用户名
ssh_user=root

[server1]
hostname=192.168.142.203
port=3306

[server2]
    #设置为候选master
candidate_master=1
check_repl_delay=0
hostname=192.168.142.132
port=3306

[server3]
hostname=192.168.142.172
port=3306

执行健康检查

//检查密钥对是否生效
[ bin]# masterha_check_ssh --conf=/etc/masterha/app1.cnf
//检查复制文件能否生效
[ bin]# masterha_check_repl --conf=/etc/masterha/app1.cnf

进行健康检查时,可能会出现ERROR的情况,

健康检查ERROR解决方法
错误:
Sat Dec 14 22:01:09 2019 - [error][/usr/local/share/perl5/MHA/ServerManager.pm, ln492] Server 192.168.142.172(192.168.142.172:3306) is dead, but must be alive! Check server settings.
Sat Dec 14 22:01:09 2019 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln427] Error happened on checking configurations. at /usr/local/share/perl5/MHA/MasterMonitor.pm line 402.
Sat Dec 14 22:01:09 2019 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln525] Error happened on monitoring servers.
解决方法:
(1)关闭防火墙
(2)清除iptables规则:iptables -F

首次配置需在主服务器上手动开启虚拟IP

[ ~]# /sbin/ifconfig ens33:1 192.168.142.100/24

启动MHA

[ app1]# 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 &

//查看MHA状态
[ app1]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:55255) is running(0:PING_OK), master:192.168.142.203

//查看MHA日志文件
[ app1]# cat /var/log/masterha/app1/manager.log

其中,

--conf=/etc/masterha/app1.cnf:配置文件位置

--remove_dead_master_conf:宕机删除

--ignore_last_failover:忽略故障转移

2>&1:将错误输出转为正常输出

感谢阅读~ ~ ~ ~

相关推荐