MYSQL MHA部署实录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQL MHA部署实录相关的知识,希望对你有一定的参考价值。

king01作为master  king02与king03作为king01的slave,king04作为mha_manager


[[email protected] ~]# mkdir .ssh

[[email protected] ~]# cd .ssh

[[email protected] .ssh]# ssh-keygen -t dsa -P '' -f id_dsa

[[email protected] .ssh]# cat id_dsa.pub >> authorized_keys


[[email protected] ~]# mkdir .ssh

[[email protected] ~]# cd .ssh

[[email protected] .ssh]# ssh-keygen -t dsa -P '' -f id_dsa

[[email protected] .ssh]# cat id_dsa.pub >> authorized_keys


[[email protected] ~]# mkdir .ssh

[[email protected] ~]# cd .ssh

[[email protected] .ssh]# ssh-keygen -t dsa -P '' -f id_dsa

[[email protected] .ssh]# cat id_dsa.pub >> authorized_keys


[[email protected] ~]# mkdir .ssh

[[email protected] ~]# cd .ssh

[[email protected] .ssh]# ssh-keygen -t dsa -P '' -f id_dsa

[[email protected] .ssh]# cat id_dsa.pub >> authorized_keys


[[email protected] ~]# scp 192.168.1.202:/root/.ssh/id_dsa.pub ./id_dsa.pub.202

[[email protected] ~]# scp 192.168.1.203:/root/.ssh/id_dsa.pub ./id_dsa.pub.203

[[email protected] ~]# scp 192.168.1.204:/root/.ssh/id_dsa.pub ./id_dsa.pub.204

[[email protected] ~]# cd .ssh

[[email protected] .ssh]# cat id_dsa.pub.202 >> authorized_keys

[[email protected] .ssh]# cat id_dsa.pub.203 >> authorized_keys

[[email protected] .ssh]# cat id_dsa.pub.204 >> authorized_keys


king01-king04

yum install perl-DBD-mysql perl-CPAN perl-Config-Tiny perl-Log-Dispatch perl-Time-HiRes perl-Parallel-ForkManager

tar zxvf mha4mysql-node-0.56.tar.gz

cd mha4mysql-node-0.56

perl Makefile.PL

make && make install


king04

tar zxvf mha4mysql-manager-0.56.tar.gz

cd mha4mysql-manager-0.56

perl Makefile.PL

make && make install


mkdir -p /etc/mha

mkdir -p /usr/local/mha


cp samples/conf/* /etc/mha

cp samples/scripts/* /usr/local/bin


cd /etc/mha/

vim /etc/mha/mha.conf

[server default]

manager_workdir=/usr/local/mha

manager_log=/usr/local/mha/manager.log

user=root

password=abcd.1234

ssh_user=root

repl_user=repl

repl_password=repl

ping_interval=1

master_ip_failover_script=/usr/local/bin/master_ip_failover

master_ip_online_change_script=/usr/local/bin/master_ip_online_change


[server1]

hostname=192.168.1.201

ssh_port=22

master_binlog_dir=/usr/local/mysql/data

candidate_master=1

port=3306

          

[server2]

hostname=192.168.1.302

ssh_port=22

master_binlog_dir=/usr/local/mysql/data 

candidate_master=1

port=3306


[server3]

hostname=192.168.1.203

ssh_port=22

master_binlog_dir=/usr/local/mysql/data

candidate_master=1

port=3306


[[email protected] ~]# vi /usr/local/bin/master_ip_failover

#!/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;

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.1.200/24'; # Virtual IP

my $gateway = '192.168.1.1'; #Gateway IP

my $interface = 'eth0';

my $key = "1";

my $ssh_start_vip = "/sbin/ifconfig $interface:$key $vip;/sbin/arping -I $interface -c 3 -s $vip $gateway >/dev/null 2>&1";

my $ssh_stop_vip = "/sbin/ifconfig $interface:$key down";

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;

};

if ([email protected]) {

warn "Got Error: [email protected]\n";

exit $exit_code;

}

exit $exit_code;

}

# 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 ([email protected]) {

warn [email protected];

exit $exit_code;

}

exit $exit_code;

}

elsif ( $command eq "status" ) {

print "Checking the Status of the script.. OK \n";

`ssh $ssh_user\@$orig_master_host \" $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";

}


[[email protected] ~]# /sbin/ifconfig eth0:1 192.168.1.200/24


[[email protected] ~]# /usr/local/bin/masterha_check_ssh --conf=/etc/mha/mha.conf

[[email protected] ~]# /usr/local/bin/masterha_check_repl --conf=/etc/mha/mha.conf

[[email protected] ~]# nohup /usr/local/bin/masterha_manager --conf=/etc/mha/mha.conf > /var/log/masterhamanager.log  < /dev/null 2>&1 &

[[email protected] ~]# /usr/local/bin/masterha_check_status --conf=/etc/mha/mha.conf






以上是关于MYSQL MHA部署实录的主要内容,如果未能解决你的问题,请参考以下文章

mysql高可用MHA部署全过程

细节决定成败 MySQL MHA 部署

MHA高可用集群部署

MHA高可用集群部署

MHA高可用集群部署

MySQL高可用MHA部署 (一主二从)