在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(長期監控與可視化)實現全面監控。