MHA高可用+VIP 集群故障转移(已测试成功)
服务器部署说明
192.168.158.201 mha管理,mysql主服
192.168.158.202 mha节点,mysql从服
192.168.158.203 mha节点,mysql从服
Manager命令说明
masterha_check_ssh 检查MHA的SSH配置状况
masterha_check_repl 检查MySQL复制状况
masterha_manger 启动MHA
masterha_check_status 检测当前MHA运行状态
masterha_master_monitor 检测master是否宕机
masterha_master_switch 控制故障转移(自动或者手动)
masterha_conf_host 添加或删除配置的server信息
Node命令说明
save_binary_logs 保存和复制master的二进制日志
apply_diff_relay_logs 识别差异的中继日志事件并将其差异的事件应用于其他的slave
filter_mysqlbinlog 去除不必要的ROLLBACK事件(MHA已不再使用这个工具)
purge_relay_logs 清除中继日志(不会阻塞SQL线程)
[生产环境最好搭建专属yum源]
更新yum源为aliyun[可选操作]
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
yum clean all
yum makecache
配置各台服务器之间ssh连接免密码
ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.158.201
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.158.202
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.158.203
在各台服务器上安装
rpm -ivh epel-release-6-8.noarch
rpm -qa |grep epel-release
yum -y install perl-DBD-MySQL
rpm -ivh mha4mysql-node-0.56-0.el6.noarch
rpm -qa |grep mha4mysql
mkdir -p /etc/masterha/app1
mkdir -p /etc/masterha
#建立配置文件
##################################
cat >/etc/masterha/myha.cnf<< EOF
[server default]
manager_workdir=/masterha/app1
manager_log=/masterha/app1/manager.log
#mysql管理员和密码
user=manager
password=mgr2016
ssh_user=root
#mysql同步密码
repl_user=rep
repl_password=rep2016
ping_interval=1
shutdown_script=""
#主库发生故障后转移VIP脚本
master_ip_failover_script="/usr/local/bin/master_ip_failover"
master_ip_online_change_script=""
report_script=""
[server1]
hostname=192.168.158.201
#mysql logbin文件目录
master_binlog_dir=/data/binlog
#主库要有这个参数
candidate_master=1
[server2]
hostname=192.168.158.202
candidate_master=1
master_binlog_dir=/data/binlog
[server3]
hostname=192.168.158.203
candidate_master=1
master_binlog_dir=/data/binlog
EOF
##################################
cat >masterha_default.cnf<<EOF
[server default]
user=manager
password=mgr2016
ssh_user=root
repl_user=rep
repl_password=rep2016
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"
#shutdown_script= /script/masterha/power_manager
#report_script=""
#secondary_check_script = masterha_secondary_check -s node1 -s node2 -s node3 --user=manager --master_host=node1 --master_ip=192.168.158.201 --master_port=3306
EOF
##################################
【安装mysql5.6数据库并设置主从,这里省略……】
chkconfig --add mysqld
service mysqld start
GRANT REPLICATION SLAVE ON *.* TO rep@'192.168.%' IDENTIFIED BY 'rep2016';
GRANT all ON *.* TO 'manager'@'192.168.%' IDENTIFIED BY 'mgr2016';
flush privileges;
CHANGE MASTER TO MASTER_HOST='192.168.158.201', MASTER_USER='rep',MASTER_PASSWORD='rep2016',master_port=3306;
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
echo 'PATH=$PATH:/usr/local/mysql/bin'>>/etc/profile
. /etc/profile
##################################
cat > /etc/my.cnf << EOF
[client]
port = 3306
socket = /data/mysql.sock
#Here follows entries for some specific programs
# The MySQL server
[mysqld]
port = 3306
user = mysql
log-bin = /data/binlog/mysqlbin
socket = /data/mysql.sock
pid-file = /data/mysql.pid
log_error = /data/mysql-error.log
expire-logs-days = 14
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin
slow_query_log = 1
slow_query_log_file=/data/slowsql.log
basedir = /usr/local/mysql
datadir = /data
tmpdir = /data/tmp
character_set_server = utf8
skip-name-resolve
[mysql]
no-auto-rehash
prompt="\\u@\\h:\p \\R:\\m:\\s [\\d]>"
[mysqld_safe]
log-error=/data/logs/mysqld.log
EOF
##################################
管理节点上安装以下包
yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch \
perl-Parallel-ForkManager perl-Time-HiRes
rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm
VIP配置
ifconfig eth0:1 192.168.158.88/24
#测试ssh 配置
/usr/bin/masterha_check_ssh --conf=/etc/masterha/myha.cnf
#测试主从配置
masterha_check_repl --conf=/etc/masterha/myha.cnf
#master 切换测试
masterha_master_switch --master_state=alive --conf=/etc/masterha/myha.cnf --new_master_host=192.168.158.202 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=1000
#查看192.168.158.202主机IP情况
#查看192.168.158.203从库的slave status;
#开启管理节点进程
nohup masterha_manager --conf=/etc/masterha/myha.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha_manger.log 2>&1 &
#停止
masterha_stop --conf=/etc/masterha/myha.cnf
#查状态
masterha_check_status --conf=/etc/masterha/myha.cnf
##################################
VIP切换perl脚本
cat /usr/local/bin/master_ip_failover
#!/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.158.88/24'; # Virtual IP
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
$ssh_user = "root";
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 ($@) {
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\@cluster1 \" $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";
}
##################################
192.168.158.201 mha管理,mysql主服
192.168.158.202 mha节点,mysql从服
192.168.158.203 mha节点,mysql从服
Manager命令说明
masterha_check_ssh 检查MHA的SSH配置状况
masterha_check_repl 检查MySQL复制状况
masterha_manger 启动MHA
masterha_check_status 检测当前MHA运行状态
masterha_master_monitor 检测master是否宕机
masterha_master_switch 控制故障转移(自动或者手动)
masterha_conf_host 添加或删除配置的server信息
Node命令说明
save_binary_logs 保存和复制master的二进制日志
apply_diff_relay_logs 识别差异的中继日志事件并将其差异的事件应用于其他的slave
filter_mysqlbinlog 去除不必要的ROLLBACK事件(MHA已不再使用这个工具)
purge_relay_logs 清除中继日志(不会阻塞SQL线程)
[生产环境最好搭建专属yum源]
更新yum源为aliyun[可选操作]
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
yum clean all
yum makecache
配置各台服务器之间ssh连接免密码
ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.158.201
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.158.202
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.158.203
在各台服务器上安装
rpm -ivh epel-release-6-8.noarch
rpm -qa |grep epel-release
yum -y install perl-DBD-MySQL
rpm -ivh mha4mysql-node-0.56-0.el6.noarch
rpm -qa |grep mha4mysql
mkdir -p /etc/masterha/app1
mkdir -p /etc/masterha
#建立配置文件
##################################
cat >/etc/masterha/myha.cnf<< EOF
[server default]
manager_workdir=/masterha/app1
manager_log=/masterha/app1/manager.log
#mysql管理员和密码
user=manager
password=mgr2016
ssh_user=root
#mysql同步密码
repl_user=rep
repl_password=rep2016
ping_interval=1
shutdown_script=""
#主库发生故障后转移VIP脚本
master_ip_failover_script="/usr/local/bin/master_ip_failover"
master_ip_online_change_script=""
report_script=""
[server1]
hostname=192.168.158.201
#mysql logbin文件目录
master_binlog_dir=/data/binlog
#主库要有这个参数
candidate_master=1
[server2]
hostname=192.168.158.202
candidate_master=1
master_binlog_dir=/data/binlog
[server3]
hostname=192.168.158.203
candidate_master=1
master_binlog_dir=/data/binlog
EOF
##################################
cat >masterha_default.cnf<<EOF
[server default]
user=manager
password=mgr2016
ssh_user=root
repl_user=rep
repl_password=rep2016
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"
#shutdown_script= /script/masterha/power_manager
#report_script=""
#secondary_check_script = masterha_secondary_check -s node1 -s node2 -s node3 --user=manager --master_host=node1 --master_ip=192.168.158.201 --master_port=3306
EOF
##################################
【安装mysql5.6数据库并设置主从,这里省略……】
chkconfig --add mysqld
service mysqld start
GRANT REPLICATION SLAVE ON *.* TO rep@'192.168.%' IDENTIFIED BY 'rep2016';
GRANT all ON *.* TO 'manager'@'192.168.%' IDENTIFIED BY 'mgr2016';
flush privileges;
CHANGE MASTER TO MASTER_HOST='192.168.158.201', MASTER_USER='rep',MASTER_PASSWORD='rep2016',master_port=3306;
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
echo 'PATH=$PATH:/usr/local/mysql/bin'>>/etc/profile
. /etc/profile
##################################
cat > /etc/my.cnf << EOF
[client]
port = 3306
socket = /data/mysql.sock
#Here follows entries for some specific programs
# The MySQL server
[mysqld]
port = 3306
user = mysql
log-bin = /data/binlog/mysqlbin
socket = /data/mysql.sock
pid-file = /data/mysql.pid
log_error = /data/mysql-error.log
expire-logs-days = 14
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin
slow_query_log = 1
slow_query_log_file=/data/slowsql.log
basedir = /usr/local/mysql
datadir = /data
tmpdir = /data/tmp
character_set_server = utf8
skip-name-resolve
[mysql]
no-auto-rehash
prompt="\\u@\\h:\p \\R:\\m:\\s [\\d]>"
[mysqld_safe]
log-error=/data/logs/mysqld.log
EOF
##################################
管理节点上安装以下包
yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch \
perl-Parallel-ForkManager perl-Time-HiRes
rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm
VIP配置
ifconfig eth0:1 192.168.158.88/24
#测试ssh 配置
/usr/bin/masterha_check_ssh --conf=/etc/masterha/myha.cnf
#测试主从配置
masterha_check_repl --conf=/etc/masterha/myha.cnf
#master 切换测试
masterha_master_switch --master_state=alive --conf=/etc/masterha/myha.cnf --new_master_host=192.168.158.202 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=1000
#查看192.168.158.202主机IP情况
#查看192.168.158.203从库的slave status;
#开启管理节点进程
nohup masterha_manager --conf=/etc/masterha/myha.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha_manger.log 2>&1 &
#停止
masterha_stop --conf=/etc/masterha/myha.cnf
#查状态
masterha_check_status --conf=/etc/masterha/myha.cnf
##################################
VIP切换perl脚本
cat /usr/local/bin/master_ip_failover
#!/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.158.88/24'; # Virtual IP
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
$ssh_user = "root";
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 ($@) {
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\@cluster1 \" $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 {
"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";
}
##################################