redis常用命令

一,配置说明redis-6.2.6(linux)

1.在虚拟机上安装好redis

2.将opt/redis-6.2.6下的redis.conf拷贝到usr/local/bin的yconfig内(yconfig文件提前自己建好)当前目录为usr/local/bin

在这里插入图片描述

cp /opt/redis-6.2.6/redis.conf yconfig

以后使用这个配置文件进行启动,保证安全

3.因为redis默认不是后台启动文件,所以进入配置文件进行修改

在这里插入图片描述

cd yconfg
vim redis.conf

找到daemonize将no改为yes(按i进入编辑模式)

在这里插入图片描述
再退出(Esc+:+wq)

4.启动redis服务

回到bin目录

cd /usr/local/bin

通过刚刚拷贝的配置文件启动服务

redis-server yconfig/redis.conf

注:若进程占用,则先查找进程再删除

ps -ef|grep redis
kill -9 [id号]

在这里插入图片描述

链接默认端口号

redis-cli -p 6379

ping一下,显示pong则连接成功

在这里插入图片描述

二,常用命令

1.连接常用命令

  • ps -ef|grep redis:查看redis进程是否开启
    在这里插入图片描述
  • shutdown:关闭redis连接
    在这里插入图片描述

2.String(字符串)常用命令

  • set key值 value值 :设置键值对
    在这里插入图片描述
  • get key值:获取键值对
    在这里插入图片描述
  • keys * :查询所有键值对
    在这里插入图片描述
  • exists key值:判断键值对是否存在(存在返回1,不存在返回0)
    在这里插入图片描述
  • move key值 当前数据库编号:删除当前库中的某个键值对
    在这里插入图片描述
  • expire key值 时间(秒数):设置键值对过期时间
  • ttl key值:查看当前key值剩余时间
    在这里插入图片描述
  • type key值:查看数据类型
    在这里插入图片描述
  • flushdb:清空当前数据库
  • flushall:清空所有数据库
  • select 数据库编号:切换数据库
  • append key值 value:在键值对后面追加数据
    在这里插入图片描述
  • strlen key值:获取长度
    在这里插入图片描述
  • incr key值:自增
    在这里插入图片描述
  • decr key值:自减
    在这里插入图片描述
  • incrby key值 步长:自增一定量
    在这里插入图片描述
  • getrange 起始底标 终止底标:截取值
    在这里插入图片描述
  • setrange key值 起始坐标 替换值:替换指定位置内容
    在这里插入图片描述
  • setex key值 时间 value值:设置一个值并且设置过期时间
    在这里插入图片描述
  • setnx key值 value值:若当前key存在则设置成功,不存在则设置失败
    在这里插入图片描述
  • mset key值 value值 key值 value值…:批量设置键值对
    在这里插入图片描述
  • mget key值 key值:批量获取键值对
    在这里插入图片描述

3.list(列表)常用命令

  • lpush key值 value值:左插入
  • rpush key值 value值:右插入
  • lrange key值 起始下标 终止下标:获取指定的长度
    在这里插入图片描述
  • lpop key值 value值:左移除
  • rpop key值 value值:右移除
  • lindex key值 下标:通过下标获取值
    在这里插入图片描述
  • lrem key值 数量 value值:移除指定数量的指定值
    在这里插入图片描述
  • ltrim key值 起始下标 终止下标:截取指定的长度
    在这里插入图片描述
  • lset key值 下标值 value值:替换指定下标值- linsert 可以值 before/after 被插入value值 插入的value值:在某个值前面(后面)插入值
    在这里插入图片描述

4.set(集合)常用命令

  • sadd key值 value值:添加值
  • smembers key值:查看值
    在这里插入图片描述
  • sismember value值:查询值是否存在
    在这里插入图片描述
  • scart key值:查询集合中元素的个数
    在这里插入图片描述
  • srem key值 value值:移除指定值
    在这里插入图片描述
  • srandmember key值 数量:随机获取指定数量的值
    在这里插入图片描述
  • spop key值:随机删除一些集合中的元素
  • smove key1值 key2值 value值:将key1中的value转入key2中
    在这里插入图片描述
  • sdiff key1值 key2值:获取key1与key2不同的值(差集)
  • sinter key1值 key2值:获取key1与key2相同的值(交集)
  • sunion key1值 key2值:获取key1与key2的并集
    在这里插入图片描述

5.Hash(哈希)常用命令

  • hset hash值 key值 value值:添加值
  • hget hash值 key值:获取值
  • hmset hash值 key1值 value1值 key2值 value2值…:批量添加值
  • hmget hash值 key1值 key2值…:批量获取值
  • hgetall hash值:获取hash的所有值
    在这里插入图片描述
  • hdel hash值 key值:删除值
    在这里插入图片描述
  • hlen hash值:获取hash的字段数量
    在这里插入图片描述
  • hexists hash值 key值:判单指定hash中key值是否存在
    在这里插入图片描述
  • hkeys hash值:获取hash的所有key值- hvals hash值:获取hash的所有value值
    在这里插入图片描述
  • hincreby hash值 key值 步长:自增
    在这里插入图片描述

6.Zset(有序集合)常用命令

  • zadd key值 index排序值 value值:添加

  • zadd key值 index排序值 value值 index排序值 value值…:添加多个

  • zrange key值 起始下标 终止下标:查询

  • zrangebyscore key值 -inf(min) +inf(max) :按照index排序从小到大遍历此范围
    在这里插入图片描述

  • zrevrangebyscore key值 +inf(max) -inf(min):按照index排序从大到小遍历此范围
    在这里插入图片描述

  • zrangebyscore key值 -inf +inf :按照index排序从小到大遍历并且附上index值
    在这里插入图片描述

  • zrem key值 value值:移除在这里插入图片描述

  • zcard key值:查询数量
    在这里插入图片描述

  • zcount key值 (min) (max):获取范围内的数量
    在这里插入图片描述

7.geospatial(地理位置)常用命令

  • geoadd key值 经度 维度 value值 经度 维度 value值…:添加
  • geopos key值 value值:获取指定的经纬度
    在这里插入图片描述
  • geodist key值 value值1 value值2 (单位):查询两地之间的距离
    • m:米
    • km:千米
    • mi:英里
    • ft:英尺
      在这里插入图片描述
  • georadius key值 经度 维度 半径 单位 (withdist[显示距离]) (withcoord[显示经纬度]) (count 数字[显示的数量]):以此经纬度为中心在半径圆内搜索指定数量的地址
    在这里插入图片描述
  • georadiusbymember key值 value值 半径 单位:以value为圆心寻找半径内的地址在这里插入图片描述

8.Hyperloglog(基数)

  • pfadd key值 value值 value值…:添加
  • pfcount key值:统计数量
  • pfmerge key3 key1 key2:整合key1 key2到key3(基数)
    在这里插入图片描述

9.Bitmaps(位:两个状态的)

例子:统计一周七天的打卡情况

  • setbit key值 date value:添加
  • getbit key值 date:查询date对应的值
    在这里插入图片描述
  • bitcount key值 (start end):统计所有状态为1的数量
    在这里插入图片描述

10.事务

  • multi:开启事务
  • exec:执行事务
  • discard:取消事务

其他命令杂记

  • 修改名字
hostname [名字]

在这里插入图片描述
再重开个窗口,发现修改成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值