Cassandra教程FG003-Cassandra配置文件核心参数官方详解与生产调优实战
本文档风哥主要介绍Cassandra数据库配置文件核心参数官方详解与生产调优,包括配置文件概述、配置文件结构、参数分类、集群配置参数、存储配置参数、内存配置参数、JVM参数配置、网络参数配置、安全参数配置、参数调优实战、参数验证实战、参数问题处理等内容,风哥教程参考Cassandra官方文档Configuration内容编写,适合DBA人员在学习和测试中使用,如果要应用于生产环境则需要自行确认。
Part01-基础概念与理论知识
1.1 Cassandra数据库配置文件概述
Cassandra数据库配置文件是控制Cassandra运行行为的核心文件,通过合理配置参数可以优化系统性能、保障数据安全、提高系统可用性。Cassandra数据库主要配置文件包括cassandra.yaml、cassandra-rackdc.properties、jvm-server.options等。更多视频教程www.fgedu.net.cn
1.1.1 Cassandra数据库配置文件列表
# 核心配置文件
cassandra.yaml # 主配置文件,控制集群、存储、网络等核心参数
cassandra-rackdc.properties # 机架和数据中心配置
jvm-server.options # JVM参数配置
jvm.options # JVM参数配置(旧版本)
# 安全配置文件
cassandra-env.sh # 环境变量配置(包含JMX安全配置)
# 日志配置文件
logback.xml # 日志配置文件
logback-tools.xml # 工具日志配置
# 配置文件位置
/cassandra/app/conf/
1.1.2 Cassandra数据库配置文件优先级
# 1. 命令行参数
最高优先级,启动时指定参数
# 2. cassandra.yaml
核心配置文件,大部分参数在此配置
# 3. cassandra-rackdc.properties
机架和数据中心配置
# 4. jvm-server.options
JVM参数配置
# 5. 环境变量
通过cassandra-env.sh设置
# 配置生效顺序
启动时读取顺序:
cassandra-env.sh → jvm-server.options → cassandra.yaml → cassandra-rackdc.properties
1.2 Cassandra数据库配置文件结构
Cassandra数据库配置文件结构说明:
1.2.1 Cassandra数据库cassandra.yaml结构
# 集群配置
cluster_name: ‘Test Cluster’
num_tokens: 256
# 种子节点配置
seed_provider:
– class_name: org.apache.cassandra.locator.SimpleSeedProvider
parameters:
– seeds: “127.0.0.1”
# 网络配置
listen_address: localhost
rpc_address: localhost
native_transport_port: 9042
storage_port: 7000
# 存储配置
data_file_directories:
– /var/lib/cassandra/data
commitlog_directory: /var/lib/cassandra/commitlog
saved_caches_directory: /var/lib/cassandra/saved_caches
# 分区器和副本策略
partitioner: org.apache.cassandra.dht.Murmur3Partitioner
endpoint_snitch: SimpleSnitch
# 性能配置
concurrent_reads: 32
concurrent_writes: 32
concurrent_counter_writes: 32
# 压缩配置
compaction_throughput_mb_per_sec: 64
# 安全配置
authenticator: AllowAllAuthenticator
authorizer: AllowAllAuthorizer
1.3 Cassandra数据库参数分类
Cassandra数据库参数按功能分类:
1.3.1 Cassandra数据库参数分类说明
# 1. 集群参数
cluster_name # 集群名称
num_tokens # 令牌数量
seed_provider # 种子节点配置
endpoint_snitch # 节点嗅探器
# 2. 网络参数
listen_address # 监听地址
rpc_address # RPC地址
native_transport_port # CQL端口
storage_port # 内部通信端口
# 3. 存储参数
data_file_directories # 数据目录
commitlog_directory # 提交日志目录
saved_caches_directory # 缓存目录
commitlog_sync # 提交日志同步方式
# 4. 内存参数
memtable_allocation_type # Memtable分配类型
memtable_heap_space_in_mb # Memtable堆内存大小
memtable_offheap_space_in_mb # Memtable堆外内存大小
# 5. 性能参数
concurrent_reads # 并发读数
concurrent_writes # 并发写数
compaction_throughput_mb_per_sec # 压缩吞吐量
# 6. 安全参数
authenticator # 认证器
authorizer # 授权器
role_manager # 角色管理器
# 7. 压缩参数
compression # 压缩配置
# 8. 修复参数
repair # 修复配置
Part02-生产环境规划与建议
2.1 Cassandra数据库集群配置参数
Cassandra数据库集群配置参数详解:
2.1.1 Cassandra数据库集群名称配置
# vi /cassandra/app/conf/cassandra.yaml
# 集群名称
# 说明: 集群的唯一标识,同一集群所有节点必须相同
# 默认值: ‘Test Cluster’
# 生产建议: 使用有意义的名称,如公司名_应用名_环境
cluster_name: ‘fgedu_cluster’
# 令牌数量
# 说明: 每个节点负责的令牌范围数量
# 默认值: 256
# 生产建议: 保持默认值256,提供更好的数据分布
num_tokens: 256
# 种子节点配置
# 说明: 新节点加入集群时用于获取集群信息的节点
# 默认值: 127.0.0.1
# 生产建议: 配置2-3个种子节点,不要配置所有节点
seed_provider:
– class_name: org.apache.cassandra.locator.SimpleSeedProvider
parameters:
– seeds: “192.168.1.101,192.168.1.102”
# 节点嗅探器
# 说明: 用于感知网络拓扑的组件
# 默认值: SimpleSnitch
# 生产建议: 使用GossipingPropertyFileSnitch
endpoint_snitch: GossipingPropertyFileSnitch
2.1.2 Cassandra数据库数据中心配置
# vi /cassandra/app/conf/cassandra-rackdc.properties
# 数据中心名称
# 说明: 节点所属的数据中心
# 生产建议: 使用有意义的名称,如dc1、dc_beijing
dc=dc1
# 机架名称
# 说明: 节点所属的机架
# 生产建议: 使用有意义的名称,如rack1、rack_a
rack=rack1
# 多数据中心配置示例
# 数据中心1 – 机架1
# dc=dc1
# rack=rack1
# 数据中心1 – 机架2
# dc=dc1
# rack=rack2
# 数据中心2 – 机架1
# dc=dc2
# rack=rack1
# 查看当前配置
# nodetool describecluster
Cluster Information:
Name: fgedu_cluster
Snitch: org.apache.cassandra.locator.GossipingPropertyFileSnitch
Partitioner: org.apache.cassandra.dht.Murmur3Partitioner
Schema versions:
8e5f6a7b-8c9d-0e1f-2a3b-4c5d6e7f8a9b: [192.168.1.101, 192.168.1.102, 192.168.1.103]
2.2 Cassandra数据库存储配置参数
Cassandra数据库存储配置参数详解:
2.2.1 Cassandra数据库数据目录配置
# vi /cassandra/app/conf/cassandra.yaml
# 数据文件目录
# 说明: SSTable数据文件存储位置
# 默认值: /var/lib/cassandra/data
# 生产建议: 使用独立磁盘,可配置多个目录提高IO性能
data_file_directories:
– /cassandra/fgdata/data
# 提交日志目录
# 说明: 提交日志存储位置
# 默认值: /var/lib/cassandra/commitlog
# 生产建议: 使用独立磁盘,与数据目录分离
commitlog_directory: /cassandra/commitlog
# 缓存目录
# 说明: 保存的缓存文件位置
# 默认值: /var/lib/cassandra/saved_caches
# 生产建议: 使用独立磁盘
saved_caches_directory: /cassandra/saved_caches
# Hints目录
# 说明: Hints文件存储位置
# 默认值: $data_file_directories/hints
# 生产建议: 与数据目录分离
hints_directory: /cassandra/fgdata/hints
# CDC目录
# 说明: 变更数据捕获日志位置
# 默认值: $data_file_directories/cdc
# 生产建议: 根据CDC需求配置
cdc_directory: /cassandra/fgdata/cdc
# 查看当前配置
# nodetool info | grep -E “Load|Data”
Load : 125.45 GB
2.2.2 Cassandra数据库提交日志配置
# vi /cassandra/app/conf/cassandra.yaml
# 提交日志同步方式
# 说明: 提交日志刷盘方式
# 可选值: periodic(周期性)、batch(批量)
# 默认值: periodic
# 生产建议: 使用periodic,性能更好
commitlog_sync: periodic
# 周期性同步间隔(commitlog_sync=periodic时)
# 说明: 提交日志刷盘周期
# 默认值: 10000ms(10秒)
# 生产建议: 根据数据重要性调整,重要数据可设置较小值
commitlog_sync_period_in_ms: 10000
# 批量同步等待时间(commitlog_sync=batch时)
# 说明: 批量同步等待时间
# 默认值: 无
# 生产建议: 不推荐使用batch模式
# commitlog_sync_batch_window_in_ms: 2
# 提交日志段大小
# 说明: 单个提交日志文件大小
# 默认值: 32MB
# 生产建议: 保持默认值或根据写入量调整
commitlog_segment_size_in_mb: 32
# 提交日志压缩
# 说明: 是否压缩提交日志
# 默认值: 无压缩
# 生产建议: 启用LZ4压缩节省空间
commitlog_compression:
– class_name: LZ4Compressor
# 提交日志总大小
# 说明: 提交日志总大小限制
# 默认值: 8GB(4.x版本)
# 生产建议: 根据写入量调整
commitlog_total_space_in_mb: 8192
2.2.3 Cassandra数据库压缩配置
# vi /cassandra/app/conf/cassandra.yaml
# 压缩吞吐量
# 说明: 压缩操作的最大吞吐量
# 默认值: 64MB/s
# 生产建议: 根据磁盘IO能力调整,SSD可设置更高值
compaction_throughput_mb_per_sec: 64
# 并发压缩数
# 说明: 同时进行的压缩任务数
# 默认值: 核心数/4,最小2
# 生产建议: 根据CPU和IO能力调整
concurrent_compactors: 4
# 压缩大分区阈值
# 说明: 大于此值的分区使用特殊压缩策略
# 默认值: 100MB
# 生产建议: 根据数据特点调整
compaction_large_partition_warning_threshold_mb: 100
# SSTable压缩配置
# 说明: SSTable文件压缩算法
# 默认值: LZ4Compressor
# 生产建议: 使用LZ4或ZSTD
# 在创建表时配置
# CREATE TABLE fgedu_table (…)
# WITH compression = {‘class’: ‘LZ4Compressor’, ‘chunk_length_in_kb’: 64};
2.3 Cassandra数据库内存配置参数
Cassandra数据库内存配置参数详解:
2.3.1 Cassandra数据库Memtable配置
# vi /cassandra/app/conf/cassandra.yaml
# Memtable分配类型
# 说明: Memtable内存分配方式
# 可选值: heap_buffers(堆内存)、offheap_buffers(堆外内存)、offheap_objects(堆外对象)
# 默认值: heap_buffers
# 生产建议: 使用offheap_buffers减少GC压力
memtable_allocation_type: offheap_buffers
# Memtable堆内存大小
# 说明: Memtable使用的堆内存大小
# 默认值: 堆内存的1/4
# 生产建议: 根据写入量调整,不超过堆内存的1/3
memtable_heap_space_in_mb: 2048
# Memtable堆外内存大小
# 说明: Memtable使用的堆外内存大小
# 默认值: 堆内存的1/4
# 生产建议: 根据写入量调整
memtable_offheap_space_in_mb: 2048
# Memtable清理阈值
# 说明: Memtable占用内存达到此比例时触发清理
# 默认值: 0.5(50%)
# 生产建议: 保持默认值
memtable_cleanup_threshold: 0.5
# Memtable刷盘方式
# 说明: Memtable刷盘策略
# 可选值: auto(自动)、fixed(固定大小)
# 默认值: auto
# 生产建议: 使用auto
memtable_flush_writers: auto
# 查看Memtable状态
# nodetool cfstats fgedudb.fgedu_table | grep -E “Memtable|Flush”
Memtable data size: 125.45 MB
Memtable off heap memory used: 128.00 MB
Memtable switch count: 15
Local read count: 12345
Local read latency: 0.125 ms
Local write count: 67890
Local write latency: 0.025 ms
Pending flushes: 0
2.3.2 Cassandra数据库缓存配置
# vi /cassandra/app/conf/cassandra.yaml
# Key缓存大小
# 说明: Key缓存大小,缓存分区键
# 默认值: 堆内存的5%,最小256KB
# 生产建议: 根据热点数据调整
key_cache_size_in_mb: 256
# Key缓存保存周期
# 说明: Key缓存保存时间
# 默认值: 14400秒(4小时)
# 生产建议: 根据数据访问模式调整
key_cache_save_period: 14400
# Key缓存预热
# 说明: 启动时是否预热Key缓存
# 默认值: true
# 生产建议: 保持默认值
key_cache_preload: true
# Row缓存大小
# 说明: Row缓存大小,缓存整行数据
# 默认值: 0(禁用)
# 生产建议: 热点数据场景启用,设置合适大小
row_cache_size_in_mb: 512
# Row缓存提供者
# 说明: Row缓存实现方式
# 可选值: SerializingCacheProvider、OHCProvider
# 默认值: SerializingCacheProvider
# 生产建议: 使用OHCProvider支持堆外内存
row_cache_provider: OHCProvider
# Row缓存保存周期
# 说明: Row缓存保存时间
# 默认值: 0(不保存)
# 生产建议: 根据数据访问模式调整
row_cache_save_period: 0
# 计数器缓存大小
# 说明: 计数器缓存大小
# 默认值: min(2.5% of Heap, 50MB)
# 生产建议: 使用计数器时配置
counter_cache_size_in_mb: 128
# 查看缓存状态
# nodetool info | grep -E “Cache”
Key Cache : entries 123456, size 128 MB, capacity 256 MB, 98.5% hits
Row Cache : entries 5678, size 64 MB, capacity 512 MB, 95.2% hits
Counter Cache : entries 0, size 0 bytes, capacity 128 MB, NaN% hits
Part03-生产环境项目实施方案
3.1 Cassandra数据库JVM参数配置
Cassandra数据库JVM参数配置详解:
3.1.1 Cassandra数据库堆内存配置
# vi /cassandra/app/conf/jvm-server.options
# 初始堆内存
# 说明: JVM初始堆内存大小
# 默认值: 无
# 生产建议: 与最大堆内存相同,避免动态调整开销
-Xms8G
# 最大堆内存
# 说明: JVM最大堆内存大小
# 默认值: 无
# 生产建议: 系统内存的1/4到1/2,最大不超过32GB
-Xmx8G
# 新生代大小
# 说明: 新生代内存大小
# 默认值: 无
# 生产建议: 堆内存的1/4到1/3
-Xmn2G
# 堆内存配置原则
# 1. 堆内存不超过32GB(避免指针压缩失效)
# 2. 预留足够内存给操作系统和页缓存
# 3. 新生代大小影响GC频率
# 查看JVM内存使用
# jcmd
-XX:InitialHeapSize=8589934592 -XX:MaxHeapSize=8589934592 -XX:NewSize=2147483648
3.1.2 Cassandra数据库垃圾回收配置
# vi /cassandra/app/conf/jvm-server.options
# 垃圾回收器
# 说明: 使用的垃圾回收器
# 可选值: UseG1GC、UseZGC、UseShenandoahGC
# 默认值: G1GC
# 生产建议: 使用G1GC,Java 17+可考虑ZGC
-XX:+UseG1GC
# G1最大暂停时间
# 说明: GC最大暂停时间目标
# 默认值: 无
# 生产建议: 设置200-500ms
-XX:MaxGCPauseMillis=200
# G1堆占用阈值
# 说明: 堆占用达到此比例时启动并发GC
# 默认值: 45
# 生产建议: 设置35-40
-XX:InitiatingHeapOccupancyPercent=35
# G1区域大小
# 说明: G1区域大小
# 默认值: 自动计算
# 生产建议: 大堆内存设置较大值
-XX:G1HeapRegionSize=16M
# G1混合GC次数
# 说明: 混合GC次数
# 默认值: 8
# 生产建议: 保持默认值
-XX:G1MixedGCCountTarget=8
# 查看GC配置
# jcmd
-XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:InitiatingHeapOccupancyPercent=35 -XX:G1HeapRegionSize=16777216
3.1.3 Cassandra数据库GC日志配置
# vi /cassandra/app/conf/jvm-server.options
# GC日志输出(Java 11+)
# 说明: GC日志配置
-Xlog:gc=info,heap*=debug,age*=trace:file=/cassandra/logs/gc.log:time,uptime,level,tags:filecount=10,filesize=10M
# 参数说明
# gc=info: GC基本信息
# heap*=debug: 堆详细信息
# age*=trace: 对象年龄信息
# file: 日志文件路径
# time,uptime,level,tags: 日志格式
# filecount: 日志文件数量
# filesize: 单个日志文件大小
# GC日志分析
# 查看GC日志
# tail -100 /cassandra/logs/gc.log
# 使用GCViewer分析GC日志
# java -jar gcviewer.jar /cassandra/logs/gc.log
# GC日志示例
[2024-01-15T10:30:00.123+0800][info][gc] GC(0) Pause Young (G1 Evacuation Pause) 1024M->256M(8192M) 15.234ms
[2024-01-15T10:30:05.456+0800][info][gc] GC(1) Pause Young (G1 Evacuation Pause) 1280M->384M(8192M) 18.567ms
[2024-01-15T10:30:10.789+0800][info][gc] GC(2) Pause Young (G1 Evacuation Pause) 1408M->512M(8192M) 20.123ms
3.1.4 Cassandra数据库其他JVM参数
# vi /cassandra/app/conf/jvm-server.options
# 元空间大小
# 说明: 类元数据存储区域
# 默认值: 无限制
# 生产建议: 设置最大值防止内存泄漏
-XX:MaxMetaspaceSize=256M
-XX:MetaspaceSize=64M
# 直接内存限制
# 说明: 直接内存最大值
# 默认值: 等于最大堆内存
# 生产建议: 根据堆外内存使用量调整
-XX:MaxDirectMemorySize=2G
# OOM时生成堆转储
# 说明: 发生OOM时生成堆转储文件
# 默认值: 无
# 生产建议: 启用,便于问题分析
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/cassandra/logs/
# 禁用显式GC
# 说明: 禁用System.gc()调用
# 默认值: 无
# 生产建议: 启用
-XX:+DisableExplicitGC
# 字符串去重
# 说明: G1字符串去重
# 默认值: 无
# 生产建议: 启用,减少内存占用
-XX:+UseStringDeduplication
3.2 Cassandra数据库网络参数配置
Cassandra数据库网络参数配置详解:
3.2.1 Cassandra数据库监听地址配置
# vi /cassandra/app/conf/cassandra.yaml
# 监听地址
# 说明: 节点间通信监听地址
# 默认值: localhost
# 生产建议: 设置为本机IP地址
listen_address: 192.168.1.101
# RPC地址
# 说明: 客户端连接监听地址
# 默认值: localhost
# 生产建议: 设置为本机IP地址
rpc_address: 192.168.1.101
# 广播地址
# 说明: 广播给其他节点的地址
# 默认值: listen_address
# 生产建议: 多网卡时设置
# broadcast_address: 192.168.1.101
# RPC广播地址
# 说明: 广播给客户端的地址
# 默认值: rpc_address
# 生产建议: 客户端连接地址
# broadcast_rpc_address: 192.168.1.101
# 查看当前配置
# nodetool info | grep -E “Address|Listen”
Listen address : 192.168.1.101
Native transport address: 192.168.1.101
3.2.2 Cassandra数据库端口配置
# vi /cassandra/app/conf/cassandra.yaml
# CQL客户端端口
# 说明: CQL客户端连接端口
# 默认值: 9042
# 生产建议: 保持默认值
native_transport_port: 9042
# 内部通信端口
# 说明: 节点间通信端口
# 默认值: 7000
# 生产建议: 保持默认值
storage_port: 7000
# SSL内部通信端口
# 说明: 启用SSL时节点间通信端口
# 默认值: 7001
# 生产建议: 启用SSL时使用
ssl_storage_port: 7100
# JMX端口
# 说明: JMX监控端口
# 默认值: 7199
# 生产建议: 保持默认值
# 在cassandra-env.sh中配置
# JVM_OPTS=”$JVM_OPTS -Dcom.sun.management.jmxremote.port=7199″
# 查看端口监听
# netstat -tlnp | grep java
tcp6 0 0 :::9042 :::* LISTEN 12345/java
tcp6 0 0 :::7000 :::* LISTEN 12345/java
tcp6 0 0 :::7199 :::* LISTEN 12345/java
3.2.3 Cassandra数据库连接配置
# vi /cassandra/app/conf/cassandra.yaml
# 最大并发连接数
# 说明: 最大并发客户端连接数
# 默认值: -1(无限制)
# 生产建议: 根据业务需求设置
native_transport_max_concurrent_connections: -1
# 每IP最大连接数
# 说明: 每个IP最大连接数
# 默认值: -1(无限制)
# 生产建议: 设置合理值防止连接攻击
native_transport_max_concurrent_connections_per_ip: 100
# 最大线程数
# 说明: 处理客户端请求的最大线程数
# 默认值: 200
# 生产建议: 根据CPU核心数调整
native_transport_max_threads: 200
# 最大帧大小
# 说明: 单个请求最大帧大小
# 默认值: 256MB
# 生产建议: 根据数据大小调整
native_transport_max_frame_size_in_mb: 256
# 连接超时
# 说明: 客户端连接超时时间
# 默认值: 5000ms
# 生产建议: 根据网络延迟调整
# request_timeout_in_ms: 10000
# 查看连接状态
# nodetool netstats
Mode: NORMAL
Not sending any streams.
Read Repair Sessions:
Pool Name Active Pending Completed Dropped
Large messages n/a 0 0 n/a
Small messages n/a 0 0 n/a
Gossip messages n/a 0 0 n/a
3.3 Cassandra数据库安全参数配置
Cassandra数据库安全参数配置详解:
3.3.1 Cassandra数据库认证配置
# vi /cassandra/app/conf/cassandra.yaml
# 认证器
# 说明: 用户认证方式
# 可选值: AllowAllAuthenticator(无认证)、PasswordAuthenticator(密码认证)
# 默认值: AllowAllAuthenticator
# 生产建议: 使用PasswordAuthenticator
authenticator: PasswordAuthenticator
# 授权器
# 说明: 权限授权方式
# 可选值: AllowAllAuthorizer(无授权)、CassandraAuthorizer(Cassandra授权)
# 默认值: AllowAllAuthorizer
# 生产建议: 使用CassandraAuthorizer
authorizer: CassandraAuthorizer
# 角色管理器
# 说明: 角色管理方式
# 可选值: CassandraRoleManager
# 默认值: CassandraRoleManager
# 生产建议: 保持默认值
role_manager: CassandraRoleManager
# 角色有效期
# 说明: 角色缓存有效期
# 默认值: 2000ms
# 生产建议: 根据安全要求调整
roles_validity_in_ms: 2000
# 权限有效期
# 说明: 权限缓存有效期
# 默认值: 2000ms
# 生产建议: 根据安全要求调整
permissions_validity_in_ms: 2000
# 重启服务生效
# systemctl restart cassandra
# 验证认证配置
# cqlsh 192.168.1.101 9042 -u cassandra -p cassandra
Connected to fgedu_cluster at 192.168.1.101:9042
3.3.2 Cassandra数据库加密配置
# vi /cassandra/app/conf/cassandra.yaml
# 客户端加密
# 说明: 客户端连接加密
# 默认值: 禁用
# 生产建议: 启用SSL加密
client_encryption_options:
enabled: true
optional: false
keystore: /cassandra/app/conf/.keystore
keystore_password: cassandra
# 节点间加密
# 说明: 节点间通信加密
# 默认值: 禁用
# 生产建议: 启用SSL加密
server_encryption_options:
internode_encryption: all
keystore: /cassandra/app/conf/.keystore
keystore_password: cassandra
truststore: /cassandra/app/conf/.truststore
truststore_password: cassandra
# 生成密钥库
# keytool -genkeypair -alias cassandra -keyalg RSA -keysize 2048 -validity 365 -keystore /cassandra/app/conf/.keystore -storepass cassandra -keypass cassandra -dname “CN=cassandra, OU=IT, O=FGEDU, L=Beijing, ST=Beijing, C=CN”
# 重启服务生效
# systemctl restart cassandra
# 使用SSL连接
# cqlsh 192.168.1.101 9042 –ssl
Part04-生产案例与实战讲解
4.1 Cassandra数据库参数调优实战
Cassandra数据库参数调优实战案例:
4.1.1 Cassandra数据库高写入场景调优
# 场景: 日志系统,写入量10万TPS
# 1. JVM配置
# vi /cassandra/app/conf/jvm-server.options
-Xms16G
-Xmx16G
-Xmn4G
-XX:+UseG1GC
-XX:MaxGCPauseMillis=200
-XX:InitiatingHeapOccupancyPercent=35
# 2. Memtable配置
# vi /cassandra/app/conf/cassandra.yaml
memtable_allocation_type: offheap_buffers
memtable_heap_space_in_mb: 4096
memtable_offheap_space_in_mb: 4096
memtable_flush_writers: 4
# 3. 提交日志配置
# vi /cassandra/app/conf/cassandra.yaml
commitlog_sync: periodic
commitlog_sync_period_in_ms: 10000
commitlog_segment_size_in_mb: 64
commitlog_total_space_in_mb: 16384
commitlog_compression:
– class_name: LZ4Compressor
# 4. 压缩配置
# vi /cassandra/app/conf/cassandra.yaml
compaction_throughput_mb_per_sec: 128
concurrent_compactors: 8
# 5. 并发配置
# vi /cassandra/app/conf/cassandra.yaml
concurrent_reads: 64
concurrent_writes: 64
concurrent_counter_writes: 32
# 重启服务
# systemctl restart cassandra
# 验证调优效果
# nodetool info
Load : 500.45 GB
Heap Memory (MB) : 8192.00 / 16384.00
Off Heap Memory (MB) : 4096.00
Key Cache : entries 234567, size 256 MB, capacity 512 MB, 99.2% hits
4.1.2 Cassandra数据库高读取场景调优
# 场景: 缓存系统,读取量50万QPS
# 1. JVM配置
# vi /cassandra/app/conf/jvm-server.options
-Xms8G
-Xmx8G
-Xmn2G
-XX:+UseG1GC
-XX:MaxGCPauseMillis=100
# 2. 缓存配置
# vi /cassandra/app/conf/cassandra.yaml
key_cache_size_in_mb: 512
row_cache_size_in_mb: 2048
row_cache_provider: OHCProvider
key_cache_save_period: 14400
row_cache_save_period: 0
# 3. Memtable配置
# vi /cassandra/app/conf/cassandra.yaml
memtable_allocation_type: offheap_buffers
memtable_heap_space_in_mb: 2048
memtable_offheap_space_in_mb: 2048
# 4. 并发配置
# vi /cassandra/app/conf/cassandra.yaml
concurrent_reads: 128
concurrent_writes: 32
# 5. 网络配置
# vi /cassandra/app/conf/cassandra.yaml
native_transport_max_threads: 400
native_transport_max_concurrent_connections: 1000
# 重启服务
# systemctl restart cassandra
# 验证缓存效果
# nodetool info | grep -E “Cache”
Key Cache : entries 567890, size 512 MB, capacity 512 MB, 99.8% hits
Row Cache : entries 123456, size 2048 MB, capacity 2048 MB, 99.5% hits
4.2 Cassandra数据库参数验证实战
Cassandra数据库参数验证实战:
4.2.1 Cassandra数据库参数查看
# 查看YAML配置
# grep -v “^#” /cassandra/app/conf/cassandra.yaml | grep -v “^$”
# 查看JVM配置
# jcmd
# 查看运行时配置
# nodetool getcachecapacity
Key cache capacity unit: MB
Key cache capacity: 256 MB
Row cache capacity unit: MB
Row cache capacity: 512 MB
Counter cache capacity unit: MB
Counter cache capacity: 128 MB
# 查看压缩配置
# nodetool getcompactionthroughput
Current compaction throughput: 64 MB/s
# 查看并发配置
# nodetool getconcurrentcompactors
Current concurrent compactors: 4
# 查看日志配置
# nodetool getlogginglevels
LoggerName | Level
—————————————————–+——-
ROOT | INFO
org.apache.cassandra | INFO
org.apache.cassandra.db | DEBUG
4.2.2 Cassandra数据库参数动态调整
# 调整压缩吞吐量
# nodetool setcompactionthroughput 128
# nodetool getcompactionthroughput
Current compaction throughput: 128 MB/s
# 调整并发压缩数
# nodetool setconcurrentcompactors 8
# nodetool getconcurrentcompactors
Current concurrent compactors: 8
# 调整日志级别
# nodetool setlogginglevel org.apache.cassandra DEBUG
# nodetool getlogginglevels | grep cassandra
org.apache.cassandra | DEBUG
# 调整缓存容量
# nodetool setcachecapacity 512 1024 128
# nodetool getcachecapacity
Key cache capacity unit: MB
Key cache capacity: 512 MB
Row cache capacity unit: MB
Row cache capacity: 1024 MB
Counter cache capacity unit: MB
Counter cache capacity: 128 MB
# 触发压缩
# nodetool compact fgedudb fgedu_table
# 触发清理
# nodetool cleanup fgedudb
# 触发修复
# nodetool repair fgedudb
4.3 Cassandra数据库参数问题处理
Cassandra数据库参数问题处理案例:
4.3.1 Cassandra数据库GC频繁问题
# 系统响应慢,GC日志显示频繁Full GC
# 分析步骤
# 1. 查看GC日志
# tail -100 /cassandra/logs/gc.log
[2024-01-15T10:30:00.123+0800][info][gc] GC(100) Pause Full (G1 Evacuation Pause) 8192M->6144M(8192M) 2345.678ms
# 2. 查看堆内存使用
# jcmd
Heap Usage:
G1 Heap:
regions = 8192
capacity = 8589934592 (8192.0MB)
used = 7516192768 (7168.0MB)
free = 1073741824 (1024.0MB)
# 3. 分析原因
# 堆内存不足,对象晋升过快
# 解决方案
# 方案1: 增加堆内存
# vi /cassandra/app/conf/jvm-server.options
-Xms12G
-Xmx12G
# 方案2: 调整新生代大小
# vi /cassandra/app/conf/jvm-server.options
-Xmn4G
# 方案3: 调整G1参数
# vi /cassandra/app/conf/jvm-server.options
-XX:InitiatingHeapOccupancyPercent=30
-XX:G1HeapRegionSize=16M
# 方案4: 减少Memtable内存
# vi /cassandra/app/conf/cassandra.yaml
memtable_heap_space_in_mb: 1536
# 重启服务
# systemctl restart cassandra
# 验证效果
# tail -f /cassandra/logs/gc.log
4.3.2 Cassandra数据库压缩积压问题
# 磁盘空间不足,压缩任务积压
# 分析步骤
# 1. 查看压缩状态
# nodetool compactionstats
pending tasks: 50
– fgedudb.fgedu_table: 45
– system.peers: 5
# 2. 查看压缩吞吐量
# nodetool getcompactionthroughput
Current compaction throughput: 64 MB/s
# 3. 查看磁盘IO
# iostat -x 1 10
avg-cpu: %user %nice %system %iowait %steal %idle
15.23 0.00 10.45 45.67 0.00 28.65
# 解决方案
# 方案1: 提高压缩吞吐量
# nodetool setcompactionthroughput 128
# 方案2: 增加并发压缩数
# nodetool setconcurrentcompactors 8
# 方案3: 手动触发压缩
# nodetool compact fgedudb fgedu_table
# 方案4: 调整压缩策略(创建表时)
# ALTER TABLE fgedudb.fgedu_table WITH compaction = {‘class’: ‘SizeTieredCompactionStrategy’, ‘max_threshold’: 32};
# 验证效果
# nodetool compactionstats
pending tasks: 10
– fgedudb.fgedu_table: 5
– system.peers: 5
Part05-风哥经验总结与分享
5.1 Cassandra数据库参数调优最佳实践
Cassandra数据库参数调优最佳实践总结:
- 内存配置:堆内存不超过32GB,预留足够内存给操作系统
- GC配置:使用G1GC,设置合理的暂停时间目标
- Memtable配置:使用堆外内存,减少GC压力
- 缓存配置:根据热点数据配置Key和Row缓存
- 压缩配置:根据磁盘IO能力调整压缩吞吐量
- 网络配置:根据并发需求调整连接和线程数
- 安全配置:生产环境必须启用认证和授权
- 监控配置:启用JMX监控,定期检查系统状态
5.2 Cassandra数据库参数调优检查清单
# 1. JVM配置检查
[ ] 堆内存大小合理(不超过32GB)
[ ] 新生代大小合理(堆内存的1/4到1/3)
[ ] 垃圾回收器配置正确
[ ] GC日志已启用
[ ] 元空间大小已限制
# 2. 内存配置检查
[ ] Memtable内存大小合理
[ ] Memtable使用堆外内存
[ ] Key缓存大小合理
[ ] Row缓存大小合理(如启用)
# 3. 存储配置检查
[ ] 数据目录配置正确
[ ] 提交日志目录独立
[ ] 提交日志同步方式正确
[ ] 压缩配置合理
# 4. 网络配置检查
[ ] 监听地址配置正确
[ ] 端口配置正确
[ ] 连接数配置合理
[ ] 超时时间合理
# 5. 安全配置检查
[ ] 认证已启用
[ ] 授权已启用
[ ] SSL加密已启用(如需要)
[ ] 默认密码已修改
# 6. 性能配置检查
[ ] 并发读写数合理
[ ] 压缩吞吐量合理
[ ] 压缩并发数合理
# 7. 监控配置检查
[ ] JMX已启用
[ ] 日志级别合理
[ ] 监控指标已采集
5.3 Cassandra数据库参数调优工具推荐
Cassandra数据库参数调优工具推荐:
5.3.1 Cassandra数据库监控工具
# 1. nodetool
# Cassandra自带工具,功能全面
# nodetool info
# nodetool status
# nodetool cfstats
# nodetool compactionstats
# 2. JConsole # 3. JVisualVM # 4. Prometheus + Grafana # 5. DataStax OpsCenter # 6. Cassandra Reaper
# Java自带JMX监控工具
# jconsole
# Java性能分析工具
# jvisualvm
# 开源监控方案
# 采集JMX指标,可视化展示
# DataStax官方监控工具
# 提供图形化管理界面
# 修复管理工具
# 管理和监控修复任务
5.3.2 Cassandra数据库性能测试工具
# 1. cassandra-stress
# Cassandra自带压测工具
# cassandra-stress write n=1000000 -node 192.168.1.101 -rate threads=50
# cassandra-stress read n=500000 -node 192.168.1.101 -rate threads=50
# 2. YCSB
# Yahoo云服务基准测试
# 支持多种NoSQL数据库
# 3. NoSQLBench
# 现代NoSQL基准测试工具
# 支持复杂场景测试
# 4. JMeter
# 通用性能测试工具
# 支持Cassandra插件
本文档详细介绍了Cassandra数据库配置文件核心参数官方详解与生产调优,包括配置文件概述、配置文件结构、参数分类、集群配置参数、存储配置参数、内存配置参数、JVM参数配置、网络参数配置、安全参数配置、参数调优实战、参数验证实战、参数问题处理、参数调优最佳实践、参数调优检查清单、参数调优工具推荐等内容。通过学习本文档,读者可以掌握Cassandra数据库参数配置和调优技能,为系统性能优化打下坚实基础。
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
