mysqlMAH高可用

mysql的MHA简介

MHA是一套优秀的作为MySQL高可用性环境下故障切换和主从角色提升的高可用软件

MHA由两部分组成:MHA Manager(管理节点)和MHA Node(数据节点),MHA Manager可以单独部署在一台独立的机器上管理多个master-slave集群,也可以部署在一台slave节点上。MHA Node运行在每台MySQL服务器及Manager服务器上,MHA Manager会定时探测集群中的master节点,当master出现故障时,它可以自动将拥有最新数据的slave提升为新的master,然后将所有其他的slave重新指向新提升的master。整个故障转移过程对应用程序层面完全透明。

在MHA自动故障切换过程中,MHA会试图从宕机的主服务器上保存二进制日志,最大程度的保证数据不丢失,但这种操作是有概率性的。例如,如果主服务器硬件故障或无法通过ssh访问,MHA没法保存二进制日志,只进行故障转移从而丢失了最新的数据。使用MySQL 5.5的半同步复制,可以降低数据丢失的风险。MHA可以与半同步复制结合起来。如果只有一个slave已经收到了最新的二进制日志,MHA可以将最新的二进制日志应用于其他所有的slave服务器上,因此可以保证所有节点的数据一致性。

目前MHA主要支持一主多从的架构,要搭建MHA,要求一个MySQL复制集群中必须最少有三台数据库服务器,一主二从,即一台充当master,一台充当备用master,另外一台充当从库,因为至少需要三台服务器,出于机器成本的考虑,淘宝也在该基础上进行了改造,目前淘宝TMHA已经支持一主一从。另外对于想快速搭建的可以参考:MHA快速搭建
我们自己使用其实也可以使用1主1从,但是master主机宕机后无法切换,以及无法补全binlog。master的mysqld进程crash后,还是可以切换成功,以及补全binlog的。

工作流程

  • 从宕机的master上尝试保存二进制日志事件(binlog events)更新到最新的slaver服务器中,并提上为master服务器,指定其他slaver连接新的master进行主从复制

MHA工具介绍

  • manager工具:
    masterha_check_ssh 检查MHA的SSH配置状况
    masterha_check_repl 检查MySQL复制状况
    masterha_manger 启动MHA
    masterha_check_status 检测当前MHA运行状态
    masterha_master_monitor 检测master是否宕机
    masterha_master_switch 控制故障转移(自动或者手动)
    masterha_conf_host 添加或删除配置的server信息

  • node工具包
    save_binary_logs 保存和复制master的二进制日志
    apply_diff_relay_logs 识别差异的中继日志事件并将其差异的事件应用于其他的slave
    filter_mysqlbinlog 去除不必要的ROLLBACK事件(MHA已不再使用这个工具)
    purge_relay_logs 清除中继日志(不会阻塞SQL线程)
    同时进行mysql的半同步复制

实验

#共五台虚拟机 10(master,server01),11(备master,server02),12(slaver,server03),13(slaver,server04),14(MAH,server05)
#配置所有主机的映射
vim /etc/hosts
192.168.200.10 server01
192.168.200.11 server02
192.168.200.12 server03
192.168.200.13 server04
192.168.200.14 server05

#关闭所有主机防火墙和安全机制
systemctl stop iptables
systemctl stop firewalld
setdenforce 0
iptables -F

#所有主机安装MHA node
rpm -ivh epel-release-latest-7.noarch.rpm
yum install -y perl-DBD-MySQL.x86_64 perl-DBI.x86_64 perl-CPAN perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker
rpm -q perl-DBD-MySQL.x86_64 perl-DBI.x86_64 perl-CPAN perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker
perl-DBD-MySQL-4.023-6.el7.x86_64
perl-DBI-1.627-4.el7.x86_64
perl-CPAN-1.9800-292.el7.noarch
perl-ExtUtils-CBuilder-0.28.2.6-292.el7.noarch
perl-ExtUtils-MakeMaker-6.68-3.el7.noarch

