linux下redis常用命令
linux下redis常用命令详细介绍
在 Linux 环境里,Redis 有许多常用命令,下面为你分类介绍:
服务器操作
- 启动 Redis 服务器:bash
redis-server若要使用自定义配置文件启动,可使用如下命令:bash
redis-server /path/to/redis.conf - 停止 Redis 服务器:bash
redis-cli - 检查 Redis 服务器状态:bash
redis-cli若服务器正常运行,会返回
PONG。
键值操作
- 设置键值对:bash
redis-cli key value - 获取键的值:bash
redis-cli get key - 检查键是否存在:bash
redis-cli exists key若键存在,返回
1;若不存在,返回0。 - 删除键:bash
redis-cli del key - 设置键的过期时间(单位:秒):bash
redis-cli expire key seconds
数据结构操作
字符串
- 追加字符串到键的值后面:bash
redis-cli append key value - 获取字符串长度:bash
redis-cli strlen key
哈希
- 设置哈希字段的值:bash
redis-cli hset hash_key field value - 获取哈希字段的值:bash
redis-cli hget hash_key field - 获取哈希所有字段和值:bash
redis-cli hgetall hash_key
列表
- 在列表头部插入元素:bash
redis-cli lpush list_key value - 在列表尾部插入元素:bash
redis-cli rpush list_key value - 获取列表指定范围的元素:bash
redis-cli lrange list_key start stop
集合
- 向集合添加元素:bash
redis-cli sadd set_key value - 获取集合所有元素:bash
redis-cli smembers set_key
有序集合
- 向有序集合添加元素并设置分数:bash
redis-cli zadd sorted_set_key score value - 获取有序集合指定分数范围的元素:bash
redis-cli zrangebyscore sorted_set_key min max
其他操作
- 查看所有键:bash
redis-cli keys *此命令在生产环境需谨慎使用,因为它会阻塞服务器。
- 清空当前数据库:bash
redis-cli flushdb - 清空所有数据库:bash
redis-cli flushall912redis-cli flushall
这些是 Redis 的常用命令,能满足大部分基本操作需求。