MHA一键部署

MHA一键部署

#!/bin/bash
#MHA部署
#MySQL主服务器名为 mysql1,ip地址192.168.100.150
#MySQL从-主备服务器名为 mysql2,ip地址192.168.100.160
#MySQL从服务器名为mysql3,ip地址192.168.100.170
#MHA服务器名为mha,ip地址192.168.100.140

#######################部署主从服务
#1.修改配置文件
##主服务器配置master

cat >> /etc/my.cnf <<EOF
server-id = 1
log_bin = master-bin
log-slave-updates = true
EOF

----------------------从服务器配置slave1----------------------
cat >> /etc/my.cnf <<EOF
server-id = 2
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
EOF
----------------------从服务器配置slave2----------------------
cat >> /etc/my.cnf <<EOF
server-id = 3
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
EOF

#2.mysql1、mysql2、mysql3 分别做两个软连接

ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/

#3.mysql1、mysql2、mysql3 上启动MySQL

service mysqld reload

#4.配置MySQL一主两从 "在3个MySQL数据库节点上授权两个用户,一个是从库同步使用,另外一个是manager使用"

CONNECT_MYSQL="mysql -uroot -pabc123"
SQL1="grant replication slave on *.* to 'myslave'@'192.168.100.%' identified by '123';"
SQL2="grant all privileges on *.* to 'mha'@'192.168.100.%' identified by 'manager';"

echo "${SQL1}" | ${CONNECT_MYSQL}
echo "${SQL2}" | ${CONNECT_MYSQL}

#5.下面三条授权按理论是不用添加的,但是做实验环境时候诵过MHA检查MySQL主从有报错,报两个从库通过主机名连接不上主库,所以所有数据库加上下面的授权
CONNECT_MYSQL="mysql -uroot -pabc123"
SQL3="grant all privileges on *.* to 'mha'@'mysql1' identified by 'manager';"
SQL4="grant all privileges on *.* to 'mha'@'mysql2' identified by 'manager';"
SQL5="grant all privileges on *.* to 'mha'@'mysql3' identified by 'manager';"
echo "${SQL3}" | ${CONNECT_MYSQL}
echo "${SQL4}" | ${CONNECT_MYSQL}
echo "${SQL5}" | ${CONNECT_MYSQL}

#6.在MySQL1(master)主机上查看二进制文件和同步点

show master status;

#7.在MySQL2和MySQL3上执行同步
change master to master_host='192.168.100.3',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=1414;
start slave;
flush privileges;

#8.必须设置两个从库为只读模式
mysql> set global read_only=1;

####################部署MHA
#!/bin/bash
#1.所有节点安装MHA依赖的环境,首先安装epel源,我这里用mha服务器做演示
yum install epel-release --nogpgcheck -y
yum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN

#2.MHA软件包对于每个操作系统版本不一样,这里CentOS7.6必须选择0.57版本,
#因为manager依赖node组件,下面都是在mha上操作演示安装node组件。
#在所有服务器上必须先安装node组件,最后在MHA-manager节点上安装manager组件
read -p "请输入你的node包和manager包路径" B
cd $B
tar zxvf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57/
perl Makefile.PL
make
make install

#3.仅mha服务器安装manager组件
cd $B
tar zxvf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57/
perl Makefile.PL
make
make install

#4检查文件少不少
cd /usr/local/bin/
ls

#5.配置无密码认证'mha主机配置到所有数据库节点的密钥对验证'

##'mha主机配置到所有数据库节点的密钥对验证'
ssh-keygen -t rsa    "一直回车创建无密码"
ssh-copy-id 192.168.100.150
ssh-copy-id 192.168.100.160
ssh-copy-id 192.168.100.170

##'在mysql1上配置到数据库节点slave1和slave2的免密验证'
ssh-keygen -t rsa   "一直回车创建无密码"
ssh-copy-id 192.168.100.160
ssh-copy-id 192.168.100.170

##'在mysql2上配置到数据库节点master和slave2的免密验证'
ssh-keygen -t rsa   "一直回车创建无密码"
ssh-copy-id 192.168.100.150
ssh-copy-id 192.168.100.170

##'在mysql3上配置到数据库节点master和slave1的免密验证'
ssh-keygen -t rsa
ssh-copy-id 192.168.100.150
ssh-copy-id 192.168.100.160

#5.配置MHA-Manager组件

##'在mha节点上复制相关脚本到/usr/local/bin目录'
cp -ra /root/mha4mysql-manager-0.57/samples/scripts /usr/local/bin

cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin/

##"修改master_ip_failover 脚本"
rm -rf /usr/local/bin/master_ip_failover
cat > /usr/local/bin/master_ip_failover <<EOF
#!/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";
}
EOF

## 创建MHA软件目录并拷贝配置文件

cd /usr/local/bin/scripts/
cp master_ip_online_change /usr/local/bin/
cp send_report /usr/local/
mkdir /etc/masterha
cp /root/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha
rm -rf /etc/masterha/app1.cnf
cat > /etc/masterha/app1.cnf <<EOF
[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
ping_interval=1
remote_workdir=/tmp
repl_password=123
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.179.124 -s 192.168.179.125
shutdown_script=""
ssh_user=root
user=mha

[server1]
hostname=192.168.179.123
port=3306

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

[server3]
hostname=192.168.179.125
port=3306
EOF

#6.测试SSH无密码验证,如果正常最后会输出successfully

masterha_check_ssh -conf=/etc/masterha/app1.cnf

#7.验证mysql复制,mysql必须都启动;"MySQL Replication Health is OK."

masterha_check_repl -conf=/etc/masterha/app1.cnf

#8.启动MHA
##第一次配置需要去master上mysql1上手动开启虚拟ip
/sbin/ifconfig ens33:1 192.168.100.200/24

##'启动MHA'
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 &

###//--remove_dead_master_conf --ignore_last_failover;这两个可选项可以不写

#9.查看MHA状态,可以看到当前的master是mysql1节点

masterha_check_status --conf=/etc/masterha/app1.cnf

#10.查看MHA日志,也可以查看当前的maser

cat /var/log/masterha/app1/manager.log

#11.查看mysql1的VIP地址192.168.179.200是否存在,这个VIP地址不会因为mha节点停止mha服务而消失
上一篇:通过 MHA 0.58 搭建一个数据库集群结构


下一篇:图论-拓扑排序-应用