Redis CLI命令行工具 #

一、连接Redis #

1.1 基本连接 #

bash
# 连接本地Redis
redis-cli

# 连接指定主机和端口
redis-cli -h 192.168.1.100 -p 6379

# 使用密码连接
redis-cli -h 192.168.1.100 -p 6379 -a yourpassword

# 使用URL连接
redis-cli -u redis://user:password@192.168.1.100:6379/0

1.2 连接选项 #

bash
# -h: 主机地址
redis-cli -h 127.0.0.1

# -p: 端口号
redis-cli -p 6379

# -a: 密码
redis-cli -a yourpassword

# -n: 数据库编号
redis-cli -n 1

# -u: 连接URL
redis-cli -u redis://127.0.0.1:6379/0

# -s: Unix socket
redis-cli -s /tmp/redis.sock

# --raw: 原始输出
redis-cli --raw

# --no-raw: 格式化输出
redis-cli --no-raw

# --csv: CSV格式输出
redis-cli --csv

1.3 交互模式 #

bash
# 进入交互模式
redis-cli

# 交互模式下的操作
127.0.0.1:6379> ping
PONG

127.0.0.1:6379> set name "John"
OK

127.0.0.1:6379> get name
"John"

# 切换数据库
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]>

# 查看当前数据库
127.0.0.1:6379> client list

# 退出
127.0.0.1:6379> exit
或
127.0.0.1:6379> quit

二、命令执行方式 #

2.1 单次执行 #

bash
# 直接执行命令
redis-cli ping
# PONG

redis-cli set name "John"
# OK

redis-cli get name
# "John"

# 执行多个命令
redis-cli -e "set a 1; get a"

2.2 管道输入 #

bash
# 使用echo和管道
echo "set name John" | redis-cli
# OK

# 使用here document
redis-cli << EOF
set name John
set age 25
get name
get age
EOF

# 从文件读取命令
cat commands.txt | redis-cli

# commands.txt内容:
# set name John
# set age 25
# get name

2.3 批量执行 #

bash
# 使用-x选项从stdin读取
echo "John" | redis-cli -x set name
# OK

# 批量设置
cat data.txt | redis-cli --pipe

三、常用命令 #

3.1 服务器信息 #

bash
# 查看服务器信息
redis-cli info

# 查看特定信息
redis-cli info memory
redis-cli info stats
redis-cli info replication
redis-cli info cpu

# 查看服务器统计
redis-cli --stat

# 实时监控
redis-cli monitor

3.2 客户端管理 #

bash
# 查看客户端列表
redis-cli client list

# 查看客户端ID
redis-cli client id

# 设置客户端名称
redis-cli client setname myapp

# 获取客户端名称
redis-cli client getname

# 关闭客户端连接
redis-cli client kill 127.0.0.1:52341

# 关闭所有客户端
redis-cli client kill all

3.3 数据库操作 #

bash
# 查看数据库大小
redis-cli dbsize

# 清空当前数据库
redis-cli flushdb

# 清空所有数据库
redis-cli flushall

# 保存数据到磁盘
redis-cli save

# 后台保存
redis-cli bgsave

# 查看最后保存时间
redis-cli lastsave

3.4 配置管理 #

bash
# 查看所有配置
redis-cli config get *

# 查看特定配置
redis-cli config get maxmemory
redis-cli config get timeout

# 设置配置
redis-cli config set maxmemory 2gb
redis-cli config set timeout 300

# 重置统计信息
redis-cli config resetstat

# 重写配置文件
redis-cli config rewrite

四、数据操作 #

4.1 字符串操作 #

bash
# 设置值
redis-cli set name "John"
redis-cli setex session 3600 "data"  # 带过期时间
redis-cli setnx name "John"          # 不存在时设置

# 获取值
redis-cli get name

# 批量操作
redis-cli mset a 1 b 2 c 3
redis-cli mget a b c

# 自增自减
redis-cli incr counter
redis-cli incrby counter 10
redis-cli decr counter
redis-cli decrby counter 5

# 追加
redis-cli append name " Doe"

# 获取长度
redis-cli strlen name

4.2 哈希操作 #

bash
# 设置字段
redis-cli hset user:1001 name "John"
redis-cli hset user:1001 age 25

# 获取字段
redis-cli hget user:1001 name
redis-cli hgetall user:1001

# 批量设置
redis-cli hmset user:1001 name "John" age 25 email "john@example.com"

# 批量获取
redis-cli hmget user:1001 name age email

# 删除字段
redis-cli hdel user:1001 email

# 检查字段存在
redis-cli hexists user:1001 name

# 获取所有字段
redis-cli hkeys user:1001
redis-cli hvals user:1001

# 获取字段数量
redis-cli hlen user:1001

4.3 列表操作 #

bash
# 左侧插入
redis-cli lpush mylist "a" "b" "c"

# 右侧插入
redis-cli rpush mylist "d" "e"

# 获取列表元素
redis-cli lrange mylist 0 -1

# 获取指定位置元素
redis-cli lindex mylist 0

# 获取列表长度
redis-cli llen mylist

# 左侧弹出
redis-cli lpop mylist

# 右侧弹出
redis-cli rpop mylist

# 阻塞弹出
redis-cli blpop mylist 10  # 等待10秒

4.4 集合操作 #

bash
# 添加元素
redis-cli sadd myset "a" "b" "c"

# 获取所有元素
redis-cli smembers myset

# 检查元素是否存在
redis-cli sismember myset "a"

# 删除元素
redis-cli srem myset "a"

# 获取集合大小
redis-cli scard myset

# 随机获取元素
redis-cli srandmember myset

