Memcached stats 命令

Memcached stats 命令详解(2025 版)

stats 是 Memcached 的 核心诊断命令,用于 实时查看运行状态、性能指标、内存使用、命中率等,是 运维监控的必备工具


1. 基本语法

stats [items|slabs|sizes|cachedump|reset|...]\r\n
子命令说明
stats默认通用统计
stats items按 slab 分项统计
stats slabs详细 slab 内存分配
stats sizes各 size 区间 item 分布
stats cachedump <slab_id> <limit>查看 slab 中 key 列表
stats reset重置部分统计项

2. stats 通用统计(最常用)

echo "stats" | nc 127.0.0.1 11211

核心字段解析(精选 20+ 关键指标)

字段说明推荐关注
pid进程 ID排查重启
uptime运行时间(秒)稳定性
time当前 Unix 时间戳时间同步
versionMemcached 版本升级检查
curr_connections当前连接数连接池
total_connections历史总连接数峰值估算
connection_structures分配的连接结构内存泄漏
cmd_getget 次数请求量
cmd_setset 次数写入量
get_hits缓存命中次数命中率
get_misses缓存未命中次数回源量
evictionsLRU 淘汰次数内存紧张
bytes当前已用内存内存使用
limit_maxbytes内存上限(-m 参数)配置核对
curr_items当前 item 数数据量
total_items历史写入 item 数写入频率
bytes_read累计读取字节流量
bytes_written累计写入字节流量
rusage_user用户态 CPU 时间性能
rusage_system内核态 CPU 时间性能

命中率计算

# 实时命中率
hits=$(echo "stats" | nc 127.0.0.1 11211 | grep get_hits | awk '{print $3}')
total=$(echo "stats" | nc 127.0.0.1 11211 | awk '/cmd_get/ {print $3}')
echo "scale=2; $hits * 100 / $total" | bc

目标命中率 > 90%


3. stats items — 按 slab 统计

echo "stats items" | nc 127.0.0.1 11211

输出示例

STAT items:1:number 150
STAT items:1:age 300
STAT items:1:evicted 0
STAT items:2:number 80
...
字段说明
items:<id>:number该 slab 当前 item 数
items:<id>:age最老 item 年龄(秒)
items:<id>:evicted淘汰次数

4. stats slabs — 内存分配详情

echo "stats slabs" | nc 127.0.0.1 11211

关键输出

STAT 1:chunk_size 104
STAT 1:chunks_per_page 10083
STAT 1:total_pages 1
STAT 1:total_chunks 10083
STAT 1:used_chunks 150
STAT 1:free_chunks 9933
STAT 1:mem_requested 14800
...
STAT active_slabs 5
STAT total_malloced 5242880
字段说明
chunk_size该 slab 块大小(字节)
used_chunks已用块数
mem_requested实际存储数据字节
active_slabs活跃 slab 类数

5. stats sizes — item 大小分布(调试内存碎片)

echo "stats sizes" | nc 127.0.0.1 11211

输出

STAT 96 50
STAT 120 200
STAT 400 10

格式:size countsize 字节有 count 个 item


6. stats cachedump — 查看 key 列表(调试用)

# 查看 slab 1 中前 10 个 key
echo "stats cachedump 1 10" | nc 127.0.0.1 11211

输出

ITEM user:1001 [13 b; 1739431200 s]
ITEM config:db [9 b; 0 s]
END

用途:热点 key 分析、缓存穿透排查


7. 客户端调用 stats

Python(pymemcache)

from pymemcache.client.base import Client

client = Client(('127.0.0.1', 11211))

stats = client.stats()
print(stats[b'uptime'])  # 运行时间
print(stats[b'get_hits'])  # 命中次数

# 高级 stats
slabs = client.stats('slabs')
items = client.stats('items')

PHP

$mc = new Memcached();
$mc->addServer('127.0.0.1', 11211);

$stats = $mc->getStats();
print_r($stats);

$slabs = $mc->getExtendedStats('slabs');

8. 监控脚本(实时告警)

#!/bin/bash
# save as memcached_monitor.sh

HOST="127.0.0.1"
PORT="11211"

stats=$(echo "stats" | nc $HOST $PORT)

# 提取关键指标
uptime=$(echo "$stats" | grep uptime | awk '{print $3}')
evictions=$(echo "$stats" | grep evictions | awk '{print $3}')
hits=$(echo "$stats" | grep get_hits | awk '{print $3}')
misses=$(echo "$stats" | grep get_misses | awk '{print $3}')
bytes=$(echo "$stats" | grep bytes | awk '{print $3}')
limit=$(echo "$stats" | grep limit_maxbytes | awk '{print $3}')

hit_rate=$(echo "scale=2; $hits*100/($hits+$misses)" | bc 2>/dev/null || echo "0")

echo "=== Memcached 监控 ==="
echo "运行时间: $uptime 秒"
echo "内存使用: $((bytes/1024/1024)) MB / $((limit/1024/1024)) MB"
echo "命中率: $hit_rate%"
echo "淘汰次数: $evictions"

# 告警
if (( evictions > 100 )); then
    echo "告警: 内存淘汰严重!"
fi
if (( $(echo "$hit_rate < 80" | bc -l) )); then
    echo "告警: 命中率过低!"
fi

9. Prometheus 监控(推荐)

使用 memcached_exporter

# prometheus.yml
scrape_configs:
  - job_name: 'memcached'
    static_configs:
      - targets: ['127.0.0.1:9150']

启动 exporter:

docker run -d -p 9150:9150 prom/memcached-exporter -memcached.address=127.0.0.1:11211

10. 最佳实践

项目建议
监控频率每 10 秒采集一次
命中率告警< 85% 告警
evictions > 0立即扩容或优化
curr_connections接近 -c 上限时扩容
定期 stats reset避免统计溢出
# 重置统计(不影响缓存)
echo "stats reset" | nc 127.0.0.1 11211

11. 小结:stats 速查表

命令用途
stats通用状态
stats items按 slab 统计
stats slabs内存分配
stats sizes大小分布
stats cachedump 1 100查看 key
stats reset重置计数

练习建议

  1. 写脚本 实时显示命中率
  2. stats cachedump 找出 前 10 大 key
  3. 部署 Prometheus + Grafana 监控面板
  4. 模拟 内存满 → 淘汰 → 命中率下降

需要 flush_all 清理、touch 延长、version 升级?继续问我!

文章已创建 2481

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部