#所有主机安装MHA node
tar xf mha4mysql-node-0.56.tar.gz 
cd mha4mysql-node-0.56/
perl Makefile.PL
make && make install
#安装后会在/usr/local/bin生成脚本

#在14主机安装MAH Manger
yum install -y perl perl-Log-Dispatch perl-Parallel-ForkManager perl-DBD-MySQL perl-DBI perl-Time-HiRes  #依赖
yum -y install perl-Config-Tiny-2.14-7.el7.noarch.rpm  #依赖
rpm -q perl cpan perl-Log-Dispatch perl-Parallel-ForkManager perl-DBD-MySQL perl-DBI perl-Time-HiRes perl-Config-Tiny  #检查

#安装MHA Manger
tar xf mha4mysql-manager-0.56.tar.gz 
cd mha4mysql-manager-0.56/
perl Makefile.PL
make && make install
#安装后会在/usr/local/bin下生成更多的脚本

#配置ssh密钥对验证
14:ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.200.10
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.200.11
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.200.12
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.200.13
#11,12,13相互配置

#所有主机安装mysql配置
yum -y install mariadb*
vim /etc/my.cnf
[mysqld]
server-id = 1  #不能一样			
log-bin=master-bin     	
log-slave-updates=true   	
relay_log_purge=0 
systemctl restart mariadb

#在11,12,13,10上创建复制授权用户
grant replication slave on *.* to 'repl'@'192.168.200.%' identified by '123456';
flush privileges;

show master status;#查看主库备份时的binlog名称和位置
scp all.sql 192.168.200.11:/tmp/
scp all.sql 192.168.200.12:/tmp/
scp all.sql 192.168.200.13:/tmp/

mysql -uroot -p123123< /tmp/all.sql  #导入备份数据到11,12,13中执行
stop slave;
CHANGE MASTER TO
MASTER_HOST='192.168.200.111',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='master-bin.000001',
MASTER_LOG_POS=474;
start slave;
show slave status\G   
# 检查IO和SQL线程是否为:yes
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

#主从同步故障处理:set global sql_slave_skip_counter =1 ;

#在11,12,13设置read_only状态
[root@server02 ~]# mysql -uroot -p123456 -e 'set global read_only=1;'
[root@server03 ~]# mysql -uroot -p123456 -e 'set global read_only=1;'
[root@server04 ~]# mysql -uroot -p123456 -e 'set global read_only=1;'

#在11,12,13创建监控用户
grant all privileges on *.* to 'root'@'192.168.200.%' identified  by '123123';
flush privileges;
grant all privileges on *.* to 'root'@'server01' identified  by '123456';
flush privileges;  #10
grant all privileges on *.* to 'root'@'server02' identified  by '123456';
flush privileges;  #11
grant all privileges on *.* to 'root'@'server03' identified  by '123456';
flush privileges;  #12
grant all privileges on *.* to 'root'@'server04' identified  by '123456';
flush privileges;  #13


#在14上配置MHA环境
mkdir /etc/masterha
cp mha4mysql-manager-0.56/samples/conf/app1.cnf /etc/masterha

#修改app.cnf配置文件
vim /etc/masterha/app1.cnf

[server default]
#设置manager的工作日志
manager_workdir=/var/log/masterha/app1 
#设置manager的日志,这两条都是默认存在的
manager_log=/var/log/masterha/app1/manager.log

#设置master默认保存binlog的位置,以便MHA可以找到master日志
master_binlog_dir=/var/lib/mysql

#设置自动failover时候的切换脚本
master_ip_failover_script= /usr/local/bin/master_ip_failover

#设置mysql中root用户的密码
password=123456
user=root
#ping包的时间间隔
ping_interval=1
#设置远端mysql在发生切换时保存binlog的具体位置
remote_workdir=/tmp
#设置复制用户的密码和用户名
repl_password=123456
repl_user=repl

