内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。
本文档介绍高可用数据库集群部署综合实战案例。
风哥提示:
Part01-MySQL高可用架构
1.1 MySQL MHA部署
[root@fgedu-db1 ~]# yum install -y perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager
# 下载MHA
[root@fgedu-db1 ~]# wget https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58-0.el7.centos.noarch.rpm
[root@fgedu-db1 ~]# wget https://github.com/yoshinorim/mha4mysql-manager/releases/download/v0.58/mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
[root@fgedu-db1 ~]# rp更多学习教程公众号风哥教程itpux_comm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm
[root@fgedu-db1 ~]# rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
# 配置SSH免密登录
[root@fgedu-db1 ~]# ssh-keygen -t rsa -N ” -f ~/.ssh/id_rsa
[root@fgedu-db1 ~]# ssh-copy-id root@fgedu-db2
[root@fgedu-db1 ~]# ssh-copy-id root@fgedu-db3
# 配置MySQL主从
# 主库配置
[root@fgedu-db1 ~]# cat > /etc/my.cnf.d/mysql-server.cnf << 'EOF'
[mysqld]
server-id = 1
log-bin = mysql-bin
binlog-format = ROW
gtid-mode = ON
enforce-gtid-consistency = ON
log-slave-updates = ON
master_info_repository = TABLE
relay_log_info_repository = TABLE
EOF
# 从库配置
[root@fgedu-db2 ~]# cat > /etc/my.cnf.d/mysql-server.cnf << 'EOF'
[mysqld]
server-id = 2
log-bin = mysql-bin
binlog-format = ROW
gtid-mode = ON
enforce-gtid-consistency = ON
log-slave-updates = ON
relay_log_info_repository = TABLE
read_only = ON
EOF
# 配置MHA
[root@fgedu-mha ~]# mkdir -p /etc/mha
[root@fgedu-mha ~]# cat > /etc/mha/app1.cnf << 'EOF'
[server default]
manager_workdir=/var/log/mha/app1
manager_log=/var/log/mha/app1/manager.log
user=mha
password=Mha@123
repl_user=repl
repl_password=Repl@123
ssh_user=root
ping_interval=3
secondary_check_script=/usr/local/bin/masterha_secondary_check -s fgedu-db2 -s fgedu-db3
master_ip_failover_script=/usr/local/bin/master_ip_failover
shutdown_script=/usr/local/bin/power_manager
[server1]
hostname=192.168.1.10
candidate_master=1
check_repl_delay=0
[server2]
hostname=192.168.1.11
candidate_master=1
check_repl_delay=0
[server3]
hostname=192.168.1.12
candidate_master=0
EOF
# 检查MHA配置
[root@fgedu-mha ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf
Sat Apr 4 23:00:00 2026 - [info] All SSH connection tests passed successfully.
[root@fgedu-mha ~]# masterha_check_repl --conf=/etc/mha/app1.cnf
MySQL Replication Health is OK.
# 启动MHA Manager
[root@fgedu-mha ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf &
Part02-MySQL读写分离
2.1 ProxySQL配置
[root@fgedu-proxysql ~]# yum install -y proxysql
# 配置ProxySQL
[root@fgedu-proxysql ~]# mysql -u admin -padmin -h 127.0.0.1 -P 6032 << 'EOF'
-- 添加MySQL服务器
INSERT INTO mysql_servers (hostgroup_id, hostname, port, weight) VALUES
(10, '192.168.1.10', 3306, 1),
(10, '192.168.1.11', 3306, 1),
(20, '192.168.1.12', 3306, 1);
-- 配置写组(主库)
INSERT INTO mysql_hostgroups (writer_hostgroup, reader_hostgroup, active) VALUES (10, 20, 1);
-- 添加监控用户
UPDATE global_variables SET variable_value='monitor' WHERE variable_name='mysql-monitor_username';
UPDATE global_variables SET variable_value='Monitor@123' WHERE variable_name='mysql-monitor_password';
-- 添加应用用户
INSERT INTO mysql_users (username, password, default_hostgroup) VALUES ('app', 'App@123', 10);
-- 配置查询规则
INSERT INTO mysql_query_rules (rule_id, match_pattern, destination_hostgroup, apply) VALUES
(1, '^SELECT', 20, 1),
(2, '.*', 10, 1);
-- 应用配置
LOAD MYSQL SERVERS TO RUNTIME;
LOAD MYSQL USERS TO RUNTIME;
LOAD MYSQL QUERY RULES TO RUNTIME;
SAVE MYSQL SERVERS TO DISK;
SAVE MYSQL USERS TO DISK;
SAVE MYSQL QUERY RULES TO DISK;
EOF
# 验证ProxySQL
[root@fgedu-proxysql ~]# mysql -u admin -padmin -h 127.0.0.1 -P 6032 -e "SELECT * FROM mysql_servers;"
+--------------+--------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+--------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10 | 192.168.1.10 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 10 | 192.168.1.11 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 20 | 192.168.1.12 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
+--------------+--------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
Part03-Redis集群部署
3.1 Redis Cluster配置
[root@fgedu-redis1 ~]# yum install -y redis
# 配置Redis节点(6个节点)
[root@fgedu-redis1 ~]# cat > /etc/redis.conf << 'EOF'
bind 0.0.0.0
port 6379
daemonize yes
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
appendonly yes
requirepass Redis@123
masterauth Redis@123
EOF
# 启动Redis
[root@fgedu-redis1 ~]# systemctl enable redis --now
# 创建集群
[root@fgedu-redis1 ~]# redis-cli --cluster create \
192.168.1.20:6379 192.168.1.21:6379 192.168.1.22:6379 \
192.更多视频教程www.fgedu.net.cn168.1.23:6379 192.168.1.24:6379 192.168.1.25:6379 \
--cluster-replicas 1 -a Redis@123
>>> Performing hash slots allocation on 6 nodes…
Master[0] -> Slots 0 – 5460
Master[1] -> Slots 5461 – 10922
Master[2] -> Slots 10923 – 16383
Adding replica 192.168.1.24:6379 to 192.168.1.20:6379
Adding replica 192.168.1.25:6379 to 192.168.1.21:6379
Adding replica 192.168.1.23:6379 to 192.168.1.22:6379
…
[OK] All 16384 slots covered.
# 验证集群状态
[root@fgedu-redis1 ~]# redis-cli -c -a Redis@123 cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_sent:1234
cluster_stats_messages_received:1234
# 测试集群
[root@fgedu-redis1 ~]# redis-cli -c -a Redis@123
127.0.0.1:6379> set fgedu_key “Hello Redis Cluster”
OK
127.0.0.1:6379> get fgedu_key
“Hello Redis Cluster”
127.0.0.1:6379> exit
Part04-数据库监控
4.1 Prometheus+Grafana监控
[root@fgedu-db1 ~]# docker run -d –name mysql-exporter \
-e DATA_SOURCE_NAME=”exporter:Exporter@123@(localhost:3306)/” \
-p 9104:9104 \
prom/mysqld-exporter:v0.15.0
# 部署Redis Exporter
[root@fgedu-redis1 ~]# docker run -d –name redis-exporter \
-e REDIS_ADDR=”redis://localhost:6379″ \
-e REDIS_PASSWORD=”Redis@123″ \
-p 9121:9121 \
oliver006/redis_exporter:v1.55.0
# 配置Prometheus
[root@fgedu-prometheus ~]# cat >> /etc/prometheus/prometheus.yml << 'EOF'
- job_name: 'mysql'
static_configs:
- targets: ['192.168.1.10:9104', '192.168.1.11:9104', '192.168.1.12:9104']
- job_name: 'redis'
static_configs:
- targets: ['192.168.1.20:9121', '192.168.1.21:9121', '192.168.1.22:9121']
EOF
[root@fgedu-prometheus ~]# systemctl restart prometheus
# 创建监控告警规则
[root@fgedu-prometheus ~]# cat > /etc/prometheus/rules/database.yml << 'EOF'
groups:
- name: mysql_alerts
rules:
- alert: MySQLDown
expr: mysql_up == 0
for: 1m
labels:
severity: critical
annotations:
summary: "MySQL实例宕机"
description: "MySQL实例 {{ $labels.instance }} 已宕机超过1分钟"
- alert: MySQLReplicationLag
expr: mysql_slave_status_seconds_behind_master > 30
for: 5m
labels:
severity: warning
annotations:
summary: “MySQL复制延迟”
description: “MySQL从库 {{ $labels.instance }} 复制延迟超过30秒”
– name: redis_alerts
rules:
– alert: RedisDown
expr: redis_up == 0
for: 1m
labels:
severity: critical
annotations:
summary: “Redis实例宕机”
description: “Redis实例 {{ $labels.instance }} 已宕机超过1分钟”
– alert: RedisMemoryHigh
expr: redis_memory_used_bytes / redis_memory_max_bytes * 100 > 80
for: 5m
labels:
severity: warning
annotations:
summary: “Redis内存使用过高”
description: “Redis {{ $labels.instance }} 内存使用率超过80%”
EOF
- 配置主从复制和自动故障切换
- 实施读写分离提升性能
- 使用Redis Cluster实现缓存高可用
- 配置全面的监控告警
- 定期进行故障演练
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
