内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。
风哥提示:
本文档介绍高可用数据库架构的完整部署方案。
Part01-MySQL MHA部署
1.1 MHA架构说明
[root@architect ~]# cat > /root/mha-architecture.txt << 'EOF' MySQL MHA架构 ============ 组件说明: - MHA Manager: 管理节点,学习交流加群风哥QQ113257174负责监控和故障转移 - MHA Node: 数据节点,运行在每个MySQL服务器上 架构拓扑: +-------------------+ | MHA Manager | | 192.168.1.60 | +--------+----------+ | +----+----+----+----+ | | | +---v---+ +---v---+ +---v---+ | Master | | Slave1| | Slave2| |.1.40 | | .1.41 | | .1.42 | +--------+ +--------+ +--------+ 工作流程: 1. Manager监控Master状态 2. Master故障时自动选举新Master 3. 从其他Slave同步差异数据 4. 将应用切换到新Master 5. 原Master恢复后作为Slave加入 EOF
1.2 安装MHA
[root@mysql-master ~]# dnf install -y perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch
[root@mysql-master ~]# rpm -ivh mha4mysql-node-0.58-0.el8.noarch.rpm
# Manager节点安装MHA Manager
[root@mha-manager ~]# dnf install -y perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager
[root@mha-manager ~]# rpm -ivh mha4mysql-manager-0.58-0.el8.noarch.rpm
# 配置SSH免密登录
[root@mha-manager ~]# ssh-keygen -t rsa -N ” -f ~/.ssh/id_rsa
[root@mha-manager ~]# ssh-copy-id root@192.168.学习交流加群风哥微信: itpux-com1.40
[root@mha-manager ~]# ssh-copy-id root@192.168.1.41
[root@mha-manager ~]# ssh-copy-id root@192.168.1.42
# 配置MySQL主从复制
[root@mysql-master ~]# cat > /etc/my.cnf.d/replication.cnf << 'EOF'
[mysqld]
server-id = 1
log-bin = mysql-bin
binlog_format = ROW
gtid_mode = ON
enforce_gtid_consistency = ON
log_slave_updates = ON
EOF
[root@mysql-master ~]# systemctl restart mysqld
# 创建复制用户
[root@mysql-master ~]# mysql -u root -p
mysql> CREATE USER ‘repl’@’%’ IDENTIFIED BY ‘Repl@123456’;
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO ‘repl’@’%’;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE USER ‘mha’@’更多学习教程公众号风哥教程itpux_com%’ IDENTIFIED BY ‘Mha@123456’;
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO ‘mha’@’%’;
Query OK, 0 rows affected (0.00 sec)
# 配置Slave
[root@mysql-slave1 ~]# cat > /etc/my.cnf.d/replication.cnf << 'EOF'
[mysqld]
server-id = 2
log-bin = mysql-bin
binlog_format = ROW
gtid_mode = ON
enforce_gtid_consistency = ON
log_slave_updates = ON
read_only = ON
relay_log_purge = 0
EOF
[root@mysql-slave1 ~]# systemctl restart mysqld
# 启动复制
[root@mysql-slave1 ~]# mysql -u root -p
mysql> CHANGE MASTER TO
-> MASTER_HOST=’192.168.1.40′,
-> MASTER_USER=’repl’,
-> MASTER_PASSWORD=’Repl@123456′,
-> MASTER_AUTO_POSITION=1;
Query OK, 0 rows affected (0.02 sec)
mysql> START SLAVE;
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.40
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-slave1-relay-bin.000002
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Part02-MHA配置
2.1 MHA Manager配置
[root@mha-manager ~]# mkdir -p /etc/mha /var/log/mha
# 配置MHA
[root@mha-manager ~]# cat > /etc/mha/app1.cnf << 'EOF'
[server default]
manager_workdir=/var/log/mha/app1
manager_log=/var/log/mha/app1/manager.log
master_binlog_dir=/var/lib/mysql
master_ip_failover_script=/usr/local/bin/master_ip_failover
ssh_user=root
repl_user=repl
repl_password=Repl@123456
user=mha
password=Mha@123456
ping_interval=3
[server1]
hostname=192.168.1.40
port=3306
candidate_master=1
[server2]
hostname=192.168.1.41
port=3306
candidate_master=1
[server3]
hostname=192.168.1.42
port=3306
candidate_master=0
EOF
# VIP切换脚本
[root@mha-manager ~]# cat > /usr/local/bin/master_ip_failover << 'EOF'
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
my $vip = '192.168.1.100';
my $key = '1';
my $ssh_start_vip = "ip addr add $vip/24 dev eth0 label eth0:$key";
my $ssh_stop_vip = "ip addr del $vip/24 dev eth0 label eth0:$key";
GetOptions(
'command=s' => \my $command,
‘ssh_user=s’ => \my $ssh_user,
‘orig_master_host=s’ => \my $orig_master_host,
‘new_master_host=s’ => \my $new_master_host,
);
exit &main();
sub main {
if ($command eq ‘stop’ || $command eq ‘stopssh’) {
print “Stopping VIP on $orig_master_host…\n”;
my $result = SSHExecute($orig_master_host, $ssh_stop_vip);
print “Result: $result\n”;
} elsif ($command eq ‘start’) {
print “Starting VIP on $new_master_host…\n”;
my $result = SSHExecute($new_master_host, $ssh_start_vip);
print “Result: $result\n”;
}
return 0;
}
sub SSHExecute {
my ($host, $cmd) = @_;
return `ssh $ssh_user\@$host “$cmd” 2>&1`;
}
EOF
[root@mha-manager ~]# chmod +x /usr/local/bin/master_ip_failover
# 检查SSH连接
[root@mha-manager ~]# masterha_check_ssh –conf=/etc/mha/app1.cnf
Sat Apr 4 23:30:00 2026 – [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sat Apr 4 23:30:00 2026 – [info] Reading application default configuration from /etc/mha/app1.cnf..
Sat Apr 4 23:30:00 2026 – [info] Reading server configuration from /etc/mha/app1.cnf..
Sat Apr 4 23:30:00 2026 – [info] Starting SSH connection tests..
Sat Apr 4 23:30:01 2026 – [debug] Connecting via SSH to root@192.168.1.40..
Sat Apr 4 23:30:01 2026 – [debug] Connecting via SSH to root@192.168.更多视频教程www.fgedu.net.cn1.41..
Sat Apr 4 23:30:01 2026 – [debug] Connecting via SSH to root@192.168.1.42..
Sat Apr 4 23:30:02 2026 – [info] All SSH connection tests passed successfully.
# 检查复制
[root@mha-manager ~]# masterha_check_repl –conf=/etc/mha/app1.cnf
Sat Apr 4 23:30:05 2026 – [info] Slaves settings check done.
Sat Apr 4 23:30:05 2026 – [info] 192.168.1.41 (current master)
+–192.168.1.40
+–192.168.1.42
Sat Apr 4 23:30:05 2026 – [info] Checking replication health on 192.168.1.40..
Sat Apr 4 23:30:05 2026 – [info] ok.
Sat Apr 4 23:30:05 2026 – [info] Checking replication health on 192.168.1.42..
Sat Apr 4 23:30:05 2026 – [info] ok.
Sat Apr 4 23:30:05 2026 – [info] Checking master_ip_failover_script status:
Sat Apr 4 23:30:05 2026 – [info] OK.
Sat Apr 4 23:30:05 2026 – [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
# 启动MHA Manager
[root@mha-manager ~]# nohup masterha_manager –conf=/etc/mha/app1.cnf > /var/log/mha/app1/manager.log 2>&1 &
# 查看状态
[root@mha-manager ~]# masterha_check_status –conf=/etc/mha/app1.cnf
app1 (pid:12345) is running(0:PING_OK), master:192.168.1.40
- 确保所有节点时间同步
- 配置SSH免密登录
- 定期测试故障转移
- 监控MHA Manager状态
- 备份MHA配置文件
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
