Mysql - 高可用方案之MHA (2)

yum install -y perl-DBD-MySQL
yum install -y perl-Config-Tiny
yum install -y perl-Log-Dispatch
yum install -y perl-Parallel-ForkManager
rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm

manager包在/usr/bin下面生成9个文件
masterha_check_repl:检查MySQL的复制状况
masterha_check_ssh:检查MHA的SSH配置状况
masterha_check_status:检测当前MHA运行状态
masterha_conf_host:添加或删除配置的server信息
masterha_manager:启动MHA
masterha_master_monitor:检测master是否宕机
masterha_master_switch:控制故障转移(自动或手动)
masterha_secondary_check:二次检查主库是否真有问题
masterha_stop:关闭MHA


5. 配置互信

在所有节点把下面的命令都执行一遍(node1&node2&node3&manager)
ssh-keygen
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.40.16.60
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.40.16.61
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.40.16.62
ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.40.16.63


四、配置MHA 1. 主节点上(node1)添加集群监控账号

(root@localhost)[(none)]> grant all privileges on *.* to 'monitor'@'%' identified by '123456';


2. 每个从节点上(node2&node3)

set global read_only = 1;
set global relay_log_purge = 0;

之所以关闭从库的relay_log_purge,是因为 在默认情况下,从服务器上的中继日志会在SQL线程执行完后被自动删除。但是在MHA环境中,这些中继日志在恢复其它从服务器时可能会被用到,因此需要禁用中继日志的自动清除。改为定期手动清除SQL线程应用完的中继日志。
每个从节点添加定时任务,最好时间节点错开
crontab -e
0 4 * * * /usr/bin/purge_relay_logs --user=monitor --password=123456 -disable_relay_log_purge --workdir=http://www.likecs.com/tmp/ >> /tmp/purge_relay_logs.log 2>&1

workdir:为relay logs创建硬链接的目录,purge_relay_logs脚本执行的时候会现在该目录下面创建relay log的硬链接,然后短时间开启relay_log_purge这个参数,当relay log删除完毕后,再关闭relay_log_purge这个参数,最后删除硬链接。

 

3. 所有数据库节点建立两个软连接(node1&node2&node3)

mha运行时需要用到/usr/bin下的两个mysql命令
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql


4. 在manager节点上准备自定义脚本

master_ip_failover:自动切换脚本(当主库发生故障时)

Mysql - 高可用方案之MHA

Mysql - 高可用方案之MHA

#!/usr/bin/env perl # Copyright (C) 2011 DeNA Co.,Ltd. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ## Note: This is a sample script and is not complete. Modify the script based on your environment. use strict; use warnings FATAL => 'all'; use Getopt::Long; use MHA::DBHelper; my ( $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 ); my $vip = '10.40.16.71'; my $key = "2"; my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip/24"; my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down"; my $ssh_send_garp = "/sbin/arping -U $vip -I eth0 -c 1"; 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, 'new_master_user=s' => \$new_master_user, 'new_master_password=s' => \$new_master_password, ); exit &main(); sub main { 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 an 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" ) { # 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 { my $new_master_handler = new MHA::DBHelper(); # args: hostname, port, user, password, raise_error_or_not $new_master_handler->connect( $new_master_ip, $new_master_port, $new_master_user, $new_master_password, 1 ); ## Set read_only=0 on the new master $new_master_handler->disable_log_bin_local(); print "Set read_only=0 on the new master.\n"; $new_master_handler->disable_read_only(); ## Creating an app user on the new master # print "Creating app user on the new master..\n"; # FIXME_xxx_create_user( $new_master_handler->{dbh} ); $new_master_handler->enable_log_bin_local(); $new_master_handler->disconnect(); print "Enabling the VIP $vip on the new master: $new_master_host \n"; &start_vip(); $exit_code = 0; }; if ($@) { warn $@; # If you want to continue failover, exit 10. exit $exit_code; } exit $exit_code; } elsif ( $command eq "status" ) { # do nothing exit 0; } else { &usage(); exit 1; } } sub start_vip(){ `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`; `ssh $ssh_user\@$new_master_host \" $ssh_send_garp \"`; } sub stop_vip(){ return 0 unless ($ssh_user); `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"; }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpyppp.html