在 CentOS 上调试 Redis,通常分为 服务调试、性能分析、连接问题排查、源码级调试 等几个层次。下面按常见场景由浅入深说明。
ps -ef | grep redis
systemctl status redis
默认日志位置(视配置而定):
tail -f /var/log/redis/redis.log
常见配置项:
loglevel notice
logfile /var/log/redis/redis.log
redis-cli
常用命令:
ping
info
info memory
info stats
info replication
redis-cli slowlog get 10
配置示例:
slowlog-log-slower-than 10000 # 微秒
slowlog-max-len 128
redis-cli --latency
redis-cli --latency-history
ss -lntp | grep 6379
netstat -lntp | grep 6379
firewall-cmd --list-ports
firewall-cmd --add-port=6379/tcp --permanent
firewall-cmd --reload
redis-cli config get maxclients
系统级限制:
ulimit -n
redis-cli info memory
重点关注:
used_memoryused_memory_rssmem_fragmentation_ratioredis-cli info persistence
查看 AOF 重写:
redis-cli config get appendonly
适用于 Redis 崩溃、异常退出、C 级别 bug。
yum install -y gdb
Redis 官方提供 redis-server-debug:
wget http://download.redis.io/releases/redis-7.2.4.tar.gz
tar -xzf redis-7.2.4.tar.gz
cd redis-7.2.4
make CFLAGS="-g -O0"
gdb ./src/redis-server
run /etc/redis.conf
bt # 查看调用栈
info threads
thread apply all bt
strace -p $(pidof redis-server)
查看系统调用耗时:
strace -c -p $(pidof redis-server)
yum install -y perf
perf top -p $(pidof redis-server)
| 问题 | 调试方式 |
|---|---|
| Redis 起不来 | 日志 + systemctl status |
| 响应慢 | slowlog + latency |
| 内存暴涨 | info memory |
| 连接失败 | 端口 + 防火墙 + maxclients |
| 崩溃 | gdb + core dump |
| CPU 高 | perf top |
ulimit -c unlimited
echo "/var/core/core-%e-%p" > /proc/sys/kernel/core_pattern
如果你能说明 具体调试目的(例如:Redis 卡死、CPU 100%、主从同步失败、C 源码问题),我可以给你更精确的调试步骤。