mha && 脚本拉起
mha_manager | 192.168.238.131 | 监控服务器 |
master | 192.168.238.128 | 主库GTID复制模式 |
slave—1 | 192.168.238.129 | 从库,备用主库 |
slave-2 | 192.168.238.130 | 从库 |
mha版本 | 0.56 |
cd mha4mysql-node-0.56
perl Makefile.PL
cd mha4mysql-manager-0.56
perl Makefile.PL
make && make install
-r-xr-xr-x 1 root root 1779 Apr 20 11:33 masterha_check_ssh -- 检查ssh联通性
-r-xr-xr-x 1 root root 1865 Apr 20 11:33 masterha_check_status -- 检查manager状态
-r-xr-xr-x 1 root root 3201 Apr 20 11:33 masterha_conf_host -- 添加或者删除配置的server信息
-r-xr-xr-x 1 root root 2517 Apr 20 11:33 masterha_manager -- 启动mha
-r-xr-xr-x 1 root root 2165 Apr 20 11:33 masterha_master_monitor -- 检测master是否宕机
-r-xr-xr-x 1 root root 2373 Apr 20 11:33 masterha_master_switch -- 控制故障转移切换
-r-xr-xr-x 1 root root 3749 Apr 20 11:33 masterha_secondary_check
-r-xr-xr-x 1 root root 1739 Apr 20 11:33 masterha_stop
-r-xr-xr-x 1 root root 7401 Apr 20 10:58 purge_relay_logs
-r-xr-xr-x 1 root root 7263 Apr 20 10:58 save_binary_logs
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
use Net::Ping;
use Switch;
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,
);
my $vip = '192.168.238.222'; # Virtual IP
my $master_srv = '192.168.238.129';
my $timeout = 5;
my $key = "1";
my $gateway = '192.168.238.2';
my $interface = 'eth0';
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";
exit &main();
sub main {
#print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
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 if the server is still UP: $orig_master_host \n";
my $p=Net::Ping->new('icmp');
&stop_vip() if $p->ping($master_srv, $timeout);
$p->close();
$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\@$new_master_host \" $ssh_start_vip \"`;
`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";
}
mha.sh
#!/bin/bash
# Script
# Name: do_mha.sh
# APP vendor: MHA
# Linux vendor: RHEL / CentOS
# --------------------------
# variable and file path
# Part:: this script
str_mha_application=$1
# Part:: Linux
# Account info
str_linux_username="root"
# Part:: MySQL
# Account info
str_mysql_username="root"
str_mysql_password="123456"
str_repl_username="repl"
str_repl_password="repl123"
# Part:: MHA
# pid
str_pid_masterha_manager=`ps -ef | grep masterha_manager | grep "$str_mha_application" | grep perl | awk '{print $2}'`
# file
file_conf_mha_application="/mha/$str_mha_application.cnf"
# file: relation / by computed
file_log_mha_manager=`cat $file_conf_mha_application | grep --color manager_log | cut -d'=' -f2`
# variable: ip info
# 如果MHA中MySQL主库的候选服务器数量超过了两台,也许下面这个list参数,就会排上用场
list_ip_candicate=`cat $file_conf_mha_application | grep -B 3 "^candidate" | grep "hostname" | cut -d'=' -f2`
str_ip_orig_master=`cat $file_log_mha_manager | grep --color "MySQL Master failover" | cut -d'(' -f2 | cut -d':' -f1 | tail -n 1`
str_ip_new_master=`cat $file_log_mha_manager | grep --color "MySQL Master failover" | cut -d'(' -f3 | cut -d':' -f1 | tail -n 1`
str_ip_mha_manager="192.168.238.131"
# 为[change master]准备的参数
str_log_file_new_master=""
str_log_pos_new_master=""
# Part:: String SQL
str_sql_mysql_change_master=""
# --------------------------
# function
function do_sql() {
# variable
func_str_ip="$1"
func_str_sql="$2"
# action
# 本场景中不涉及到对MySQL某个库的操作,所以没有选择[db]
mysql -u $str_mysql_username -h $func_str_ip -p"$str_mysql_password" -N -e "$func_str_sql" -P3306
}
# 获取主库状态信息 非GTID复制
#function get_info_mysql_master_new_master() {
# version ONE
#str_log_file_new_master=`do_sql "$str_ip_new_master" "show master status" | awk '{print $1}'`
#str_log_pos_new_master=`do_sql "$str_ip_new_master" "show master status" | awk '{print $2}'`
#}
# 生成orig_master作为slave加入new_master的[change master]SQL命令
function gen_sql_mysql_change_master() {
func_temp_master_host_sed=`cat $file_log_mha_manager | grep --color "All other slaves should start" | tail -n 1 | cut -d',' -f1 | cut -d'=' -f2 | cut -d\' -f2`
func_temp_repl_password_sed=`cat $file_log_mha_manager | grep --color "All other slaves should start" | tail -n 1 | rev | cut -d\' -f2`
echo "======================"
echo "@@ func variable: func_temp_repl_password_sed = $func_temp_repl_password_sed"
echo "======================"
str_sql_mysql_change_master=`cat $file_log_mha_manager | grep --color "All other slaves should start" | tail -n 1 | sed "s/'$func_temp_repl_password_sed'/'$str_repl_password'/g" | cut -d':' -f4`
str_sql_mysql_change_master=`echo $str_sql_mysql_change_master | sed "s/'$func_temp_master_host_sed'/'$str_ip_new_master'/g"`
}
# 对指定主机执行Linux命令
# 前提:
# 1. IP可达
# 2. SSH等价关系
function do_linux_by_ssh() {
# variable
func_str_ip="$1"
func_str_user="$2"
func_str_command="$3"
# action
ssh -T -t $func_str_user@$func_str_ip "$func_str_command"
}
function do_part_orig_master_is_new_slave() {
do_linux_by_ssh "$str_ip_orig_master" "root" "service mysql start"
do_sql "$str_ip_orig_master" "set global read_only=1;"
do_sql "$str_ip_orig_master" "$str_sql_mysql_change_master"
do_sql "$str_ip_orig_master" "start slave;"
}
function do_part_mha_master_manager_start() {
do_linux_by_ssh "$str_ip_mha_manager" "root" "nohup masterha_manager --conf=$file_conf_mha_application --ignore_last_failover &"
}
# 如果PID不存在,则执行该脚本,否则,退出
function runable_by_mha_manager_pid() {
echo "-----------------"
echo "Script for MySQL Master HA"
echo "-----------------"
echo "Begin:: "`date "+|%Y-%m-%d|%H:%M:%S|"`
if [[ "$str_pid_masterha_manager" == "" ]]
then
echo "## masterha_manager is [NOT ALIVED]."
else
echo "## masterha_manager is [ALIVED]."
echo "[masterha_manager] PID is:: $str_pid_masterha_manager"
# do something.
echo "## Exit Script"
exit 0
fi
}
# --------------------------
# action
# 如果PID不存在,则执行该脚本,否则,退出
echo "------------------"
echo "app: runable_by_mha_manager_pid"
runable_by_mha_manager_pid
echo ""
echo "------------------"
echo "app: gen_sql_mysql_change_master"
gen_sql_mysql_change_master
echo ""
echo "------------------"
echo "app: do_part_orig_master_is_new_slave"
do_part_orig_master_is_new_slave
echo ""
echo "------------------"
echo "app: do_part_mha_master_manager_start"
#do_part_mha_master_manager_start 如果脚本在费manager端,则使用该函数,否则就是用如下命令
nohup masterha_manager --conf=$file_conf_mha_application --ignore_last_failover &
echo ""
# --------------------------
# Show time
# ---------
# version one
# ---------
#echo "new master is:: $str_ip_new_master"
#echo "Master log file is:: $str_log_file_new_master"
#echo "Master log POS is:: $str_log_pos_new_master"
#echo "orig master --> new master ## SQL: CHANGE MASTER ## is:: $str_sql_mysql_change_master"
# ---------
# version two
# ---------
echo "================="
echo "MySQL info:"
echo "## Account and Password"
echo "username @ $str_mysql_username"
echo "password @ $str_mysql_password"
echo "--- for REPLICATION ---"
echo "repl @ username ## $str_repl_username"
echo "repl @ password ## $str_repl_password"
echo "## Master Server info"
echo "log file @ Master ## $str_log_file_new_master"
echo "log pos @ Master ## $str_log_pos_new_master"
echo ""
echo "================="
echo "SQL statement:"
echo "[CHANGE MASTER] -->"
echo "$str_sql_mysql_change_master"
echo ""
echo "================="
echo "MasterHA info:"
echo "## File and Path"
echo "MHA Global config file @ $file_conf_mha_global"
echo "MHA Application config file @ $file_conf_mha_application"
echo "MHA Log file:: masterha_manager @ $file_log_mha_manager"
echo "## Architecture"
echo "Candicate Server list::"
echo "$list_ip_candicate"
echo "## IP"
echo "MHA Manager Server:: $str_ip_mha_manager"
echo "Last:: new master:: $str_ip_new_master"
echo "Last:: orig master:: $str_ip_orig_master"
echo ""
# --------------------------
echo "-----------------"
echo "Finished:: "`date "+|%Y-%m-%d|%H:%M:%S|"`
# Done
mha && 脚本拉起的更多相关文章
- MHA脚本master_ip_failover.pl(三)
#!/usr/bin/env perl use strict;use warnings FATAL => 'all'; use Getopt::Long; my ( $command, $ssh ...
- shell 脚本拉取svn代码,vim中文乱码解决办法
VIM安装成功后可以使用,但对中文是乱码,解决方法是在vim的配置文档中添加相关设置即可: 找到etc/vimrc 编辑~/.vimrc文件,加上如下几行: set fileencodings=utf ...
- MHA(下)
一.配置VIP漂移 主机名 IP地址(NAT) 漂移VIP 描述 mysql-db01 eth0:192.168.0.51 VIP:192.168.0.60 系统:CentOS6.5(6.x都可以) ...
- MySQL集群架构:MHA+MySQL-PROXY+LVS实现MySQL集群架构高可用/高性能-技术流ken
MHA简介 MHA可以自动化实现主服务器故障转移,这样就可以快速将从服务器晋级为主服务器(通常在10-30s),而不影响复制的一致性,不需要花钱买更多的新服务器,不会有性能损耗,容易安装,不必更改现有 ...
- mysql MHA架构搭建过程
[环境介绍] 系统环境:Red Hat Enterprise Linux 7 + 5.7.18 + MHA version 0.57 系统 IP 主机名 备注 版本 xx系统 192.168.142. ...
- mha高可用以及读写分离
一.MHA简介 二.工作流程 三.MHA架构图 四.MHA工具介绍 五.基于GTID的主从复制 六.部署MHA 七.配置VIP漂移 八.配置binlog-server 九.MySQL中间件Atlas ...
- 批量拼脚本神器-NimbleText
工作中要给产品经理写各种脚本拉数据.修改数据.这种批量拼sql,Excel当然是最合适的.但是苦于Excel玩不转,之前一直用Visual Studio Code的多焦点编辑功能,即便如此,这在同事眼 ...
- mysql高可用架构 -> MHA配置VIP漂移-05
VIP漂移的两种方式 1)通过keepalived的方式,管理虚拟IP的漂移 2)通过MHA自带脚本方式,管理虚拟IP的漂移 MHA脚本方式 虚拟ip漂移的脚本下载地址 -> wget http ...
- (MHA+MYSQL-5.7增强半同步)高可用架构设计与实现
架构使用mysql5.7版本基于GTD增强半同步并行复制配置 reploication 一主两从,使用MHA套件管理整个复制架构,实现故障自动切换高可用 优势: ...
随机推荐
- Java语言的Hook实现
引言:最近在玩完美时空的诛仙Online(不知道这里有没人有共同爱好的),这个游戏每晚七点会出现一个任务"新科试炼".这个任务简单地说就是做选择题,范围小到柴米油盐,大到世界大千, ...
- 洛谷 P1400 塔
P1400 塔 题目描述 有N(2<=N<=600000)块砖,要搭一个N层的塔,要求:如果砖A在砖B上面,那么A不能比B的长度+D要长.问有几种方法,输出 答案 mod 10000000 ...
- Service-监听手机来电
public class MonitorPhone extends Activity { TelephonyManager tManager; @Override protected void onC ...
- iOS_31_cocos2d_微信飞机
终于效果图: 纹理素材 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize/400 ...
- Spring中事务的XML方式[声明方式]
事务管理: 管理事务,管理数据,数据完整性和一致性 事务[业务逻辑] : 由一系列的动作[查询书价格,更新库存,更新余额],组成一个单元[买书业务], 当我们动作当中有一个错了,全错~ ACID 原子 ...
- HDU 1142 A Walk Through the Forest(最短路+dfs搜索)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- POJ 1874 畅通工程续(最短路模板题)
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- BZOJ3530: [Sdoi2014]数数(Trie图,数位Dp)
Description 我们称一个正整数N是幸运数,当且仅当它的十进制表示中不包含数字串集合S中任意一个元素作为其子串.例如当S=(22,333,0233)时,233是幸运数,2333.20233.3 ...
- Python Jsonpath模块用法
在使用Python做自动化校验的时候,经常会从Json数据中取值,所以会用到Jsonpath模块,这里做个简单的总结 1.关于jsonpath用来解析多层嵌套的json数据;JsonPath 是一种信 ...
- SpringMVC,Mybatis,FreeMarker连接mycat示例(一)
首页 > 程序开发 > 软件开发 > Java > 正文 SpringMVC,Mybatis,FreeMarker连接mycat示例(一) 项目结构如图: 首先是各种配置文件, ...