# 随机弹出元素
redis-cli spop myset

# 集合运算
redis-cli sinter set1 set2
redis-cli sunion set1 set2
redis-cli sdiff set1 set2

4.5 有序集合操作 #

bash
# 添加元素
redis-cli zadd myzset 1 "one" 2 "two" 3 "three"

# 获取元素分数
redis-cli zscore myzset "one"

# 获取排名
redis-cli zrank myzset "one"

# 获取范围
redis-cli zrange myzset 0 -1
redis-cli zrange myzset 0 -1 withscores

# 获取分数范围
redis-cli zrangebyscore myzset 1 2

# 删除元素
redis-cli zrem myzset "one"

# 获取集合大小
redis-cli zcard myzset

五、高级功能 #

5.1 管道模式 #

bash
# 启用管道模式
redis-cli --pipe

# 示例:批量插入
cat << EOF | redis-cli --pipe
SET key1 value1
SET key2 value2
SET key3 value3
EOF

# 从文件批量导入
cat data.txt | redis-cli --pipe

# 输出示例:
# All data transferred. Waiting for the last reply...
# Last reply received from server.
# errors: 0, replies: 3

5.2 模式扫描 #

bash
# 扫描键
redis-cli scan 0

# 带模式扫描
redis-cli scan 0 match user:* count 100

# 迭代扫描
redis-cli --scan --pattern "user:*"

# 扫描哈希字段
redis-cli hscan myhash 0

# 扫描集合
redis-cli sscan myset 0

# 扫描有序集合
redis-cli zscan myzset 0

5.3 大键扫描 #

bash
# 查找大键
redis-cli --bigkeys

# 输出示例:
# Scanning the entire keyspace to find biggest keys as well as
# average sizes per key type.
# 
# Biggest string found 'key' has 1000 bytes
# Biggest list found 'mylist' has 500 items
# Biggest set found 'myset' has 300 members
# Biggest hash found 'myhash' has 200 fields
# Biggest zset found 'myzset' has 100 members

# 查找内存占用大的键
redis-cli --memkeys
redis-cli --memkeys-samples 1000

5.4 延迟监控 #

bash
# 测试延迟
redis-cli --latency

# 输出示例:
# min: 0, max: 1, avg: 0.10 (96 samples)

# 延迟历史
redis-cli --latency-history

# 延迟分布图
redis-cli --latency-dist

# 内部延迟
redis-cli intrinsic-latency 10

5.5 从节点模式 #

bash
# 从节点模式
redis-cli --slave

# 模拟从节点接收主节点数据
# 输出所有写操作命令

5.6 RDB导出 #

bash
# 导出RDB文件
redis-cli --rdb dump.rdb

# 导出并检查
redis-cli --rdb dump.rdb --pipe

六、调试和诊断 #

6.1 慢查询日志 #

bash
# 查看慢查询日志
redis-cli slowlog get

# 查看指定数量
redis-cli slowlog get 10

# 获取慢查询长度
redis-cli slowlog len

# 重置慢查询日志
redis-cli slowlog reset

6.2 调试命令 #

bash
# 调试对象
redis-cli debug object mykey

# 调试段错误(危险)
redis-cli debug segfault

# 查看内存使用
redis-cli memory usage mykey

# 分析内存
redis-cli memory malloc-stats
redis-cli memory doctor

6.3 性能分析 #

bash
# 性能测试
redis-benchmark

# 测试特定命令
redis-benchmark -t set,get -n 100000 -q

# 测试特定大小
redis-benchmark -d 1000 -n 100000 -q

# 测试管道
redis-benchmark -P 10 -n 100000 -q

七、集群操作 #

7.1 集群信息 #

bash
# 查看集群信息
redis-cli cluster info

# 查看集群节点
redis-cli cluster nodes

# 查看槽位分配
redis-cli cluster slots

7.2 集群操作 #

bash
# 创建集群
redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002

# 添加节点
redis-cli --cluster add-node 127.0.0.1:7003 127.0.0.1:7000

# 删除节点
redis-cli --cluster del-node 127.0.0.1:7000 <node-id>

# 重新分片
redis-cli --cluster reshard 127.0.0.1:7000

# 检查集群
redis-cli --cluster check 127.0.0.1:7000

# 集群模式连接
redis-cli -c -h 127.0.0.1 -p 7000

八、实用技巧 #

8.1 输出格式化 #

bash
# 原始输出
redis-cli --raw get name
# John

# JSON格式(需要jq)
redis-cli hgetall user:1001 | jq -n 'inputs | split("\n") | . as $a | reduce range(0; length/2) as $i ({}; . + {($a[2*$i]): ($a[2*$i+1])})'

# CSV输出
redis-cli --csv lrange mylist 0 -1

8.2 批量删除 #

bash
# 删除匹配的键(慎用)
redis-cli --scan --pattern "temp:*" | xargs redis-cli del

# 使用Lua脚本删除
redis-cli eval "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 "temp:*"

8.3 数据迁移 #

bash
# 导出数据
redis-cli --rdb dump.rdb

# 导入数据
cat dump.rdb | redis-cli --pipe

# 迁移键
redis-cli migrate 192.168.1.100 6379 mykey 0 1000

九、总结 #

常用连接选项:

选项 说明
-h 主机地址
-p 端口号
-a 密码
-n 数据库编号
-c 集群模式

常用操作命令:

命令 说明
info 查看服务器信息
monitor 实时监控
–stat 统计信息
–bigkeys 查找大键
–latency 延迟测试

下一步,让我们学习Redis的数据结构!

最后更新:2026-03-27