一套优秀的MySQL高可用环境下故障切换和主从复制的软件
MySQL故障过程中,MHA能做到0~30秒内自动完成故障切换
MHA Manager (管理节点)
MHA Node(数据节点)
自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据不丢失
使用半同步复制,可以大大降低数据丢失的风险
目前MHA支持一主多从架构,最少三台服务,即一主两从
同步,异步,半同步
1.基础环境
主机名称 | ip地址 | 配置 | 系统 |
manager | 192.168.27.131 | 2c4G | centos7最小化 |
master | 192.168.27.129 | 2C4G | centos7最小化 |
slave1 | 192.168.27.130 | 2c4G | centos7最小化 |
slave2 | 192.168.27.132 | 2c4G | centos7最小化 |
2.配置hosts解析
192.168.27.131 manager
192.168.27.130 slave1
192.168.27.129 master
192.168.27.132 slave2
3.配置master,slave1,slave2的主从复制(在master,slave1,slave2节点安装MySQL,部署主从复制,master为主节点,slave1和slave2为从节点)
4.在两台从节点的数据库中执行只读权限
set global read_only=1;
5.所有机器执行yum命令
yum install -y epel-release
yum install -y perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN
6.所有安装完毕后,上传软件包(mha4mysql-node-0.57.tar.gz)执行安装node
tar zxvf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make
make install
7.安装mha4mysql-manager-0.57.tar.gz,在manager节点上传包(mha4mysql-manager-0.57.tar.gz),编译安装manager
cd
tar zxvf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make
make install
cd
8.在manager节点配置manager免密
1.执行命令,直接回车到生成密钥
ssh-keygen
2.设置三台机器的免密
#一步一步的执行,输入yes后回车,输入密码
ssh-copy-id master
ssh-copy-id slave1
ssh-copy-id slave2
9.设置另外三台节点的免密,(跟第八步一样,注意不要敲错命令)
10.查看脚本,移动脚本进行编辑(删除原有内容,替换文本内容,修改自己的IP网段)
[root@manager ~]# ll /root/mha4mysql-manager-0.57/samples/scripts/
总用量 32
-rwxr-xr-x 1 1001 1001 3648 5月 31 2015 master_ip_failover
-rwxr-xr-x 1 1001 1001 9870 5月 31 2015 master_ip_online_change
-rwxr-xr-x 1 1001 1001 11867 5月 31 2015 power_manager
-rwxr-xr-x 1 1001 1001 1360 5月 31 2015 send_report
[root@manager ~]#
- master_ip_failover:自动切换时 VIP 管理的脚本
- master_ip_online_change:在线切换时vip 的管理
- power_manager:故障发生后关闭主机的脚本
- send_report:因故障切换后发送报警的脚本
ll /root/mha4mysql-manager-0.57/samples/scripts/
#复制“master_ip_failover”脚本到/usr/local/bin 目录,这里使用脚本管理 VIP,也是推荐的一种方式,生产环境不建议使用 Keepalived
cp /root/mha4mysql-manager-0.57/samples/scripts/* /usr/local/bin
vi /usr/local/bin/master_ip_failover
#!/usr/bin/env perl
# Copyright (C) 2011 DeNA Co.,Ltd.
# 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
);
#改为自己网段的没有使用过的IP地址
my $vip = '192.168.27.200/24';
my $key = '1';
#需要前提能够使用ifconfig命令,请提前准备好
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip up";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$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,
'new_master_user=s' => \$new_master_user,
'new_master_password=s' => \$new_master_password,
);
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 \"`;
}
sub stop_vip() {
return 0 unless ($ssh_user);
`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";
}
创建存放配置文件的目录,复制模板,删除原有内容,替换文本内容(修改为自己的内容)
mkdir /etc/masterha
cp /root/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/
vi /etc/masterha/app1.cnf
[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/manager.log
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
user=mha
password=manager
ping_interval=1
remote_workdir=/tmp
repl_user=myslave
repl_password=123456
#以下IP修改为自己的两台从节点的IP
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.27.130 -s 192.168.27.132
shutdown_script=""
ssh_user=root
[server1]
#修改为自己的master节点的IP
hostname=192.168.27.129
port=3306
[server2]
#修改为自己的slave1节点的IP
hostname=192.168.27.130
port=3306
candidate_master=1
check_repl_delay=0
[server3]
#修改为自己的slave2节点的IP
hostname=192.168.27.132
port=3306
在manager节点创建配置所需的文件目录
mkdir -p /var/log/masterha/app1
在主服务数据库上执行sql同步到两台从服务数据库上
grant all privileges on *.* to 'mha'@'192.168.27.%' identified by 'manager';
GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.27.%' IDENTIFIED BY '123456';
在主从三台服务上进行软连接
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
校验配置信息,在manager服务上执行下列代码验证
# 测试 ssh 无密码认证,如果正常最后会输出 successfully
masterha_check_ssh -conf=/etc/masterha/app1.cnf
#回馈信息是is OK代表效验通过
masterha_check_repl -conf=/etc/masterha/app1.cnf
在master节点创建网卡接口
ifconfig ens33:1 192.168.27.200
在manager 以无挂断的方式在后台启动 masterha_manager
,根据指定的配置文件来管理 MySQL 主从复制集群,并将所有输出记录到指定的日志文件中
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 &
#在master停掉mysqld服务,模拟数据库损坏,MHA切换数据库
ifconfig #查看刚刚创建的虚拟接口是否生成
systemctl stop mysqld #关闭服务