# 使用 stats

URL: https://caijiao.org/memcached/06-monitoring/01-stats
Source: docs/memcached/06-monitoring/01-stats.md
Description: 本章节介绍如何使用 Memcached 的 'stats' 命令监控服务器运行状态，包括内存使用、命中率和连接数等重要指标，帮助运维和开发优化缓存性能。

Memcached 提供 `stats` 命令，输出服务器运行的详细统计信息，便于监控和调优。

## 常用 stats 类型

- `stats`: 显示基本统计数据，如命中次数、存储项数、内存使用等。  
- `stats settings`: 显示当前服务器配置参数。  
- `stats items`: 显示缓存条目分布情况。  
- `stats slabs`: 显示内存分配细节。

## 示例

使用 telnet 连接 Memcached：

```bash
telnet 127.0.0.1 11211
stats
````

返回示例：

```bash
STAT pid 12345
STAT uptime 3600
STAT curr_items 1000
STAT total_items 5000
STAT bytes 1048576
STAT curr_connections 50
STAT cmd_get 20000
STAT get_hits 15000
STAT get_misses 5000
END
```

## 关键指标说明

* `get_hits` 和 `get_misses`：缓存命中与未命中次数。
* `curr_items`：当前缓存项数量。
* `bytes`：当前使用的内存字节数。
* `curr_connections`：当前连接数。
* `uptime`：服务器运行时间（秒）。

## 运用

结合 `stats` 数据，可以分析缓存命中率、内存使用和连接负载，调整配置提升性能。
