搭建 MySQL MHA
- 搭建 MySQL MHA
- 实验拓扑图
- 实验环境
- 实验思路
- MHA架构
- 故障模拟
- 实验部署
- 数据库安装
- 主从复制部署
- 时间同步
- 主服务器配置
- 从服务器配置
- 创建链接
- MHA搭建
- 安装依赖的环境
- 安装 node 组件
- 安装 manager 组件
- 配置无密码认证
- 在 manager 节点上配置 MHA
- 管理 mysql 节点服务器
- 创建相关目录
- 编写配置文件
- 手动开启虚拟IP
- 测试 ssh 无密码认证及 MySQL主从连接情况
- 启动 MHA
- 故障模拟———— Master 节点意外故障
- 停止 Master 节点MySQL服务
- 查看 mysql2 是否接管 VIP
- 修复故障
- 启动原来的主节点
- 修复数据
- 在 manager 节点上修改配置文件
- 对mysql1和mysql2 进行重启
- 在 manager 节点上启动 MHA
搭建 MySQL MHA
实验拓扑图
实验环境
主机 | IP |
---|---|
Master主库服务器 | 10.190.33.71 |
Slave从库服务器01 | 10.190.33.72 |
Slave从库服务器02 | 10.190.33.73 |
MHA Manager服务器 | 10.190.33.74 |
Client | 10.190.33.75 |
实验思路
MHA架构
- 数据库安装
- 一主两从
- MHA搭建
故障模拟
- 主库失效
- 备选主库成为主库
- 原故障主库恢复重新加入到MHA成为从库
实验部署
数据库安装
MySQL安装教程
主从复制部署
时间同步
所有服务器均需要进行配置(仅以Master服务器举例)
yum install -y chrony
vim /etc/chrony.conf
#注释原有ntp服务器,添加阿里ntp服务源
server ntp.aliyun.com iburst
systemctl restart chronyd
chronyc sources -v
systemctl enable --now chronyd
主服务器配置
vim /etc/my.cnf
server-id=1
log_bin = mysql-bin
binlog_format = mixed
log-slave-updates = true
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
systemctl restart mysqld
mysql -u root -pAdmin@123
grant replication slave on *.* to 'myslave'@'10.190.33.%' identified by 'Admin@123';
grant all privileges on *.* to 'manager'@'10.190.33.%' identified by 'Admin@123';
grant all privileges on *.* to 'manager'@'10.190.33.71' identified by 'Admin@123';
grant all privileges on *.* to 'manager'@'10.190.33.72' identified by 'Admin@123';
grant all privileges on *.* to 'manager'@'10.190.33.73' identified by 'Admin@123';
flush privileges;
show master status;
从服务器配置
vim /etc/my.cnf
server-id = 2
log_bin = mysql-bin
binlog_format = mixed
log-slave-updates = true
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
systemctl restart mysqld
mysql -u root -pAdmin@123
grant replication slave on *.* to 'myslave'@'10.190.33.%' identified by 'Admin@123';
grant all privileges on *.* to 'manager'@'10.190.33.%' identified by 'Admin@123';
grant all privileges on *.* to 'manager'@'10.190.33.71' identified by 'Admin@123';
grant all privileges on *.* to 'manager'@'10.190.33.72' identified by 'Admin@123';
grant all privileges on *.* to 'manager'@'10.190.33.73' identified by 'Admin@123';
flush privileges;
change master to master_host='10.190.33.71',master_user='myslave',master_password='Admin@123',master_log_file='mysql-bin.000001',master_log_pos=3400;
start slave;
show slave status\G
set global read_only=1;
- Slave1
- Slave2
创建链接
所有的数据库服务器(仅以Master服务器举例)
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
MHA搭建
安装依赖的环境
所有的服务器(仅以Master服务器举例)
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
安装 node 组件
所有的服务器(仅以Master服务器举例)
cd /opt
tar xf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make && make install
安装 manager 组件
manager服务器
cd /opt
tar xf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make && make install
配置无密码认证
所有的服务器
#manager节点上配置到所有数据库节点的无密码认证
ssh-keygen -t rsa
ssh-copy-id 10.190.33.71
ssh-copy-id 10.190.33.72
ssh-copy-id 10.190.33.73
#master节点上配置到数据库节点 slave1 和 slave2的无密码认证
ssh-keygen -t rsa
ssh-copy-id 10.190.33.72
ssh-copy-id 10.190.33.73
#slave1节点上配置到数据库节点 master和 slave2的无密码认证
ssh-keygen -t rsa
ssh-copy-id 10.190.33.71
ssh-copy-id 10.190.33.73
#slave2节点上配置到数据库节点 master和 slave1 的无密码认证
ssh-keygen -t rsa
ssh-copy-id 10.190.33.71
ssh-copy-id 10.190.33.72
- manager
- master
- slave1
- slave2
在 manager 节点上配置 MHA
cp -rp /opt/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
ll /usr/local/bin/scripts/
cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
vim /usr/local/bin/master_ip_failover
###删除所有后;拷贝前输入 :set paste 解决vim粘贴乱序问题
#!/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 = '10.190.33.100';
my $brdc = '10.190.33.255';
my $ifdev = 'ens33'; ##看下自己网卡是ens33则不用动,不是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";
}
管理 mysql 节点服务器
创建相关目录
所有的服务器(仅以Master服务器举例)
mkdir -p /opt/mysql-mha/mha-node
编写配置文件
mkdir -p /opt/mysql-mha/mha-manager
vim /opt/mysql-mha/mysql_mha.cnf
[server default]
manager_log=/opt/mysql-mha/manager.log
manager_workdir=/opt/mysql-mha/mha-manager
remote_workdir=/opt/mysql-mha/mha-node
master_binlog_dir=/usr/local/mysql/data
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
user=manager
password=Admin@123
repl_user=myslave
repl_password=Admin@123
port=3306
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 10.190.33.72 -s 10.190.33.73
shutdown_script=""
ssh_user=root
[server1]
hostname=10.190.33.71
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=10.190.33.72
port=3306
[server3]
hostname=10.190.33.73
port=3306
手动开启虚拟IP
Master服务器
/sbin/ifconfig ens33:1 10.190.33.100/24
ifconfig
测试 ssh 无密码认证及 MySQL主从连接情况
masterha_check_ssh -conf=/opt/mysql-mha/mysql_mha.cnf
#测试 ssh 无密码认证
masterha_check_repl -conf=/opt/mysql-mha/mysql_mha.cnf
#MySQL主从连接情况
启动 MHA
nohup masterha_manager \
--conf=/opt/mysql-mha/mysql_mha.cnf \
--remove_dead_master_conf \
--ignore_last_failover < /dev/null > /var/log/mha_manager.log 2>&1 &
masterha_check_status --conf=/opt/mysql-mha/mysql_mha.cnf
#查看 MHA 状态
cat /opt/mysql-mha/manager.log | grep "current master"
#查看 MHA 日志
故障模拟———— Master 节点意外故障
停止 Master 节点MySQL服务
systemctl stop mysqld
查看 mysql2 是否接管 VIP
ifconfig
修复故障
启动原来的主节点
systemctl start mysqld
修复数据
现主MySQL服务器(原从MySQL服务器1)
mysql -u root -p
mysql> show master status;
在原来的主节点
change master to master_host='10.190.33.72',master_user='myslave',master_password='Admin@123',master_log_file='mysql-bin.000001',master_log_pos=1777;
start slave;
在 manager 节点上修改配置文件
vim /opt/mysql-mha/mysql_mha.cnf
······
[server1]
hostname= 10.190.33.71
port=3306
······
对mysql1和mysql2 进行重启
systemctl restart mysqld
在 manager 节点上启动 MHA
masterha_stop --conf=/opt/mysql-mha/mysql_mha.cnf
nohup masterha_manager \
--conf=/opt/mysql-mha/mysql_mha.cnf \
--remove_dead_master_conf \
--ignore_last_failover < /dev/null > /var/log/mha_manager.log 2>&1 &