MHA目前在MySQL高可用方面是一个相对成熟的解决方案
但是在搭建的过程中会经常报错,且MHA的构建综合了主从复制,所以MHA的安装要思路清晰才可
日本DeNA公司 youshimaton(现就职于 Facebook公司)开发
一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件
支持故障切换
在MySQL故障切换过程中,MHA能做到在0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程中,MHA能在最大程度上保证数据的一致性,以达到真正意义上的高可用
MHA还提供在线主库切换的功能,能够安全地切换当前运行的主库到一个新的主库中(通过将从库提升为主库),大概0.5-2秒内即可完成
MHA Manager(管理节点)
用来接收外部信号,监控下方数据节点的工作状态
专门用来监控下面数据库的服务状态 一旦某台服务器出现问题 就会切换
MHA Node(数据节点)
工作的单位,负责具体的工作
自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据的不丢失
使用 MySQL55的半同步复制,可以大大降低数据丢失的风险
传统mysql主从架构存在单点故障的问题
传统架构中,只有一个mysql主服务器,所以当出现单点故障的时候,整个服务器群集就会瘫痪掉
为了解决这种情况,我们需要在主服务器宕机的时候,重新建立一台主服务器,负责监控等工作
1.本案例环境
服务器CentOS7.4(64位) MHA-manager/192.168.195.132
管理节点,安装manager组件
MHA搭建
.故障模拟
主库失效
备选主库成为主库
从库2将备选主库指向为主库
安装三台mysql5.6版本数据库
以下操作同时都要在三台机器上
yum安装依赖包
[root@localhost opt]# yum -y install ncurses-devel gcc-c++ perl-Module-Install
[root@master opt]# tar xzvf cmake-2.8.6.tar.gz
[root@master opt]# cd cmake-2.8.6/
[root@master cmake-2.8.6]# ./configure
[root@master cmake-2.8.6]# gmake && gmake install
[root@master cmake-2.8.6]# cd ..
[root@master opt]# tar xzvf mysql-5.6.36.tar.gz
[root@master opt]# cd mysql-5.6.36/
[root@master mysql-5.6.36]# cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DSYSCONFIDIR=/etc
[root@master mysql-5.6.36]# make && make install
[root@master mysql-5.6.26]# cp support-files/my-default.cnf /etc/my.cnf
[root@master mysql-5.6.26]#
[root@master mysql-5.6.26]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@master mysql-5.6.26]# chmod +x /etc/rc.d/init.d/mysqld
[root@master mysql-5.6.26]# chkconfig --add mysqld
[root@master mysql-5.6.26]# echo "PATH=$PATH:/usr/local/mysql/bin">>/etc/profile
[root@master mysql-5.6.26]# source /etc/profile
[root@master mysql-5.6.36]# groupadd mysql
[root@master mysql-5.6.26]# useradd -M -s /sbin/nologin mysql -g mysql
[root@master mysql-5.6.26]# chown -R mysql:mysql /usr/local/mysql/
[root@master mysql-5.6.36]# mkdir -p /data/mysql
[root@master mysql-5.6.26]# /usr/local/mysql/scripts/mysql_install_db \
--user=mysql \
--basedir=/usr/local/mysql/ \
--datadir=/usr/local/mysql/data
三台机器分别设置my.cnf
#master:
vim /etc/my.cnf
server-id = 1
log_bin = master-bin
log-slave-updates = true
#slave1:
vim /etc/my.cnf
server-id = 2
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
#slave2:
vim /etc/my.cnf
server-id = 3
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
三台一起启动mysql
[root@master mysql-5.6.36]# ln -s /usr/local/mysql/bin/mysql /usr/sbin/
[root@master mysql-5.6.36]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
[root@master mysql-5.6.36]# /usr/local/mysql/bin/mysqld_safe --user=mysql &
[1] 99496
[root@master mysql-5.6.36]# Logging to '/usr/local/mysql/data/master.err'.
200827 22:18:46 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
回车退出后台模式
[root@master mysql-5.6.36]# netstat -antp | grep 3306
mysq|> GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.110.%' IDENTIFIED BY '123';
mysql>grant all privileges on *.* to 'mha'@'192.168.110.%' identified by 'manager';
mysq|> flush privileges;
2.下面三条授权按理论是不用添加的,但是做案例实验环境时候通过MHA检查MySQL主从有报错,
报两个从库通过主机名连接不上主库,所以所有数据库加.上下面的授权。
mysql> grant all privileges on *.* to 'mha'@'master' identified by 'manager';
mysql> grant all privileges on *.* to 'mha'@'slave1' identified by 'manager';
mysql> grant all privileges on *.* to 'mha'@'slave2' identified by 'manager';
3.在Mysql1主机上查看二进制文件和同步点
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000002 | 120 | | | |
+-------------------+----------+--------------+------------------+-------------------+
#####分别在两台从服务器上进行授权账号 同步主从复制
change master to master_host='192.168.110.133',master_user='myslave',master_password='123',master_log_file='master-bin.000002',master_log_pos=120;
start slave;
4.接下来在Mysq|2和Mysql3分别执行同步。
#####分别在两台从服务器上进行授权账号 同步主从复制
change master to master_host='192.168.110.133',master_user='myslave',master_password='123',master_log_file='master-bin.000002',master_log_pos=120;
start slave;
show slave status\G;
Relay_Log_Pos: 284
Relay_Master_Log_File: master-bin.000002
Slave_IO_Running: Yes 出现呢yes即可
Slave_SQL_Running: Yes
5.查看I0和SQL线程都是yes代表同步是否正常。
show slave status\G;
Relay_Log_Pos: 284
Relay_Master_Log_File: master-bin.000002
Slave_IO_Running: Yes 出现呢yes即可
Slave_SQL_Running: Yes
#######验证 在主服务器创建一个库看看是否同步
|*必须设置两个从库为只读模式:
mysql> set global read_ only=1;
mysql> flush privileges;
注意:设置完成直接验证主从复制功能
1.所有服务器上都安装MHA依赖的环境,首先安装epel源。
[root@mha_manager ~]# yum install epel-release --nogpgcheck -y
[root@mha_manager ~]# yum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN
[root@master ~]# tar zxvf mha4mysql-node-0.57.tar.gz
[root@master ~]# cd mha4mysql-node-0.57/
[root@master mha4mysql-node-0.57]# yum install perl-Module-Install -y
[root@master mha4mysql-node-0.57]# perl Makefile.PL
==> Auto-install the 1 mandatory module(s) from CPAN? [y] y '//输入y'
[root@master mha4mysql-node-0.57]# make && make install
3.在MHA-manager上安装manager组件(! 注意:- -定要先安装node组件才能安装manager组件)
仅mha_manager服务器安装manager组件
[root@mha_manager ~]# tar zxvf mha4mysql-manager-0.57.tar.gz
[root@mha_manager ~]# cd mha4mysql-manager-0.57/
[root@mha_manager mha4mysql-manager-0.57]# perl Makefile.PL
[root@mha_manager mha4mysql-manager-0.57]# make
[root@mha_manager mha4mysql-manager-0.57]# make install
manager安装后在/usr/local/bin下面会生成几个工具,主要包括以下几个:
masterha check_ ssh检查MHA的SSH配置状况
masterha check_ repl 检查MySQL复制状况
masterha_ manger启动manager的脚本
masterha check_ status检测当前MHA运行状态
masterha_ master_ monitor检测master是否宕机
masterha_ master_ switch控制故障转移(自动或者手动)
masterha_ conf_ host添加或删除配置的server信息
masterha_ stop 关闭manager
5.配置无密码认证
[root@MHA-manager ~ ]# ssh-keygen -t rsa //一路按回车键
[root@MHA-manager ~]# ssh-copy-id 192.168.110.133
[root@MHA-manager ~]# ssh-copy-id 192.168.110.134
[root@ MHA-manager ~]# ssh-copy-id 192.168.110.135
2.在Mysq| master 133上配置到数据库节点Mysq|2和MysqI3的无密码认证
[root@Mysql1 ~]# ssh-keygen -t rsa
[root@ Mysql1 ~]# ssh-copy-id 192.168.110.134
[root@Mysq|1 ~ ]# ssh-copy-id 192.168.110.135
3.在Mysqlslave 134上配置到数据库节点Mysq|1和Mysq|3的无密码认证
[root@Mysql1 ~]# ssh-keygen -t rsa
[root@ Mysql1 ~]# ssh-copy-id 192.168.110.133
[root@Mysq|1 ~ ]# ssh-copy-id 192.168.110.135
4.在Mysq| 135上配置到数据库节点Mysql1和Mysq|2的无密码认证
[root@Mysql1 ~]# ssh-keygen -t rsa
[root@ Mysql1 ~]# ssh-copy-id 192.168.110.133
[root@Mysq|1 ~ ]# ssh-copy-id 192.168.110.134
1.在manager节点上复制相关脚本到/usr/local/bin目录。
[root@MHA-manager ~]# [root@manager bin]# cp -ra /root/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
//拷贝后会有四个执行文件
[root@atlas ~]# II /usr/local/bin/scripts/
总用量32
-rwxr-xr-x 1 mysq| mysq| 36485月31 2015 master_ ip_ failover #自动切换时VIP管理的脚本
-rwxr-xr-x 1 mysql mysql 9872 5月25 09:07 master ip_ online_ change #在线切换时vip的管理
-rwxr-xr-x 1 mysql mysql 11867 5月31 2015 power_ manager #故障发生后关闭主机的脚本
-rwxr-xr-x 1 mysql mysql 13605月31 2015 send_ _report #因故障切换后发送报警的脚本
2.复制.上述的自动切换时VIP管理的脚本到/usr/local/bin目录,这里使用脚本管理VIP,
[root@MHA-manager ~ ]# cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin/
3.修改内容如下: (删除原有内容,直接复制)
[root@MHA-manager ~ ]#vim /usr/local/bin/master_ip_failover
:% s/#^//g 将开头的#全部干掉
#!/usr/bin/env perl 第一行的#不能删除
#!/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.110.200';
my $brdc = '192.168.110.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";
}
4.创建MHA软件目录并拷贝配置文件。
cd /usr/local/bin/scripts/
[root@MHA-manager ~]# mkdir /etc/masterha
[root@MHA-manager ~]# cp /root/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/
[root@MHA-manager ~]# vim /etc/masterha/app1.cnf
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/usr/local/mysql/data
master_ip_failover_script=/usr/local/bin/master_ip_failover
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
password=manager
ping_interval=1
remote_workdir=/tmp
repl_password=123456
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.110.134 -s 192.168.110.135
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=192.168.110.133
port=3306
[server2]
candidate_master=1
hostname=192.168.110.135
check_repl_delay=0
port=3306
[server3]
hostname=192.168.79.134
port=3306
---------------配置文件解析----------------
[server default]
manager_ workdir=/var/log/ masterha/app1.log
##manager工作目录
manager_ log=/var/log/masterha/app1/manager.log
#manager日志
master_ binlog_ _dir= /usr/local/mysqI/data/
#master保存binlog的位置,这里的路径要与master目
#master_ ip_ failover_ script= /usr/local/bin/master ip_ failover
#设置自动failover时候的切换脚本,也就是上边的哪个脚本
master_ ip_ online_ change script= /usr/local/bin/master ip_ online change #设置手动切换时候的切换脚本
password = manager
#设置mysql中root用户的密码,这个密码是前文中创建监控用户的那个密码
user= mha
#设置监控用户root
ping_ interval=1
#设置监控主库,发送ping包的时间间隔,默认是3秒,尝试三次没有回应的时候自动进行railover
remote_ workdir= /tmp
#设置远端mysq|在发生切换时binlog的保存位置
repl_ password= 123
#设置复制用户的密码
repl_ user= myslave
#设置复制用户的用户
report script=/usr/local/send_ report
//设置发生切换后发送的报警的脚本
secondary_ check_ script= /usr/local/bin/masterha secondary check -S 192.1 68.195.130 -S 192.168.195.131
shutdown script= "
#设置故障发生后关闭故障主机脚本(该脚本的主要作用是关闭主机放在发生脑裂,这里没有使用)
ssh_ user= root
#设置ssh的登录用户名
[server1]
hostname= 192.1 68.195.129
| port= 3306
[server2]
hostname=192.168.195.130
5.测试ssh无密码认证,如果正常最后会输出successfully, 如下所示。
[root@MHA-manager ~ ]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
Tue Nov 26 23:09:45 2019 - [warning] Global configuration file /etc/masterha_ default.cnf not found. Skipping.
Tue Nov 26 23:09:45 2019 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Tue Nov 26 23:09:45 2019 - [info] Reading server configuration from /etc/masterha/app1 .cnf..
Tue Nov 26 23:09:45 2019 - [info] Starting SSH connection tests..
Tue Nov 26 23:09:46 2019 - [debug]
Tue Nov 26 23:09:45 2019 - [debug] Connecting via SSH from root@192.168.195.130(192.1 68.195.130:22) to root@192.
Tue Nov 26 23:09:46 2019 - [debug] ok.
Tue Nov 26 23:09:47 2019 - [debug]
Tue Nov 26 23:09:46 2019 - [debug] Connecting via SSH from root@192.168.195.131(192.1 68.195.131:22) to root@192.'
Tue Nov 26 23:09:47 2019 - [debug]
ok.
Tue Nov 26 23:09:47 2019 - [info] All SSH connection tests passed successfully.
---------------检查群集状态
[root@MHA-manager ~]# masterha check repl -conf= /etc/masterha/app1 .cnf
Tue Nov 26 23:10:29 2019 - [info] Slaves settings check done.
Tue Nov 26 23:10:29 2019 - [info]
192.168.195.130(192.168.195.1 30:3306) (current master)
+--192.1 68.195.131(192.1 68.195.131:3306)
Tue Nov 26 23:10:29 2019 - [info] Checking replication health on 192.1 68.195.131.. .
Tue Nov 26 23:10:29 2019 - [info] ok.
Tue Nov 26 23:10:29 2019 - [info] Checking master ip_ failover_ script status:
Tue Nov 26 23:10:29 2019 - [info] /usr/local/bin/master ip_ failover --command=status --ssh_ user=root --orig_ master_ ha
IN SCRIPT TEST====/sbin/ifconfig ens33:1 down==/sbin/ifconfig ens33:1 192.168.110.200===
Checking the Status of the script.. OK
Fri Aug 28 15:03:03 2020 - [info] OK.
Fri Aug 28 15:03:03 2020 - [warning] shutdown_script is not defined.
Fri Aug 28 15:03:03 2020 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
报错如下
Fri Aug 28 14:32:46 2020 - [error][/usr/local/share/perl5/MHA/ServerManager.pm, ln671] Master 192.168.110.133:3306 from which slave 192.168.110.134(192.168.110.134:3306) replicates is not defined in the configuration file!
找不到133的从服务器 ,检查发现在配置文件内没有指明主服务器的ip地址
//注意:第一次配置需要去master,上手动开启虚拟IP
[root@Mysql1 ~]# /sbin/ifconfig ens33:1 192.168.110.100/24 '配置刚才配置文件的虚拟ip'
7.启动MHA
[root@MHA-manager ~]#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 &
[1] 129929
--remove_ dead_ master_ conf该参数代表当发生主从切换后,老的主库的ip将会从配置文件中移除。
--manger_ log日志存放位置。
--ignore_ last failover在缺省情况下,如果MHA检测到连续发生宕机,且两次宕机间
隔不足8小时的话,则不会进行Failover,之所以这样限制是为了避免ping-pong效应。该
参数代表忽略上次MHA触发切换产生的文件,默认情况下,MHA发生切换后会在日志记
目录,也就是.上面设置的日志app1.failover.complete文件,下次再次切换的时候如果发现
该目录下存在该文件将不允许触发切换,除非在第一 次切换后收到删除该文件,为了方便,
这里设置为-ignore last failover。
8.查看MHA状态,可以看到当前的master是MysqI1 节点。
[root@MHA-manager ~ ]# masterha check_ status --conf=/etc/masterha/app1.cnf
app1 (pid:7763) is running(0:PING OK), master:192.168.110.132
9.查看MHA日志,也以看到当前的master是192.168.110.132, 如下所示。
[root@MHA-manager ~]# cat /var/log/masterha/app1/manager.log
Tue Nov 26 23:12:30 2019 - [info] Checking master_ ip_ failover_ script status:
Tue Nov 26 23:12:30 2019 - [info] /usr/local/bin/master_ ip_ failover --command=status --ssh_ user=root --orig_ master_ hc
IN SCRIPT TEST=== =/sbin/ifconfig ens33:1 down= =/sbin/ifconfig ens33:1 192.168.110.132===
Checking the Status of the script.. OK
Tue Nov 26 23:12:30 2019 - [info] OK.
Tue Nov 26 23:12:30 2019 - [warning] shutdown_ script is not defined.
Tue Nov 26 23:12:30 2019 - [info] Set master ping interval 1 seconds.
Jue Nov 26 23:12:30 2019- finfol Set secondarv check script: /usr/local/bin/masterha secondarv check -S 192.168.110.132
ens33: flags=4163 <UP BROADCAST,RUNNING,MULTICAST> mtu 1 500
inet 192.168.110.132 netmask 255.255.255.0 broadcast 192.168.110.132
inet6 fe80::be3f:d090:5539:5db1 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:70:4f:5b txqueuelen 1000 (Ethernet)
RX packets 968912 bvtes 200636579 (191 .3 MiB)
[root@MHA-manager ~ ]#tailf /var/log/masterha/app1/manager.log //启用监控观察日志记录
[root@Mysql1 ~]# pkill -9 mysql //查看master变化
故障模拟:
在主库上:
pkill mysqld
可以看到从库的状态,其中之- -肯定有切换到主库的