Skip to content

Instantly share code, notes, and snippets.

@yusuke2255
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yusuke2255/9407296 to your computer and use it in GitHub Desktop.
Save yusuke2255/9407296 to your computer and use it in GitHub Desktop.
Mysql Master HAをソースからインストールするレシピ
#!/bin/bash
if [ $# -lt 2 ]; then
echo "Illegal param count.count = $#"
exit 1
fi
# High Availability IP variables
# Other node's IP to ping and VIP to swap if other node goes down
VIP_ENI_ID=eni-xxxxxxxxx
NEW_MASTER_IP=$1
OLD_MASTER_IP=$2
echo "NEW_MASTER_IP=${NEW_MASTER_IP},OLD_MASTER_IP=${OLD_MASTER_IP}"
# Specify the EC2 region that this will be running in
REGION=ap-northeast-1
# Run aws-apitools-common.sh to set up default environment variables and to
# leverage AWS security credentials provided by EC2 roles
. /etc/profile.d/aws-apitools-common.sh
# /opt/aws/credentials.txtの読み込み
source $AWS_CREDENTIAL_FILE
# aws toolsのユーザ設定
ACCESS_USER_OPTION="--aws-access-key ${AWSAccessKeyId} --aws-secret-key ${AWSSecretKey}"
# 旧マスタDBサーバのVIPのeth1を落とす
sudo -u jenkins ssh -t -t hoge-ope@${OLD_MASTER_IP} sudo /sbin/ifconfig eth1 down
# VIP用のENIのattachment_idの取得
VIP_ATTACHMENT_ID=`ec2-describe-network-interface-attribute ${VIP_ENI_ID} ${ACCESS_USER_OPTION} --region ${REGION} -a | grep ATTACHMENT -m 1 | awk '{print $3;}'`
echo "VIP_ATTACHMENT_ID=${VIP_ATTACHMENT_ID}"
# VIP用のENIを旧マスタDBサーバからdetach
/opt/aws/bin/ec2-detach-network-interface ${VIP_ATTACHMENT_ID} ${ACCESS_USER_OPTION} --force --region ${REGION}
# detach待ち
sleep 10
# マスタDBに昇格するサーバのinstance_idを取得
INSTANCE_ID=`sudo -u jenkins ssh hoge-ope@${NEW_MASTER_IP} /usr/bin/curl --silent http://169.254.169.254/latest/meta-data/instance-id`
echo "INSTANCE_ID=${INSTANCE_ID}"
# マスタDBに昇格するサーバのeth1にVIPのENIを設定
/opt/aws/bin/ec2-attach-network-interface ${VIP_ENI_ID} -i ${INSTANCE_ID} -d 1 ${ACCESS_USER_OPTION} --region ${REGION}
# /opt/aws/credentials.txt
AWSAccessKeyId=XXXXXXXXXXXXXXXXXXX
AWSSecretKey=*************************
#!/usr/bin/env perl
# Copyright (C) 2011 DeNA Co.,Ltd.
#s
# 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
);
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 {
# updating global catalog, etc
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) {
my $exit_code = 10;
# VIPの付け替え
my @vip_change_cmd = ("/usr/local/bin/aws_vip_change.sh",$new_master_host,$orig_master_host);
system @vip_change_cmd;
$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 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";
}
package "perl-ExtUtils-MakeMaker" do
action :install
end
package "perl-CPAN" do
action :install
end
package "perl-DBD-MySQL" do
action :install
end
mha_node_file_base="mha4mysql-node-0.54"
mha_file=mha_node_file_base + ".tar.gz"
cookbook_file "/usr/local/src/#{mha_file}" do
source "#{mha_file}"
mode 0644
end
bash "install mysql master HA node" do
cwd '/usr/local/src'
code <<-EOC
tar -xvzof #{mha_file}
cd #{mha_node_file_base}
perl Makefile.PL
make
make install
rm -rf /usr/local/src/#{mha_node_file_base}
EOC
creates "/usr/local/bin/save_binary_logs"
end
[server default]
# mysql user and password
user=root
password=abcdefg
ssh_user=mysql
# working directory on the manager
manager_workdir=/root/masterha
# working directory on MySQL servers
remote_workdir=/home/mysql/masterha
master_binlog_dir=/usr/local/mysql/data
master_ip_failover_script=/usr/local/bin/master_ip_failover
# 現在稼働中のマスタDBサーバ
[server1]
hostname=10.0.10.11
# マスタ候補
candidate_master=1
# バックアップ兼フェイルオーバ時マスタ昇格スレーブDBサーバ
[server2]
hostname=10.0.10.10
# マスタ候補
candidate_master=1
# 参照用スレーブサーバ
[server3]
hostname=10.0.1.10
# マスタ昇格候補対象外
no_master=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment