设为首页 收藏本站
查看: 843|回复: 0

[经验分享] Redis运维之常用命令操作

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-4-18 09:10:08 | 显示全部楼层 |阅读模式

在平时的工作中,需要根据需求对Redis数据库进行一些操作。

可以参考Redis官网http://redis.io/commands 进行详细了解

1.SELECT 切换数据库
1
2
3
4
5
6
7
8
9
redis 127.0.0.1:6379[1]> HELP SELECT

  SELECT index
  summary: Change the selected database for the current connection
  since: 1.0.0
  group: connection

redis 127.0.0.1:6379[1]> SELECT 2
OK



2.LLEN 得到一个列表的长度
1
2
3
4
5
6
7
8
9
redis 127.0.0.1:6379[2]> HELP LLEN

  LLEN key
  summary: Get the length of a list
  since: 1.0.0
  group: list

redis 127.0.0.1:6379[2]> LLEN bi
(integer) 412




3.LRANGE 获取一个列表的所有元素

    LRANGE 索引是以0开始的,0表示第一个元素,-1表示最后一个元素

1
2
3
4
5
6
  LRANGE key start stop
  summary: Get a range of elements from a list
  since: 1.0.0
  group: list

redis 127.0.0.1:6379[2]> LRANGE bi 0 5





4.LPUSH 将一个或多个值添加到一个列表的开头
1
2
3
4
5
6
7
8
redis 127.0.0.1:6379[2]> HELP LPUSH

  LPUSH key value [value ...]
  summary: Prepend one or multiple values to a list
  since: 1.0.0
  group: list

redis 127.0.0.1:6379[2]> LPUSH bi http://abc.com/logUserLogin?even ... 21bcef5cba1fc182d18




5.RPUSH 将一个或多个值追加到一个列表的末尾
1
2
3
4
5
6
7
8
redis 127.0.0.1:6379[2]> HELP RPUSH

  RPUSH key value [value ...]
  summary: Append one or multiple values to a list
  since: 1.0.0
  group: list

redis 127.0.0.1:6379[2]> RPUSH bi http://abc.com/logUserLogin?even ... 21bcef5cba1fc182d18




6.SAVE 同步数据到磁盘

     SAVE命令执行的时候会阻塞连接,所以生成环境最好使用BGSAVE命令

1
2
3
4
5
6
7
8
9
10
redis 127.0.0.1:6379[2]> HELP SAVE

  SAVE -
  summary: Synchronously save the dataset to disk
  since: 1.0.0
  group: server

redis 127.0.0.1:6379[2]> SAVE
OK
(1.33s)




7.BGSAVE 异步数据到磁盘

    使用BGSAVE,Redis将会在后台执行保存数据的操作,不影响正常的客户端连接,Redis将会fork出一个子进程用于保存数据,父进程继续处理客户端请求。

1
2
3
4
5
6
7
8
9
redis 127.0.0.1:6379[2]> HELP BGSAVE

  BGSAVE -
  summary: Asynchronously save the dataset to disk
  since: 1.0.0
  group: server

redis 127.0.0.1:6379[2]> BGSAVE
Background saving started




8.TYPE 判断一个KEY的类型
1
2
3
4
5
6
7
8
9
redis 127.0.0.1:6379[2]> HELP TYPE

  TYPE key
  summary: Determine the type stored at key
  since: 1.0.0
  group: generic

redis 127.0.0.1:6379[2]> TYPE bi
list




9.BGREWRITEAOF   

异步重写AOF文件,Redis将会创建一个对当前AOF文件优化过的AOF版本。

1
2
3
4
5
6
redis 127.0.0.1:6379> help BGREWRITEAOF

  BGREWRITEAOF -
  summary: Asynchronously rewrite the append-only file
  since: 1.0.0
  group: server




10.CONFIG GET   

   获取某个配置项的值

1
2
3
4
5
6
7
8
9
10
redis 127.0.0.1:6379> help config get

  CONFIG GET parameter
  summary: Get the value of a configuration parameter
  since: 2.0.0
  group: server

redis 127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "0"




11.CONFIG SET

  设置某个参数的值

1
2
3
4
5
6
7
8
9
redis 127.0.0.1:6379> help config set

  CONFIG SET parameter value
  summary: Set a configuration parameter to the given value
  since: 2.0.0
  group: server

redis 127.0.0.1:6379> config set maxmemory 200000000
OK




12.DBSIZE

  返回当前数据库的KEY值得数量

1
2
3
4
5
6
7
8
9
redis 127.0.0.1:6379[3]> HELP DBSIZE

  DBSIZE -
  summary: Return the number of keys in the selected database
  since: 1.0.0
  group: server

redis 127.0.0.1:6379[3]> dbsize
(integer) 12502




13.DEL

删除一个KEY值

1
2
3
4
5
6
7
8
9
redis 127.0.0.1:6379> help del

  DEL key [key ...]
  summary: Delete a key
  since: 1.0.0
  group: generic

redis 127.0.0.1:6379> del foo
(integer) 1




14.EXISTS

检查一个KEY是否存在

1
2
3
4
5
6
7
8
9
redis 127.0.0.1:6379> help exists

  EXISTS key
  summary: Determine if a key exists
  since: 1.0.0
  group: generic

redis 127.0.0.1:6379> exists foo
(integer) 1




15.SET 命令

设置一个KEY的值

1
2
3
4
5
6
7
8
9
10
redis 127.0.0.1:6379> help set

  SET key value
  summary: Set the string value of a key
  since: 1.0.0
  group: string

redis 127.0.0.1:6379> set foo test
OK
redis 127.0.0.1:6379>




16.PERSIST

删除一个KEY的过期时间

1
2
3
4
5
6
edis 127.0.0.1:6379> help persist

  PERSIST key
  summary: Remove the expiration from a key
  since: 2.2.0
  group: generic




17.RENAME

重新命名一个KEY

1
2
3
4
5
6
7
8
9
10
redis 127.0.0.1:6379> help rename

  RENAME key newkey
  summary: Rename a key
  since: 1.0.0
  group: generic

redis 127.0.0.1:6379> rename foo footest
OK
redis 127.0.0.1:6379>




18.EXPIRE

为一个KEY设置一个TTL过期时间

1
2
3
4
5
6
7
8
9
redis 127.0.0.1:6379> help expire

  EXPIRE key seconds
  summary: Set a key's time to live in seconds
  since: 1.0.0
  group: generic

redis 127.0.0.1:6379> expire footest 300
(integer) 1




19.TTL

获取过期时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
redis 127.0.0.1:6379> help ttl

  TTL key
  summary: Get the time to live for a key
  since: 1.0.0
  group: generic

redis 127.0.0.1:6379> ttl footest
(integer) 289
redis 127.0.0.1:6379> ttl footest
(integer) 285
redis 127.0.0.1:6379> ttl footest
(integer) 283
redis 127.0.0.1:6379> ttl footest
(integer) 282
redis 127.0.0.1:6379> ttl footest
(integer) 282
redis 127.0.0.1:6379>




20.EXPIREAT

设置一个KEY的过期时间,以UNIX时间戳表示

1
2
3
4
5
6
7
8
9
10
11
redis 127.0.0.1:6379> help expireat

  EXPIREAT key timestamp
  summary: Set the expiration for a key as a UNIX timestamp
  since: 1.2.0
  group: generic

redis 127.0.0.1:6379> expireat foo 1431532800
(integer) 1
redis 127.0.0.1:6379> ttl foo
(integer) 3210141




21.GET

获取一个KEY的值

1
2
3
4
5
6
7
8
9
redis 127.0.0.1:6379> help get

  GET key
  summary: Get the value of a key
  since: 1.0.0
  group: string

redis 127.0.0.1:6379> get foo
"test"




22.HGET

获取一个哈希字段的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
redis 127.0.0.1:6379> help hget

  HGET key field
  summary: Get the value of a hash field
  since: 2.0.0
  group: hash

redis 127.0.0.1:6379> hset myhash field1 "foo"
(integer) 1
redis 127.0.0.1:6379> hget myhash field1
"foo"
redis 127.0.0.1:6379> hget myhash field2
(nil)
redis 127.0.0.1:6379>




23.LASTSAVE

上次成功保存数据到磁盘的UNIX时间戳

1
2
3
4
5
6
7
8
9
10
redis 127.0.0.1:6379> help lastsave

  LASTSAVE -
  summary: Get the UNIX time stamp of the last successful save to disk
  since: 1.0.0
  group: server

redis 127.0.0.1:6379> lastsave
(integer) 1428373205
redis 127.0.0.1:6379>




24.LPUSH

将一个或多个值附加到一个Redis列表中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
redis 127.0.0.1:6379> help lpush

  LPUSH key value [value ...]
  summary: Prepend one or multiple values to a list
  since: 1.0.0
  group: list



redis 127.0.0.1:6379> lpush mylist a b c
(integer) 6
redis 127.0.0.1:6379> LRANGE mylist  0 -1
1) "c"
2) "b"
3) "a"
4) "c"
5) "b"
6) "a"
redis 127.0.0.1:6379> llen mylist
(integer) 6



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-205382-1-1.html 上篇帖子: 分布式redis服务:codis 下篇帖子: Redis安装
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表