当前位置:首页>Linux>Linux 部署 Memcached 及基础命令指南

Linux 部署 Memcached 及基础命令指南

  • 2026-08-23 03:06:28
Linux 部署 Memcached 及基础命令指南

1. 安装 Memcached

Ubuntu/Debian 系统

# 更新包列表
sudo apt update

# 安装 Memcached
sudo apt install memcached -y

# 安装 libmemcached-tools(客户端工具)
sudo apt install libmemcached-tools -y

# 查看版本
memcached --version

CentOS/RHEL 系统

# CentOS 7/8
sudo yum install memcached libmemcached -y

# 或者使用 EPEL 仓库
sudo yum install epel-release
sudo yum install memcached libmemcached -y

# CentOS 8 还可以使用 dnf
sudo dnf install memcached libmemcached -y

2. 配置 Memcached

查看默认配置文件

# Ubuntu/Debian
cat /etc/memcached.conf

# CentOS/RHEL
cat /etc/sysconfig/memcached

修改配置

# Ubuntu/Debian - 编辑配置文件
sudo nano /etc/memcached.conf

# CentOS/RHEL - 编辑配置文件
sudo nano /etc/sysconfig/memcached

常用配置参数

# 监听的IP地址(0.0.0.0 表示所有接口)
-l127.0.0.1

# 监听的端口(默认11211)
-p11211

# 运行用户
-u memcache

# 最大连接数
-c1024

# 最大内存使用(MB)
-m64

# 日志文件路径
logfile /var/log/memcached.log

3. 管理 Memcached 服务

启动/停止/重启服务

# Ubuntu/Debian(使用 systemd)
sudo systemctl start memcached
sudo systemctl stop memcached
sudo systemctl restart memcached
sudo systemctl status memcached

# CentOS/RHEL
sudo systemctl start memcached
sudo systemctl enable memcached  # 开机自启
sudo systemctl status memcached

查看服务状态

# 查看进程
ps aux | grep memcached

# 查看端口监听
netstat -tlnp | grep memcached
# 或者使用 ss
ss -tlnp | grep memcached

# 查看系统服务状态
systemctl status memcached

4. 基础命令使用

telnet 客户端操作

# 连接到 Memcached
telnet localhost 11211

# 在 telnet 会话中的基本操作
set key 0360010# 存储键值,0=标志位,3600=过期时间(秒),10=数据长度
example_data         # 输入数据
get key              # 获取键值
delete key           # 删除键值
flush_all            # 清空所有缓存
stats                # 查看统计信息
stats items          # 查看项目统计
stats slabs          # 查看slab统计
quit                 # 退出

使用 memcached-tool

# 查看统计信息
memcached-tool localhost:11211 stats

# 查看内存使用情况
memcached-tool localhost:11211 dump

# 查看设置
memcached-tool localhost:11211 settings

使用 libmemcached 工具

# 设置值
echo"set mykey 0 0 11" | nc localhost 11211
echo"hello world" | nc localhost 11211

# 获取值
echo"get mykey" | nc localhost 11211

# 删除值
echo"delete mykey" | nc localhost 11211

5. 常用操作示例

存储数据

# 使用 printf 和 nc
printf "set username 0 3600 6\r\nalice\r\n" | nc localhost 11211
# 响应:STORED

# 设置多个标志位(16进制)
printf "set user:1001 0x10 3600 5\r\nvalue\r\n" | nc localhost 11211

获取数据

# 获取单个键
printf "get username\r\n" | nc localhost 11211

# 获取多个键
printf "get username user:1001\r\n" | nc localhost 11211

# 带 CAS 值的获取
printf "gets username\r\n" | nc localhost 11211

更新和删除

# 替换已存在的键
printf "replace username 0 1800 3\r\nbob\r\n" | nc localhost 11211

# 追加数据
printf "append username 0 0 4\r\n_old\r\n" | nc localhost 11211

# 前置数据
printf "prepend username 0 0 3\r\nnew_\r\n" | nc localhost 11211

# 递增/递减(针对数值)
printf "incr counter 5\r\n" | nc localhost 11211
printf "decr counter 2\r\n" | nc localhost 11211

CAS(检查并设置)操作

# 1. 获取数据及CAS值
printf "gets mykey\r\n" | nc localhost 11211
# 响应:VALUE mykey 0 11 15
#        hello world
# END

# 2. 使用CAS值更新(15是上面返回的CAS值)
printf "cas mykey 0 0 11 15\r\nnew value!\r\n" | nc localhost 11211

6. 监控和统计

查看详细统计

# 连接到 memcached 后使用 stats 命令
echo"stats" | nc localhost 11211

# 重要统计指标解读:
# bytes:当前存储的字节数
# curr_items:当前存储的项目数
# cmd_get:get命令总次数
# cmd_set:set命令总次数
# get_hits:命中次数
# get_misses:未命中次数
# evictions:因内存不足被移除的项目数
# bytes_read:读取的总字节数
# bytes_written:写入的总字节数

查看 slabs 信息

echo"stats slabs" | nc localhost 11211
echo"stats items" | nc localhost 11211

7. 安全配置建议

绑定特定IP

# 在配置文件中修改
-l192.168.1.100  # 只绑定到内网IP

使用防火墙

# 只允许特定IP访问
sudo ufw allow from 192.168.1.0/24 to any port 11211

# CentOS 使用 firewalld
sudo firewall-cmd --permanent--add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="11211" accept'
sudo firewall-cmd --reload

8. 故障排查

查看日志

# Ubuntu/Debian
tail -f /var/log/memcached.log

# CentOS/RHEL
journalctl -u memcached -f

连接测试

# 测试连接
nc-zv localhost 11211

# 测试基本功能
timeout 2bash-c"cat <(echo stats) <(sleep 1) | nc localhost 11211"

性能测试

# 使用 memslap 工具(需要安装)
sudo apt install libmemcached-tools  # Ubuntu
memslap --servers=localhost --concurrency=10--execute-number=1000

最新文章

随机文章