[server1]
hostname=server01
port=3306

[server2]
hostname=server02
candidate_master=1
port=3306
check_repl_delay=0

[server3]
hostname=server03
port=3306

[server4]
hostname=server04
port=3306


#配置故障转移脚本

#在11,12,13设置纵谷relay log清除方法
mysql -uroot -p123123 -e 'set global relay_log_purge=0;'

#在11,12,13清除中继日志
purge_relay_logs --user=root --password=123456 --disable_relay_log_purge  --port=3306 --workdir=/tmp

#在14检查MHA ssh通信状态
masterha_check_ssh --conf=/etc/masterha/app1.cnf
masterha_check_repl --conf=/etc/masterha/app1.cnf

#在15中开启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 &
masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:65837) is running(0:PING_OK), master:server01  #检查监控
cat /var/lib/masterha/app1/manager.log

#检查VIP:ip a | grep 254

#模拟主库故障
systemcytl stop mariadb   #10
ip a |grep 254  #11
show slave status\G  #12
#回车14:发现监控自动关闭
cat /etc/masterha/app.cnf

#主库恢复
systemctl start mariadb  #10
mysql -uroot -p123123
stop slave
#在11上重新指向新的主库
stop slave;
CHANGE MASTER TO
MASTER_HOST='192.168.200.10',
MASTER_USER='repl',
MASTER_PASSWORD='123123';
start slave;
show slave status\G
#在14上修改app.cnf添加server02配置
masterha_check_repl --conf=/etc/masterha/app1.cnf

故障脚本
[root@server05 ~]# 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.200.100';          	# 写入VIP
my $key = "1"; 	#非keepalived方式切换脚本使用的
my $ssh_start_vip = "/sbin/ifconfig ens32:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens32:$key down"; #那么这里写服务的开关命令
$ssh_user = "root"; 
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" ) { 
# $orig_master_host, $orig_master_ip, $orig_master_port are passed. 
# If you manage master ip address at global catalog database, 
# invalidate orig_master_ip here. 
my $exit_code = 1; 
#eval { 
# print "Disabling the VIP on old master: $orig_master_host \n"; 
# &stop_vip(); 
# $exit_code = 0; 
#}; 
eval { 
print "Disabling the VIP on old master: $orig_master_host \n"; 
#my $ping=`ping -c 1 10.0.0.13 | grep "packet loss" | awk -F',' '{print $3}' | awk '{print $1}'`; 
#if ( $ping le "90.0%"&& $ping gt "0.0%" ){ 
#$exit_code = 0; 
#} 
#else { 
&stop_vip(); 
# updating global catalog, etc 
$exit_code = 0; 
#} 
}; 

if ($@) { 
warn "Got Error: $@\n"; 
exit $exit_code; 
} 
exit $exit_code; 
} 
elsif ( $command eq "start" ) {
# all arguments are passed. 
# If you manage master ip address at global catalog database, 
# activate new_master_ip here. 
# You can also grant write access (create user, set read_only=0, etc) here. 
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"; 
`ssh $ssh_user\@$orig_master_ip \" $ssh_start_vip \"`; 
exit 0; 
} 
else { 
&usage(); 
exit 1; 
} 
} 
# A simple system call that enable the VIP on the new master 
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"; }

[root@server05 ~]# chmod +x /usr/local/bin/master_ip_failover

配置请求负载均衡(Keepalived)

#在21,22上安装Keepalived
yum -y install ipvsadm kernel-devel openssl-devel keepalived
#21上面配置Keepalived.conf
#22上面配置keepalived.conf

#检查ip地址
ip a | grep 200

#在12,13(两个从库)上面配置脚本
vim realserver.sh

#在21,22上面检查ipvsam -Ln
posted @ 2021-10-15 22:39  whml  阅读(119)  评论(0编辑  收藏  举报