在CentOS系统中,监控Redis的性能与状态可通过自带命令行工具、轻量级第三方工具或专业监控系统实现,以下是具体方法:
Redis自带的redis-cli工具无需额外安装,适合快速检查Redis状态:
redis-cli ping,若返回PONG则表示服务正常运行。redis-cli info可获取内存使用、连接数、命中率等全面信息;若需特定指标(如内存、客户端连接),可使用redis-cli info memory或redis-cli info clients。redis-cli monitor会实时显示所有进入Redis的命令(生产环境慎用,避免大量输出影响性能)。/etc/redis/redis.conf,设置slowlog-log-slower-than 10000(超过10秒的命令)、slowlog-max-len 128(最多保存128条日志);sudo systemctl restart redis;redis-cli slowlog get。redis-benchmark -h localhost -p 6379 -c 50 -n 100000(模拟50个并发客户端发送10万次请求);redis-cli --bigkeys(统计数据库中最大的Key)。redis-cli --latency可测量Redis服务器的响应延迟(单位:毫秒)。sudo yum install epel-release && sudo yum install redis-stat;redis-stat(默认监控本地Redis实例,可指定主机/端口:redis-stat -h 192.168.1.100 -p 6379),实时显示QPS、内存使用、连接数等指标。sudo yum install glances;glances --plugins=redis(需提前安装redis Python包),可监控Redis的内存、CPU、命令执行等状态。Prometheus + Grafana(推荐,适合生产环境)
wget https://github.com/oliver006/redis_exporter/releases/download/v1.26.2/redis_exporter-1.26.2.linux-amd64.tar.gz && tar xvf redis_exporter-1.26.2.linux-amd64.tar.gz;cd redis_exporter-1.26.2.linux-amd64 && ./redis_exporter(默认监听9121端口)。/etc/prometheus/prometheus.yml,添加Redis监控任务:scrape_configs:
- job_name: 'redis'
static_configs:
- targets: ['localhost:9121'] # 替换为redis_exporter的实际地址
重启Prometheus:sudo systemctl restart prometheus。http://localhost:3000),进入“Configuration→Data Sources”,输入Prometheus URL(如http://localhost:9090)并保存;763,官方提供),即可查看内存、QPS、延迟等可视化图表。以上工具覆盖了从基础检查到专业监控的需求,可根据实际场景选择使用。生产环境中建议结合自带命令行工具(快速排查问题)与Prometheus+Grafana(长期监控与可视化)实现全面监控。