Cassandra教程FG011-Cassandra日常维护实战
本文档风哥主要介绍Cassandra数据库日常维护实战,包括日常维护概述、维护任务分类、维护策略、维护计划制定、维护工具介绍、维护监控体系、日常维护操作实战、周期性维护实战、自动化维护脚本、压缩维护实战、修复维护实战、清理维护实战等内容,风哥教程参考Cassandra官方文档Operations内容编写,适合DBA人员和开发人员在学习和测试中使用,如果要应用于生产环境则需要自行确认。
Part01-基础概念与理论知识
1.1 Cassandra数据库日常维护概述
Cassandra数据库日常维护是保证数据库稳定运行的关键,包括节点管理、数据修复、压缩管理、日志清理等任务。更多视频教程www.fgedu.net.cn
1.1.1 Cassandra数据库维护重要性
# 1. 保证数据一致性
– 定期运行repair保证副本一致
– 处理数据不一致问题
– 防止数据丢失
# 2. 优化性能
– 压缩SSTable减少空间占用
– 清理墓碑释放空间
– 优化读写性能
# 3. 保证可用性
– 监控节点状态
– 处理节点故障
– 平衡集群负载
# 4. 预防故障
– 定期检查日志
– 监控资源使用
– 及时处理告警
# 5. 合规要求
– 数据保留策略
– 审计日志管理
– 安全配置检查
# 维护任务分类
# 日常维护(每天)
– 节点状态检查
– 日志检查
– 性能监控
# 周期维护(每周/每月)
– 数据修复
– 压缩管理
– 备份验证
# 季度维护
– 版本升级评估
– 容量规划
– 安全审计
1.1.2 Cassandra数据库维护目标
# 1. 高可用性
目标: 99.99%可用性
措施:
– 节点监控
– 故障快速恢复
– 负载均衡
# 2. 数据一致性
目标: 数据最终一致
措施:
– 定期repair
– 一致性验证
– 冲突解决
# 3. 性能优化
目标: 读写延迟<10ms
措施:
- 压缩优化
- 缓存调优
- 查询优化
# 4. 资源管理
目标: 资源利用率<80%
措施:
- 容量监控
- 数据清理
- 扩容规划
# 5. 安全合规
目标: 满足安全要求
措施:
- 访问控制
- 加密配置
- 审计日志
# 维护KPI指标
# 可用性指标
- 节点在线率: >99.99%
– 集群可用性: >99.99%
# 性能指标
– 读写延迟: <10ms
- 吞吐量: >10000 ops/s
# 一致性指标
– repair完成率: 100%
– 数据一致性: 100%
# 资源指标
– CPU利用率: <70%
- 内存利用率: <80%
- 磁盘利用率: <80%
1.2 Cassandra数据库维护任务分类
Cassandra数据库维护任务分类详解:
1.2.1 Cassandra数据库节点维护任务
# 1. 节点状态监控
# 检查节点状态
nodetool status
# 检查节点信息
nodetool info
# 检查节点运行状态
nodetool netstats
# 2. 节点资源监控
# 检查内存使用
nodetool info | grep -E “Heap|Memory”
# 检查磁盘使用
df -h /cassandra/fgdata
# 检查CPU使用
top -p $(pgrep -f cassandra)
# 3. 节点配置管理
# 检查配置文件
cat /cassandra/app/current/conf/cassandra.yaml
# 检查JVM配置
cat /cassandra/app/current/conf/jvm.options
# 检查日志配置
cat /cassandra/app/current/conf/logback.xml
# 4. 节点服务管理
# 启动节点
/cassandra/app/current/bin/cassandra
# 停止节点
nodetool drain
kill $(cat /cassandra/app/cassandra.pid)
# 重启节点
nodetool drain
/cassandra/app/current/bin/cassandra
# 5. 节点故障处理
# 检查节点日志
tail -100 /cassandra/logs/system.log
# 检查GC日志
tail -100 /cassandra/logs/gc.log
# 检查错误日志
grep -i error /cassandra/logs/system.log
1.2.2 Cassandra数据库数据维护任务
# 1. 数据修复
# 主范围修复(推荐)
nodetool repair -pr
# 全量修复
nodetool repair
# 并行修复
nodetool repair -par
# 2. 数据压缩
# 查看压缩状态
nodetool compactionstats
# 手动触发压缩
nodetool compact
# 修改压缩策略
ALTER TABLE fgedu_users WITH compaction = {‘class’: ‘SizeTieredCompactionStrategy’};
# 3. 数据清理
# 清理墓碑
nodetool garbagecollect
# 清理旧数据
nodetool cleanup
# 清理快照
nodetool clearsnapshot
# 4. 数据验证
# 验证数据
nodetool verify fgedudb fgedu_users
# 验证校验和
nodetool verify -e fgedudb fgedu_users
# 验证数据一致性
nodetool scrub fgedudb fgedu_users
# 5. 数据统计
# 表统计
nodetool tablestats fgedudb
# 表直方图
nodetool tablehistograms fgedudb fgedu_users
# 数据分布
nodetool describering fgedudb
1.3 Cassandra数据库维护策略
Cassandra数据库维护策略详解:
1.3.1 Cassandra数据库维护策略制定
# 1. 预防性维护
# 目标: 预防问题发生
# 措施:
– 定期监控
– 主动巡检
– 容量规划
– 性能优化
# 执行频率: 每天
# 2. 纠正性维护
# 目标: 修复已发生问题
# 措施:
– 故障诊断
– 问题修复
– 数据恢复
– 配置调整
# 执行频率: 按需
# 3. 适应性维护
# 目标: 适应环境变化
# 措施:
– 版本升级
– 参数调整
– 架构优化
– 扩容缩容
# 执行频率: 季度
# 4. 完善性维护
# 目标: 改进系统性能
# 措施:
– 性能优化
– 功能增强
– 自动化改进
– 文档完善
# 执行频率: 月度
# 维护策略选择原则
1. 优先预防性维护
2. 快速响应纠正性维护
3. 定期评估适应性维护
4. 持续改进完善性维护
1.3.2 Cassandra数据库维护窗口规划
# 1. 维护窗口类型
# 日常维护窗口
– 时间: 每天凌晨2:00-4:00
– 任务: 日志清理、监控检查
– 影响: 无
# 周期维护窗口
– 时间: 每周日凌晨3:00-6:00
– 任务: repair、压缩
– 影响: 轻微
# 大型维护窗口
– 时间: 每月第一个周日凌晨2:00-8:00
– 任务: 版本升级、架构调整
– 影响: 中等
# 2. 维护窗口选择原则
– 选择业务低峰期
– 避免关键业务时段
– 预留足够时间
– 准备回滚方案
# 3. 维护窗口通知
# 维护前通知
– 提前3天发送通知
– 说明维护内容和影响
– 提供联系方式
# 维护中通知
– 实时更新维护进度
– 及时通报异常情况
# 维护后通知
– 确认维护完成
– 总结维护结果
# 4. 维护窗口监控
# 维护前
– 确认系统状态正常
– 备份关键数据
– 准备回滚方案
# 维护中
– 监控系统状态
– 记录操作日志
– 及时处理异常
# 维护后
– 验证系统功能
– 检查性能指标
– 更新维护记录
Part02-生产环境规划与建议
2.1 Cassandra数据库维护计划制定
Cassandra数据库维护计划制定详解:
2.1.1 Cassandra数据库日维护计划
# 时间: 每天凌晨2:00-4:00
# 任务列表:
# 1. 节点状态检查(2:00-2:15)
nodetool status
nodetool info
nodetool netstats
# 2. 日志检查(2:15-2:30)
grep -i error /cassandra/logs/system.log
grep -i warn /cassandra/logs/system.log
tail -100 /cassandra/logs/system.log
# 3. 性能监控(2:30-2:45)
nodetool tablestats
nodetool tpstats
nodetool proxyhistograms
# 4. 资源监控(2:45-3:00)
df -h /cassandra/fgdata
free -m
iostat -x 1 5
# 5. GC监控(3:00-3:15)
tail -100 /cassandra/logs/gc.log
jstat -gc $(pgrep -f cassandra) 1 5
# 6. 连接监控(3:15-3:30)
netstat -an | grep 9042
netstat -an | grep 7000
# 7. 告警检查(3:30-3:45)
# 检查告警系统
# 处理未解决告警
# 8. 维护记录(3:45-4:00)
# 更新维护日志
# 记录异常情况
# 日维护脚本
#!/bin/bash
# daily_maintenance.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
LOG_FILE=”/cassandra/logs/maintenance/daily_$(date +%Y%m%d).log”
mkdir -p /cassandra/logs/maintenance
echo “=== Cassandra日维护开始 ===” | tee -a ${LOG_FILE}
echo “时间: $(date)” | tee -a ${LOG_FILE}
# 节点状态检查
echo “1. 节点状态检查” | tee -a ${LOG_FILE}
nodetool status >> ${LOG_FILE} 2>&1
# 日志检查
echo “2. 日志检查” | tee -a ${LOG_FILE}
grep -i error /cassandra/logs/system.log >> ${LOG_FILE} 2>&1
# 性能监控
echo “3. 性能监控” | tee -a ${LOG_FILE}
nodetool tablestats >> ${LOG_FILE} 2>&1
# 资源监控
echo “4. 资源监控” | tee -a ${LOG_FILE}
df -h /cassandra/fgdata >> ${LOG_FILE} 2>&1
echo “=== Cassandra日维护完成 ===” | tee -a ${LOG_FILE}
2.1.2 Cassandra数据库周维护计划
# 时间: 每周日凌晨3:00-6:00
# 任务列表:
# 1. 数据修复(3:00-4:30)
nodetool repair -pr
# 2. 压缩管理(4:30-5:00)
nodetool compactionstats
nodetool compact
# 3. 快照管理(5:00-5:30)
nodetool snapshot
nodetool clearsnapshot -t old_snapshot
# 4. 墓碑清理(5:30-5:45)
nodetool garbagecollect
# 5. 性能分析(5:45-6:00)
nodetool proxyhistograms
nodetool tablehistograms
# 周维护脚本
#!/bin/bash
# weekly_maintenance.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
LOG_FILE=”/cassandra/logs/maintenance/weekly_$(date +%Y%m%d).log”
echo “=== Cassandra周维护开始 ===” | tee -a ${LOG_FILE}
echo “时间: $(date)” | tee -a ${LOG_FILE}
# 数据修复
echo “1. 数据修复” | tee -a ${LOG_FILE}
nodetool repair -pr >> ${LOG_FILE} 2>&1
# 压缩管理
echo “2. 压缩管理” | tee -a ${LOG_FILE}
nodetool compactionstats >> ${LOG_FILE} 2>&1
# 快照管理
echo “3. 快照管理” | tee -a ${LOG_FILE}
nodetool snapshot -t weekly_$(date +%Y%m%d) >> ${LOG_FILE} 2>&1
# 墓碑清理
echo “4. 墓碑清理” | tee -a ${LOG_FILE}
nodetool garbagecollect >> ${LOG_FILE} 2>&1
echo “=== Cassandra周维护完成 ===” | tee -a ${LOG_FILE}
2.2 Cassandra数据库维护工具介绍
Cassandra数据库维护工具介绍:
2.2.1 Cassandra数据库nodetool工具详解
# 1. 节点管理命令
# 查看集群状态
nodetool status
# 输出示例
Datacenter: dc1
===============
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
— Address Load Tokens Owns (effective) Host ID Rack
UN 192.168.1.101 100 GB 256 100.0% a1b2c3d4… RAC1
UN 192.168.1.102 100 GB 256 100.0% e5f6g7h8… RAC1
UN 192.168.1.103 100 GB 256 100.0% i9j0k1l2… RAC2
# 查看节点信息
nodetool info
# 输出示例
ID : a1b2c3d4-e5f6-7890-abcd-ef1234567890
Gossip active : true
Thrift active : false
Native Transport active: true
Load : 100 GB
Generation No : 1234567890
Uptime (seconds) : 864000
Heap Memory (MB) : 4096.00 / 8192.00
Off Heap Memory (MB) : 512.00
Data Center : dc1
Rack : RAC1
Exceptions : 0
Key Cache : entries 100000, size 100 MB, capacity 200 MB
Row Cache : entries 0, size 0, capacity 0
# 2. 数据管理命令
# 数据修复
nodetool repair -pr
# 输出示例
[2024-01-15 03:00:00,000] Starting repair…
[2024-01-15 03:30:00,000] Repair completed successfully
# 数据压缩
nodetool compact
# 输出示例
Compaction requested
# 数据清理
nodetool cleanup
# 输出示例
Cleanup started
# 墓碑清理
nodetool garbagecollect
# 输出示例
Garbage collection started
# 3. 监控命令
# 查看压缩状态
nodetool compactionstats
# 输出示例
pending tasks: 0
compaction type keyspace table completed total unit progress
n/a n/a n/a 0 0 bytes n/a
# 查看表统计
nodetool tablestats fgedudb
# 输出示例
Total number of tables: 10
Keyspace : fgedudb
Read Count: 100000
Read Latency: 5.23 ms
Write Count: 500000
Write Latency: 2.15 ms
Pending Flushes: 0
# 查看线程池状态
nodetool tpstats
# 输出示例
Pool Name Active Pending Completed Blocked All time blocked
ReadStage 0 0 10000000 0 0
MutationStage 0 0 50000000 0 0
RequestResponseStage 0 0 20000000 0 0
2.2.2 Cassandra数据库其他维护工具
# 1. cqlsh工具
# 连接数据库
cqlsh 192.168.1.101 9042 -u fgedu -p Fgedu@2024
# 执行查询
cqlsh> SELECT * FROM system.local;
# 执行脚本
cqlsh -f script.cql
# 导出数据
cqlsh -e “COPY fgedudb.fgedu_users TO ‘/tmp/users.csv'”
# 导入数据
cqlsh -e “COPY fgedudb.fgedu_users FROM ‘/tmp/users.csv'”
# 2. cassandra-stress工具
# 写入测试
cassandra-stress write n=100000 -node 192.168.1.101
# 读取测试
cassandra-stress read n=100000 -node 192.168.1.101
# 混合测试
cassandra-stress mixed ratio(write=1,read=1) n=100000 -node 192.168.1.101
# 3. sstable工具
# 查看SSTable信息
sstablemetadata /cassandra/fgdata/fgedudb/fgedu_users-abc123
# 验证SSTable
sstableverify /cassandra/fgdata/fgedudb/fgedu_users-abc123
# 重建二级索引
sstablelevelreset /cassandra/fgdata/fgedudb/fgedu_users
# 4. JMX工具
# 使用jconsole连接
jconsole service:jmx:rmi:///jndi/rmi://192.168.1.101:7199/jmxrmi
# 使用jstat监控GC
jstat -gc $(pgrep -f cassandra) 1 10
# 使用jmap查看堆内存
jmap -heap $(pgrep -f cassandra)
# 5. 系统监控工具
# CPU监控
top -p $(pgrep -f cassandra)
# 内存监控
free -m
# 磁盘监控
iostat -x 1 10
# 网络监控
netstat -an | grep 9042
2.3 Cassandra数据库维护监控体系
Cassandra数据库维护监控体系详解:
2.3.1 Cassandra数据库监控指标
# 1. 节点监控指标
# 节点状态
– 节点在线状态
– 节点负载
– 节点延迟
# 资源使用
– CPU使用率
– 内存使用率
– 磁盘使用率
– 网络流量
# 2. 性能监控指标
# 读写性能
– 读写延迟
– 读写吞吐量
– 读写错误率
# 压缩性能
– 压缩队列长度
– 压缩速度
– 压缩比率
# 3. 一致性监控指标
# 数据修复
– repair进度
– repair时间
– repair错误
# 数据同步
– 数据同步状态
– Hint队列长度
– 数据差异
# 4. JVM监控指标
# 堆内存
– 堆内存使用
– GC频率
– GC时间
# 线程
– 线程数量
– 线程状态
– 线程阻塞
# 5. 业务监控指标
# 连接数
– 客户端连接数
– 连接错误数
# 请求量
– 请求总数
– 请求成功率
– 请求延迟分布
# 监控指标采集脚本
#!/bin/bash
# collect_metrics.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
METRICS_FILE=”/cassandra/logs/metrics/metrics_$(date +%Y%m%d%H%M).txt”
mkdir -p /cassandra/logs/metrics
echo “=== Cassandra监控指标采集 ===” > ${METRICS_FILE}
echo “时间: $(date)” >> ${METRICS_FILE}
# 节点状态
echo “1. 节点状态” >> ${METRICS_FILE}
nodetool status >> ${METRICS_FILE} 2>&1
# 性能指标
echo “2. 性能指标” >> ${METRICS_FILE}
nodetool tablestats >> ${METRICS_FILE} 2>&1
# JVM指标
echo “3. JVM指标” >> ${METRICS_FILE}
jstat -gc $(pgrep -f cassandra) >> ${METRICS_FILE} 2>&1
# 资源指标
echo “4. 资源指标” >> ${METRICS_FILE}
df -h /cassandra/fgdata >> ${METRICS_FILE} 2>&1
free -m >> ${METRICS_FILE} 2>&1
echo “监控指标采集完成”
Part03-生产环境项目实施方案
3.1 Cassandra数据库日常维护操作实战
Cassandra数据库日常维护操作实战:
3.1.1 Cassandra数据库节点状态检查
# 1. 检查集群状态
# nodetool status
Datacenter: dc1
===============
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
— Address Load Tokens Owns (effective) Host ID Rack
UN 192.168.1.101 100 GB 256 100.0% a1b2c3d4… RAC1
UN 192.168.1.102 100 GB 256 100.0% e5f6g7h8… RAC1
UN 192.168.1.103 100 GB 256 100.0% i9j0k1l2… RAC2
# 状态说明
# U – Up (节点在线)
# D – Down (节点离线)
# N – Normal (正常状态)
# L – Leaving (正在离开)
# J – Joining (正在加入)
# M – Moving (正在移动)
# 2. 检查节点详细信息
# nodetool info
ID : a1b2c3d4-e5f6-7890-abcd-ef1234567890
Gossip active : true
Thrift active : false
Native Transport active: true
Load : 100 GB
Generation No : 1234567890
Uptime (seconds) : 864000
Heap Memory (MB) : 4096.00 / 8192.00
Off Heap Memory (MB) : 512.00
Data Center : dc1
Rack : RAC1
Exceptions : 0
# 3. 检查节点网络状态
# nodetool netstats
Mode: NORMAL
Not sending any streams.
Not receiving any streams.
Owns (or will own) 100.0% of the ring
# 4. 检查节点运行时间
# nodetool info | grep Uptime
Uptime (seconds) : 864000
# 5. 检查节点异常
# nodetool info | grep Exceptions
Exceptions : 0
# 6. 检查节点负载
# nodetool info | grep Load
Load : 100 GB
3.1.2 Cassandra数据库日志检查
# 1. 检查系统日志错误
# grep -i error /cassandra/logs/system.log | tail -20
ERROR [ReadStage-1] 2024-01-15 10:30:00,000 CassandraDaemon.java:200 – Exception in thread ReadStage-1
ERROR [MutationStage-1] 2024-01-15 10:31:00,000 CassandraDaemon.java:200 – Exception in thread MutationStage-1
# 2. 检查系统日志警告
# grep -i warn /cassandra/logs/system.log | tail -20
WARN [GossipStage:1] 2024-01-15 10:30:00,000 Gossiper.java:500 – FatClient 192.168.1.100 has been silent for 30000ms, removing from gossip
WARN [CompactionExecutor:1] 2024-01-15 10:31:00,000 CompactionTask.java:200 – Too many tombstones detected
# 3. 检查GC日志
# tail -50 /cassandra/logs/gc.log
[2024-01-15T10:30:00.000+0800][gc,info] GC(1000) Pause Young (Allocation Failure) 100M->50M(200M) 5.000ms
[2024-01-15T10:31:00.000+0800][gc,info] GC(1001) Pause Young (Allocation Failure) 150M->80M(200M) 8.000ms
# 4. 检查慢查询日志
# grep -i slow /cassandra/logs/system.log | tail -20
# 5. 检查连接日志
# grep -i connection /cassandra/logs/system.log | tail -20
# 6. 检查压缩日志
# grep -i compaction /cassandra/logs/system.log | tail -20
INFO [CompactionExecutor:1] 2024-01-15 10:30:00,000 CompactionTask.java:100 – Compacting fgedu_users
INFO [CompactionExecutor:1] 2024-01-15 10:31:00,000 CompactionTask.java:200 – Compacted fgedu_users
# 7. 日志分析脚本
#!/bin/bash
# log_analysis.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
LOG_FILE=”/cassandra/logs/system.log”
REPORT_FILE=”/cassandra/logs/log_report_$(date +%Y%m%d).txt”
echo “=== Cassandra日志分析报告 ===” > ${REPORT_FILE}
echo “分析时间: $(date)” >> ${REPORT_FILE}
echo “” >> ${REPORT_FILE}
# 错误统计
echo “1. 错误统计” >> ${REPORT_FILE}
grep -i error ${LOG_FILE} | wc -l >> ${REPORT_FILE}
echo “” >> ${REPORT_FILE}
# 警告统计
echo “2. 警告统计” >> ${REPORT_FILE}
grep -i warn ${LOG_FILE} | wc -l >> ${REPORT_FILE}
echo “” >> ${REPORT_FILE}
# 最近错误
echo “3. 最近10条错误” >> ${REPORT_FILE}
grep -i error ${LOG_FILE} | tail -10 >> ${REPORT_FILE}
echo “” >> ${REPORT_FILE}
# 最近警告
echo “4. 最近10条警告” >> ${REPORT_FILE}
grep -i warn ${LOG_FILE} | tail -10 >> ${REPORT_FILE}
echo “日志分析完成,报告已保存到 ${REPORT_FILE}”
3.2 Cassandra数据库周期性维护实战
Cassandra数据库周期性维护实战:
3.2.1 Cassandra数据库数据修复实战
# 1. 主范围修复(推荐)
# nodetool repair -pr
[2024-01-15 03:00:00,000] Starting repair…
[2024-01-15 03:00:05,000] Repair session 1 started
[2024-01-15 03:00:10,000] Repairing range (-1234567890,1234567890]
[2024-01-15 03:30:00,000] Repair completed successfully
# 2. 全量修复
# nodetool repair
[2024-01-15 03:00:00,000] Starting repair…
[2024-01-15 03:00:05,000] Repair session 1 started
[2024-01-15 03:00:10,000] Repairing range (-1234567890,1234567890]
[2024-01-15 04:00:00,000] Repair completed successfully
# 3. 指定Keyspace修复
# nodetool repair -pr fgedudb
[2024-01-15 03:00:00,000] Starting repair on keyspace fgedudb…
[2024-01-15 03:30:00,000] Repair completed successfully
# 4. 指定表修复
# nodetool repair -pr fgedudb fgedu_users
[2024-01-15 03:00:00,000] Starting repair on table fgedudb.fgedu_users…
[2024-01-15 03:15:00,000] Repair completed successfully
# 5. 并行修复
# nodetool repair -par
[2024-01-15 03:00:00,000] Starting parallel repair…
[2024-01-15 03:00:05,000] Repair session 1,2,3 started in parallel
[2024-01-15 03:30:00,000] Repair completed successfully
# 6. 修复进度监控
# nodetool repair -pr 2>&1 | tee /cassandra/logs/repair_$(date +%Y%m%d).log
# 7. 修复脚本
#!/bin/bash
# repair_maintenance.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
LOG_FILE=”/cassandra/logs/maintenance/repair_$(date +%Y%m%d).log”
echo “=== Cassandra数据修复开始 ===” | tee -a ${LOG_FILE}
echo “时间: $(date)” | tee -a ${LOG_FILE}
# 执行主范围修复
echo “执行主范围修复…” | tee -a ${LOG_FILE}
nodetool repair -pr 2>&1 | tee -a ${LOG_FILE}
# 检查修复结果
if [ $? -eq 0 ]; then
echo “修复成功” | tee -a ${LOG_FILE}
else
echo “修复失败” | tee -a ${LOG_FILE}
# 发送告警
echo “Cassandra修复失败,请检查” | mail -s “Cassandra修复告警” admin@fgedu.net.cn
fi
echo “=== Cassandra数据修复完成 ===” | tee -a ${LOG_FILE}
3.2.2 Cassandra数据库压缩管理实战
# 1. 查看压缩状态
# nodetool compactionstats
pending tasks: 2
compaction type keyspace table completed total unit progress
Minor fgedudb fgedu_users 50000000 100000000 bytes 50.00%
Major fgedudb fgedu_orders 0 200000000 bytes 0.00%
# 2. 手动触发压缩
# nodetool compact fgedudb fgedu_users
Compaction requested
# 3. 主压缩(全量压缩)
# nodetool compact
Compaction requested
# 4. 墓碑压缩
# nodetool garbagecollect fgedudb fgedu_users
Garbage collection started
# 5. 查看压缩历史
# nodetool compactionhistory
id keyspace table completed_at bytes_in bytes_out
12345678-1234-1234-1234-123456789012 fgedudb fgedu_users 2024-01-15T10:30:00Z 100000000 80000000
# 6. 修改压缩策略
# cqlsh -e “ALTER TABLE fgedudb.fgedu_users
WITH compaction = {‘class’: ‘LeveledCompactionStrategy’,
‘sstable_size_in_mb’: 160};”
# 7. 压缩监控脚本
#!/bin/bash
# compaction_monitor.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
LOG_FILE=”/cassandra/logs/maintenance/compaction_$(date +%Y%m%d).log”
echo “=== Cassandra压缩监控 ===” | tee -a ${LOG_FILE}
echo “时间: $(date)” | tee -a ${LOG_FILE}
# 查看压缩状态
echo “压缩状态:” | tee -a ${LOG_FILE}
nodetool compactionstats >> ${LOG_FILE} 2>&1
# 查看压缩历史
echo “压缩历史:” | tee -a ${LOG_FILE}
nodetool compactionhistory >> ${LOG_FILE} 2>&1
# 检查待压缩任务
PENDING=$(nodetool compactionstats | grep “pending tasks” | awk ‘{print $3}’)
if [ “${PENDING}” -gt 10 ]; then
echo “警告: 待压缩任务过多 (${PENDING})” | tee -a ${LOG_FILE}
fi
echo “压缩监控完成” | tee -a ${LOG_FILE}
3.3 Cassandra数据库自动化维护脚本
Cassandra数据库自动化维护脚本:
3.3.1 Cassandra数据库综合维护脚本
# comprehensive_maintenance.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# Cassandra综合维护脚本
# 配置变量
CASSANDRA_HOME=”/cassandra/app/current”
LOG_DIR=”/cassandra/logs/maintenance”
DATE=$(date +%Y%m%d)
LOG_FILE=”${LOG_DIR}/maintenance_${DATE}.log”
# 创建日志目录
mkdir -p ${LOG_DIR}
# 日志函数
log() {
echo “[$(date ‘+%Y-%m-%d %H:%M:%S’)] $1″ | tee -a ${LOG_FILE}
}
# 发送告警
send_alert() {
local subject=”Cassandra维护告警: $1″
local message=”$2”
echo “${message}” | mail -s “${subject}” admin@fgedu.net.cn
}
# 主函数
main() {
log “=== Cassandra综合维护开始 ===”
# 1. 节点状态检查
log “1. 节点状态检查”
nodetool status >> ${LOG_FILE} 2>&1
# 检查节点是否全部在线
DOWN_NODES=$(nodetool status | grep -c “^DN”)
if [ “${DOWN_NODES}” -gt 0 ]; then
log “警告: 发现 ${DOWN_NODES} 个节点离线”
send_alert “节点离线” “发现 ${DOWN_NODES} 个节点离线”
fi
# 2. 资源检查
log “2. 资源检查”
# 磁盘使用检查
DISK_USAGE=$(df -h /cassandra/fgdata | tail -1 | awk ‘{print $5}’ | sed ‘s/%//’)
log “磁盘使用率: ${DISK_USAGE}%”
if [ “${DISK_USAGE}” -gt 80 ]; then
log “警告: 磁盘使用率过高”
send_alert “磁盘空间告警” “磁盘使用率 ${DISK_USAGE}%”
fi
# 内存使用检查
MEMORY_USAGE=$(free | grep Mem | awk ‘{printf “%.0f”, $3/$2 * 100}’)
log “内存使用率: ${MEMORY_USAGE}%”
if [ “${MEMORY_USAGE}” -gt 90 ]; then
log “警告: 内存使用率过高”
send_alert “内存告警” “内存使用率 ${MEMORY_USAGE}%”
fi
# 3. 性能检查
log “3. 性能检查”
nodetool tablestats >> ${LOG_FILE} 2>&1
nodetool tpstats >> ${LOG_FILE} 2>&1
# 4. 日志检查
log “4. 日志检查”
ERROR_COUNT=$(grep -c -i error /cassandra/logs/system.log)
log “错误数量: ${ERROR_COUNT}”
if [ “${ERROR_COUNT}” -gt 100 ]; then
log “警告: 错误数量过多”
send_alert “错误告警” “系统日志错误数量 ${ERROR_COUNT}”
fi
# 5. GC检查
log “5. GC检查”
jstat -gc $(pgrep -f cassandra) >> ${LOG_FILE} 2>&1
# 6. 压缩检查
log “6. 压缩检查”
nodetool compactionstats >> ${LOG_FILE} 2>&1
# 7. 数据修复(仅在周日执行)
if [ $(date +%u) -eq 7 ]; then
log “7. 数据修复(周日任务)”
nodetool repair -pr >> ${LOG_FILE} 2>&1
if [ $? -eq 0 ]; then
log “数据修复成功”
else
log “数据修复失败”
send_alert “修复失败” “数据修复失败,请检查”
fi
fi
log “=== Cassandra综合维护完成 ===”
}
# 执行主函数
main
Part04-生产案例与实战讲解
4.1 Cassandra数据库压缩维护实战
Cassandra数据库压缩维护实战案例:
4.1.1 Cassandra数据库压缩策略选择
# 1. SizeTieredCompactionStrategy (STCS)
# 特点: 适合写入密集型场景
# 优点: 写入性能好
# 缺点: 读放大较高
# 适用场景
– 日志数据
– 时序数据
– 写入密集型应用
# 配置示例
ALTER TABLE fgedu_logs
WITH compaction = {
‘class’: ‘SizeTieredCompactionStrategy’,
‘min_threshold’: 4,
‘max_threshold’: 32
};
# 2. LeveledCompactionStrategy (LCS)
# 特点: 适合读取密集型场景
# 优点: 读取性能好,空间效率高
# 缺点: 写入放大较高
# 适用场景
– 用户数据
– 订单数据
– 读取密集型应用
# 配置示例
ALTER TABLE fgedu_users
WITH compaction = {
‘class’: ‘LeveledCompactionStrategy’,
‘sstable_size_in_mb’: 160
};
# 3. DateTieredCompactionStrategy (DTCS)
# 特点: 适合时序数据
# 优点: 时序查询性能好
# 缺点: 需要配置TTL
# 适用场景
– IoT数据
– 监控数据
– 时序数据
# 配置示例
ALTER TABLE fgedu_metrics
WITH compaction = {
‘class’: ‘DateTieredCompactionStrategy’,
‘base_time_seconds’: 3600,
‘max_sstable_age_days’: 365
};
# 4. TimeWindowCompactionStrategy (TWCS)
# 特点: DTCS的改进版本
# 优点: 更好的时序数据处理
# 缺点: 需要配置时间窗口
# 适用场景
– IoT数据
– 监控数据
– 时序数据
# 配置示例
ALTER TABLE fgedu_timeseries
WITH compaction = {
‘class’: ‘TimeWindowCompactionStrategy’,
‘compaction_window_unit’: ‘HOURS’,
‘compaction_window_size’: 1
};
4.1.2 Cassandra数据库压缩实战操作
# 1. 查看当前压缩策略
# cqlsh -e “DESCRIBE TABLE fgedudb.fgedu_users;” | grep compaction
compaction = {‘class’: ‘SizeTieredCompactionStrategy’}
# 2. 修改压缩策略
# cqlsh -e “ALTER TABLE fgedudb.fgedu_users
WITH compaction = {‘class’: ‘LeveledCompactionStrategy’,
‘sstable_size_in_mb’: 160};”
# 3. 查看压缩状态
# nodetool compactionstats
pending tasks: 1
compaction type keyspace table completed total unit progress
Major fgedudb fgedu_users 0 100000000 bytes 0.00%
# 4. 手动触发压缩
# nodetool compact fgedudb fgedu_users
Compaction requested
# 5. 监控压缩进度
# nodetool compactionstats
pending tasks: 1
compaction type keyspace table completed total unit progress
Major fgedudb fgedu_users 50000000 100000000 bytes 50.00%
# 6. 压缩完成后验证
# nodetool compactionstats
pending tasks: 0
# 7. 查看压缩效果
# nodetool tablestats fgedudb fgedu_users
Table: fgedu_users
SSTable count: 5
Space used (live): 80000000
Space used (total): 80000000
Compacted partition minimum bytes: 100
Compacted partition maximum bytes: 10000
# 8. 压缩维护脚本
#!/bin/bash
# compaction_maintenance.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
KEYSPACE=”fgedudb”
TABLE=”fgedu_users”
LOG_FILE=”/cassandra/logs/maintenance/compaction_$(date +%Y%m%d).log”
echo “=== 压缩维护开始 ===” | tee -a ${LOG_FILE}
# 检查压缩状态
echo “检查压缩状态…” | tee -a ${LOG_FILE}
PENDING=$(nodetool compactionstats | grep “pending tasks” | awk ‘{print $3}’)
echo “待压缩任务数: ${PENDING}” | tee -a ${LOG_FILE}
# 如果待压缩任务过多,触发手动压缩
if [ “${PENDING}” -gt 10 ]; then
echo “待压缩任务过多,触发手动压缩…” | tee -a ${LOG_FILE}
nodetool compact ${KEYSPACE} ${TABLE} 2>&1 | tee -a ${LOG_FILE}
fi
# 检查墓碑比例
echo “检查墓碑比例…” | tee -a ${LOG_FILE}
nodetool tablehistograms ${KEYSPACE} ${TABLE} | grep Tombstones >> ${LOG_FILE} 2>&1
# 如果墓碑过多,执行墓碑清理
# nodetool garbagecollect ${KEYSPACE} ${TABLE}
echo “=== 压缩维护完成 ===” | tee -a ${LOG_FILE}
4.2 Cassandra数据库修复维护实战
Cassandra数据库修复维护实战案例:
4.2.1 Cassandra数据库修复策略
# 1. 主范围修复 (Primary Range Repair)
# 特点: 只修复本节点负责的主范围
# 优点: 效率高,资源消耗低
# 推荐: 日常维护使用
nodetool repair -pr
# 2. 全量修复
# 特点: 修复所有数据范围
# 优点: 保证数据完全一致
# 缺点: 资源消耗高
# 推荐: 数据不一致时使用
nodetool repair
# 3. 增量修复
# 特点: 只修复有变化的数据
# 优点: 效率高
# 推荐: 频繁修复场景
nodetool repair -inc
# 4. 并行修复
# 特点: 并行执行多个修复任务
# 优点: 速度快
# 缺点: 资源消耗高
# 推荐: 大集群使用
nodetool repair -par
# 5. 顺序修复
# 特点: 顺序执行修复任务
# 优点: 资源消耗低
# 缺点: 速度慢
# 推荐: 资源紧张时使用
nodetool repair -seq
# 修复频率建议
# 小集群 (<10节点): 每周一次
# 中等集群 (10-50节点): 每周一次主范围修复
# 大集群 (>50节点): 每天一次主范围修复
4.2.2 Cassandra数据库修复实战操作
# 1. 检查数据一致性
# 在不同节点查询同一数据
# cqlsh 192.168.1.101 -e “SELECT * FROM fgedudb.fgedu_users WHERE user_id = ?”
# cqlsh 192.168.1.102 -e “SELECT * FROM fgedudb.fgedu_users WHERE user_id = ?”
# 2. 执行主范围修复
# nodetool repair -pr
[2024-01-15 03:00:00,000] Starting repair…
[2024-01-15 03:00:05,000] Repair session 1 started
[2024-01-15 03:00:10,000] Repairing range (-1234567890,1234567890]
[2024-01-15 03:30:00,000] Repair completed successfully
# 3. 监控修复进度
# nodetool repair -pr 2>&1 | tee /cassandra/logs/repair.log
# 4. 验证修复结果
# cqlsh 192.168.1.101 -e “SELECT * FROM fgedudb.fgedu_users WHERE user_id = ?”
# cqlsh 192.168.1.102 -e “SELECT * FROM fgedudb.fgedu_users WHERE user_id = ?”
# 5. 修复脚本
#!/bin/bash
# repair_maintenance.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
LOG_FILE=”/cassandra/logs/maintenance/repair_$(date +%Y%m%d).log”
KEYSPACE=”fgedudb”
echo “=== 数据修复开始 ===” | tee -a ${LOG_FILE}
echo “时间: $(date)” | tee -a ${LOG_FILE}
# 检查节点状态
echo “检查节点状态…” | tee -a ${LOG_FILE}
nodetool status >> ${LOG_FILE} 2>&1
# 检查是否有节点离线
DOWN_NODES=$(nodetool status | grep -c “^DN”)
if [ “${DOWN_NODES}” -gt 0 ]; then
echo “错误: 有节点离线,无法执行修复” | tee -a ${LOG_FILE}
exit 1
fi
# 执行主范围修复
echo “执行主范围修复…” | tee -a ${LOG_FILE}
START_TIME=$(date +%s)
nodetool repair -pr ${KEYSPACE} 2>&1 | tee -a ${LOG_FILE}
END_TIME=$(date +%s)
# 计算修复时间
DURATION=$((END_TIME – START_TIME))
echo “修复耗时: ${DURATION} 秒” | tee -a ${LOG_FILE}
# 检查修复结果
if [ $? -eq 0 ]; then
echo “修复成功” | tee -a ${LOG_FILE}
else
echo “修复失败” | tee -a ${LOG_FILE}
# 发送告警
echo “Cassandra修复失败” | mail -s “Cassandra修复告警” admin@fgedu.net.cn
fi
echo “=== 数据修复完成 ===” | tee -a ${LOG_FILE}
4.3 Cassandra数据库清理维护实战
Cassandra数据库清理维护实战案例:
4.3.1 Cassandra数据库墓碑清理实战
# 1. 查看墓碑统计
# nodetool tablehistograms fgedudb fgedu_users
Percentile SSTables Tombstones
50% 0.00 0.00
75% 0.00 0.00
95% 0.00 10.00
98% 0.00 50.00
99% 0.00 100.00
min 0.00 0.00
max 0.00 1000.00
# 2. 执行墓碑清理
# nodetool garbagecollect fgedudb fgedu_users
Garbage collection started
# 3. 验证清理结果
# nodetool tablehistograms fgedudb fgedu_users
Percentile SSTables Tombstones
50% 0.00 0.00
75% 0.00 0.00
95% 0.00 0.00
98% 0.00 0.00
# 4. 清理旧数据
# nodetool cleanup fgedudb
Cleanup started
# 5. 清理快照
# 查看快照列表
nodetool listsnapshots
Snapshot name Keyspace Table Size
weekly_20240108 fgedudb fgedu_users 10 GB
weekly_20240101 fgedudb fgedu_users 10 GB
# 清理指定快照
nodetool clearsnapshot -t weekly_20240101
# 清理所有快照
nodetool clearsnapshot
# 6. 清理脚本
#!/bin/bash
# cleanup_maintenance.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
LOG_FILE=”/cassandra/logs/maintenance/cleanup_$(date +%Y%m%d).log”
echo “=== 数据清理开始 ===” | tee -a ${LOG_FILE}
# 检查墓碑比例
echo “检查墓碑比例…” | tee -a ${LOG_FILE}
TOMBSTONES=$(nodetool tablehistograms fgedudb fgedu_users | grep “99%” | awk ‘{print $3}’)
echo “墓碑比例(99%): ${TOMBSTONES}” | tee -a ${LOG_FILE}
# 如果墓碑过多,执行清理
if [ “${TOMBSTONES}” -gt 100 ]; then
echo “墓碑过多,执行清理…” | tee -a ${LOG_FILE}
nodetool garbagecollect fgedudb fgedu_users 2>&1 | tee -a ${LOG_FILE}
fi
# 清理旧快照(保留最近4周)
echo “清理旧快照…” | tee -a ${LOG_FILE}
KEEP_WEEKS=4
for snapshot in $(nodetool listsnapshots | grep weekly | awk ‘{print $1}’ | sort | head -n -${KEEP_WEEKS}); do
echo “删除快照: ${snapshot}” | tee -a ${LOG_FILE}
nodetool clearsnapshot -t ${snapshot} 2>&1 | tee -a ${LOG_FILE}
done
echo “=== 数据清理完成 ===” | tee -a ${LOG_FILE}
Part05-风哥经验总结与分享
5.1 Cassandra数据库维护最佳实践
Cassandra数据库维护最佳实践总结:
- 定期维护:建立定期维护计划,每天检查节点状态和日志,每周执行数据修复和压缩
- 自动化维护:编写自动化维护脚本,减少人工操作,提高维护效率
- 监控告警:建立完善的监控告警体系,及时发现和处理问题
- 文档记录:详细记录维护操作和结果,便于问题追溯和经验积累
- 预防为主:优先预防性维护,避免问题发生
- 持续改进:定期评估维护效果,持续优化维护策略
5.2 Cassandra数据库维护问题排查
# 1. 节点故障
# 症状: 节点离线
# 排查步骤
# 检查节点状态
nodetool status
# 检查节点日志
tail -100 /cassandra/logs/system.log
# 检查系统资源
top
df -h
free -m
# 检查网络
ping
netstat -an | grep 7000
# 2. 数据不一致
# 症状: 不同节点数据不同
# 排查步骤
# 检查数据
cqlsh
cqlsh
# 执行修复
nodetool repair -pr
# 验证结果
nodetool verify fgedudb fgedu_users
# 3. 性能下降
# 症状: 读写延迟增加
# 排查步骤
# 检查压缩状态
nodetool compactionstats
# 检查GC状态
jstat -gc $(pgrep -f cassandra) 1 10
# 检查资源使用
nodetool info
iostat -x 1 10
# 检查线程池
nodetool tpstats
# 4. 磁盘空间不足
# 症状: 磁盘使用率高
# 排查步骤
# 检查磁盘使用
df -h /cassandra/fgdata
# 检查快照
nodetool listsnapshots
# 清理快照
nodetool clearsnapshot
# 清理墓碑
nodetool garbagecollect
# 5. 内存不足
# 症状: 内存使用率高,GC频繁
# 排查步骤
# 检查内存使用
free -m
nodetool info | grep Heap
# 检查GC日志
tail -100 /cassandra/logs/gc.log
# 调整JVM参数
# 修改 jvm.options
5.3 Cassandra数据库维护检查清单
# 日检查清单
[ ] 节点状态检查
[ ] 日志错误检查
[ ] 性能指标检查
[ ] 资源使用检查
[ ] 告警处理
# 周检查清单
[ ] 数据修复
[ ] 压缩状态检查
[ ] 快照管理
[ ] 墓碑清理
[ ] 备份验证
# 月检查清单
[ ] 容量规划评估
[ ] 性能分析报告
[ ] 安全配置检查
[ ] 维护脚本优化
[ ] 文档更新
# 季度检查清单
[ ] 版本升级评估
[ ] 架构优化评估
[ ] 灾备演练
[ ] 培训计划
[ ] 维护策略评估
# 维护检查脚本
#!/bin/bash
# maintenance_checklist.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
LOG_FILE=”/cassandra/logs/maintenance/checklist_$(date +%Y%m%d).log”
echo “=== Cassandra维护检查清单 ===” | tee -a ${LOG_FILE}
echo “时间: $(date)” | tee -a ${LOG_FILE}
echo “” | tee -a ${LOG_FILE}
# 日检查
echo “【日检查项目】” | tee -a ${LOG_FILE}
echo “1. 节点状态检查” | tee -a ${LOG_FILE}
nodetool status | grep -E “^UN|^DN” | wc -l | tee -a ${LOG_FILE}
echo “2. 日志错误检查” | tee -a ${LOG_FILE}
grep -c -i error /cassandra/logs/system.log | tee -a ${LOG_FILE}
echo “3. 性能指标检查” | tee -a ${LOG_FILE}
nodetool tablestats | grep -E “Read Latency|Write Latency” | tee -a ${LOG_FILE}
echo “4. 资源使用检查” | tee -a ${LOG_FILE}
df -h /cassandra/fgdata | tail -1 | tee -a ${LOG_FILE}
echo “” | tee -a ${LOG_FILE}
echo “【周检查项目】” | tee -a ${LOG_FILE}
echo “5. 压缩状态检查” | tee -a ${LOG_FILE}
nodetool compactionstats | grep “pending tasks” | tee -a ${LOG_FILE}
echo “6. 快照管理” | tee -a ${LOG_FILE}
nodetool listsnapshots | wc -l | tee -a ${LOG_FILE}
echo “” | tee -a ${LOG_FILE}
echo “检查完成” | tee -a ${LOG_FILE}
本文档详细介绍了Cassandra数据库日常维护实战,包括日常维护概述、维护任务分类、维护策略、维护计划制定、维护工具介绍、维护监控体系、日常维护操作实战、周期性维护实战、自动化维护脚本、压缩维护实战、修复维护实战、清理维护实战、维护最佳实践、维护问题排查、维护检查清单等内容。通过学习本文档,读者可以掌握Cassandra数据库日常维护技能,为生产环境应用打下坚实基础。
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
