在CentOS上配置Redis时,配置文件通常位于 /etc/redis.conf
。以下是一个示例配置文件的结构和内容解析:
# 全局配置
port 6379
bind 127.0.0.1
protected-mode yes
daemonize no
pidfile /var/run/redis_6379.pid
logfile /var/log/redis/redis_6379.log
loglevel notice
# 持久化配置
save 900 1
save 300 10
save 60 10000
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
# 安全性配置
requirepass your_strong_password
masterauth your_master_password
rename-command FLUSHDB ""
rename-command FLUSHALL ""
# 内存管理配置
maxmemory 512mb
maxmemory-policy allkeys-lru
# 网络配置
timeout 300
tcp-keepalive 300
# 复制配置
slaveof 127.0.0.1 6380
repl-id your_replica_id
repl-diskless-sync yes
repl-diskless-sync-delay 5
# 集群配置
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
# 其他配置
lua-time-limit 5000
notify-keyspace-events KEA
全局配置:
port
: Redis服务器监听的端口号,默认为6379。bind
: 绑定的IP地址,留空表示监听所有接口。protected-mode
: 是否启用保护模式,建议设置为yes
。daemonize
: 是否以守护进程方式运行,默认为no
。pidfile
: PID文件路径,用于记录Redis进程ID。logfile
: 日志文件路径,用于记录Redis日志。loglevel
: 日志级别,可选值有debug
、verbose
、notice
和warning
。持久化配置:
save
: 设置自动保存快照的条件。appendonly
: 是否启用AOF持久化,建议设置为yes
。appendfilename
: AOF文件名。appendfsync
: AOF同步策略,可选值有everysec
、always
和no
。安全性配置:
requirepass
: 设置连接密码,建议设置强密码。masterauth
: 主节点认证密码(用于复制)。rename-command
: 重命名危险命令,以防止未授权操作。内存管理配置:
maxmemory
: 设置最大内存限制,以防止内存溢出。maxmemory-policy
: 内存淘汰策略,可选值有volatile-lru
、allkeys-lru
等。网络配置:
timeout
: 客户端空闲连接超时时间。tcp-keepalive
: TCP保持连接检测间隔。复制配置:
slaveof
: 设置主节点地址和端口。repl-id
: 复制ID。repl-diskless-sync
: 是否启用无磁盘复制。repl-diskless-sync-delay
: 无磁盘复制的延迟时间。集群配置:
cluster-enabled
: 是否启用集群模式。cluster-config-file
: 集群配置文件路径。cluster-node-timeout
: 节点超时时间。请注意,以上配置示例仅供参考,实际生产环境中应根据具体需求进行调整。例如,在生产环境中,建议绑定特定IP地址以增强安全性,并根据服务器资源设置合理的内存限制和持久化策略。