这里写目录标题
MHA简介
MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Facebook公司)开发,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件。在MySQL故障切换过程中,MHA能做到在10~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程中,MHA能在最大程度上保证数据的一致性,以达到真正意义上的高可用。
MHA还提供在线主库切换的功能,能够安全地切换当前运行的主库到一个新的主库中 (通过将从库提升为主库),大概0.5-2秒内即可完成。
该软件由两部分组成:MHA Manager(管理节点)和MHA Node(数据节点)。MHA Manager可以单独部署在一台独立的机器上管理多个master-slave集群,也可以部署在一台slave节点上。MHA Node运行在每台MySQL服务器上,MHA Manager会定时探测集群中的master节点,当master出现故障时,它可以自动将最新数据的slave提升为新的master,然后将所有其他的slave重新指向新的master。整个故障转移过程对应用程序完全透明。
MHA优点
1)自动故障转移快
2)主库崩溃不存在数据一致性问题
3)不需要对当前mysql环境做重大修改
4不需要添加额外的服务器(仅一台manager就可管理上百个replication)
5)性能优秀,可工作在半同步复制和异步复制,当监控mysql状态时,仅需要每隔N秒向master发送ping包(默认3秒),所以对性能无影响。你可以理解为MHA的性能和简单的主从复制框架性能一样。
6)只要replication支持的存储引擎,MHA都支持,不会局限于innodb
配置MHA
安装环境
MHA版本为0.57 版本
服务器 CentOS7: MHA-manager/192.168.100.40
管理节点,安装 manager 组件
服务器 CentOS7 Mysql1:192.168.100.10 Master 节点,安装 node 组件
服务器 CentOS7 Mysql2:192.168.100.20Slave 节点,安装 node 组件
服务器 CentOS7 Mysql3:192.168.100.30Slave 节点,安装 node 组件
安装mysql数据库
[root@Mysql1 ~]# yum -y install ncurses-devel gcc-c++ perl-Module-Install
[root@Mysql1 ~]# tar zxvf cmake-2.8.6.tar.gz
[root@Mysql1 ~]# cd cmake-2.8.6
[root@Mysql1 cmake-2.8.6]# ./configure
[root@Mysql1 cmake-2.8.6]# gmake && gmake install
[root@Mysql1 ~]# tar -zxvf mysql-5.6.36.tar.gz
[root@Mysql1 ~]# cd 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
[root@Mysql1 mysql-5.6.36]# make -j2 && make install
[root@Mysql1 mysql-5.6.36]# cp support-files/my-default.cnf /etc/my.cnf
[root@Mysql1 mysql-5.6.36]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@Mysql1 ~]# chmod +x /etc/rc.d/init.d/mysqld
[root@Mysql1 ~]# chkconfig --add mysqld
[root@Mysql1 ~]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@Mysql1 ~]# source /etc/profile
[root@Mysql1 ~]# groupadd mysql
[root@Mysql1 ~]# useradd -M -s /sbin/nologin mysql -g mysql
[root@Mysql1 ~]# chown -R mysql.mysql /usr/local/mysql
[root@Mysql1 ~]# mkdir -p /data/mysql
/usr/local/mysql/scripts/mysql_install_db \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data \
--user=mysql
修改 Master 的主配置文件/etc/my.cnf 文件
主服务器
[root@Mysql1 ~]# cat /etc/my.cnf
[mysqld]
server-id = 1
log_bin = master-bin
log-slave-updates = true
从服务器
[root@Mysql2 ~]# vim /etc/my.cnf
server-id = 2
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
[root@Mysql3 ~]# vim /etc/my.cnf
server-id = 3
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
做软连接(三台mysql都要做)并启动mysql
[root@Mysql1 ~]# ln -s /usr/local/mysql/bin/mysql /usr/sbin/
[root@Mysql1 ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
[root@Mysql2 ~]# ln -s /usr/local/mysql/bin/mysql /usr/sbin/
[root@Mysql2 ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
[root@Mysql3 ~]# ln -s /usr/local/mysql/bin/mysql /usr/sbin/
[root@Mysql3 ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
[root@Mysql1 ~]# /usr/local/mysql/bin/mysqld_safe --user=mysql &
[root@Mysql2 ~]# /usr/local/mysql/bin/mysqld_safe --user=mysql &
[root@Mysql3 ~]# /usr/local/mysql/bin/mysqld_safe --user=mysql &
配置mysql
一主两从
所有数据库节点上授权(从库同步使用和manager 使用)
mysql> grant replication slave on *.* to 'myslave'@'192.168.100.%' identified by '123';
mysql> grant all privileges on *.* to 'mha'@'192.168.100.%' identified by 'manager';
mysql> flush privileges;
授权
mysql> grant all privileges on *.* to 'mha'@'192.168.100.10' identified by 'manager';
mysql> grant all privileges on *.* to 'mha'@'192.168.100.20' identified by 'manager';
mysql> grant all privileges on *.* to 'mha'@'192.168.100.30' identified by 'manager';
在 Mysql2 和 Mysql3都执行同步并查看 IO 和 SQL 线程是否同步
mysql> change master to master_host='192.168.100.10',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=1353;
mysql> start slave;
mysql> show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
设置两个从库为只读模式:
mysql> set global read_only=1;
开始做MHA
所有服务器上都安装 MHA 依赖的环境
[root@MHA-manager ~]# yum install epel-release --nogpgcheck
[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@Mysql1 ~]# tar zxvf mha4mysql-node-0.57.tar.gz
[root@Mysql1 ~]# cd mha4mysql-node-0.57
[root@Mysql1 mha4mysql-node-0.57]# perl Makefile.PL
[root@Mysql1 mha4mysql-node-0.57]# make
[root@Mysql1 mha4mysql-node-0.57]# make install
安装node组件,每个数据库都要安装node组件,现在已musql1为例
安装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
配置无密码认证
[root@MHA-manager ~]# ssh-keygen -t rsa //一路按回车键
[root@MHA-manager ~]# ssh-copy-id 192.168.100.10
[root@MHA-manager ~]# ssh-copy-id 192.168.100.20
[root@MHA-manager ~]# ssh-copy-id 192.168.100.30
在 Mysql1 上配置到数据库节点Mysql2和Mysql3的无密码认证
[root@Mysql1 ~]# ssh-keygen -t rsa
[root@Mysql1 ~]# ssh-copy-id 192.168.100.20
[root@Mysql1 ~]# ssh-copy-id 192.168.100.30
同上,在mysql2和mysql3上也配置无密码认证
在 manager 节点上复制相关脚本到/usr/local/bin
[root@MHA-manager ~]# cp -ra /root/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
//拷贝后会有四个执行文件
[root@atlas ~]# ll /usr/local/bin/scripts/
总用量 32
-rwxr-xr-x 1 mysql mysql 3648 5 月 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 1360 5 月 31 2015 send_report #因故障切换后发送报警的脚本
[root@MHA-manager ~]# cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin #复制
[root@MHA-manager ~]#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.100.200'
my $brdc = '192.168.100.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 软件目录并拷贝配置文件
[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
user=mha
ping_interval=1
remote_workdir=/tmp
repl_password=123
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.100.20 -s 192.168.100.30
shutdown_script=""
ssh_user=root
[server1]
hostname=192.168.100.10
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.100.20
port=3306
[server3]
candidate_master=1
check_repl_delay=0
hostname=192.168.100.30
port=3306
测试 ssh 无密码认证
[root@MHA-manager ~]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
...
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
...
MySQL Replication Health is OK.
去master上手动开启虚拟IP
[root@Mysql1 ~]# /sbin/ifconfig ens33:1 192.168.100.200/24
启动 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
查看 MHA 状态
[root@MHA-manager ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:7763) is running(0:PING_OK), master:192.168.100.10
可以看到当前的 master 是 Mysql1 节点
故障模拟
[root@MHA-manager ~]#tailf /var/log/masterha/app1/manager.log #启用监控观察日志记录
[root@Mysql1 ~]# pkill -9 mysql #模拟mysql挂掉
此时会看到日志记录在发送变化,最后成功将192.168.100.200漂到slave上
故障恢复
[root@Mysql1 ~]# /etc/init.d/mysqld start
登陆Mysql1
mysql> change master to master_host='192.168.100.10',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=1235; #此处根据新master的状态来
mysql> start slave;
修改配置文件
vi /etc/masterha/app1.cnf
[server1]
hostname=192.168.100.10
port=3306
在manager上启动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 &