Redis命令使用示例(一)

作者 : admin 本文共4983个字,预计阅读时间需要13分钟 发布时间: 2024-06-9 共2人阅读

1 检查所有可用类别

> ACL CAT
 1) "keyspace"
 2) "read"
 3) "write"
 4) "set"
 5) "sortedset"
 6) "list"
 7) "hash"
 8) "string"
 9) "bitmap"
10) "hyperloglog"
11) "geo"
12) "stream"
13) "pubsub"
14) "admin"
15) "fast"
16) "slow"
17) "blocking"
18) "dangerous"
19) "connection"
20) "transaction"
21) "scripting"

2 检查哪些命令是给定类别的一部分

> ACL CAT dangerous
 1) "flushdb"
 2) "acl"
 3) "slowlog"
 4) "debug"
 5) "role"
 6) "keys"
 7) "pfselftest"
 8) "client"
 9) "bgrewriteaof"
10) "replicaof"
11) "monitor"
12) "restore-asking"
13) "latency"
14) "replconf"
15) "pfdebug"
16) "bgsave"
17) "sync"
18) "config"
19) "flushall"
20) "cluster"
21) "info"
22) "lastsave"
23) "slaveof"
24) "swapdb"
25) "module"
26) "restore"
27) "migrate"
28) "save"
29) "shutdown"
30) "psync"
31) "sort"

3 删除所有指定的ACL用户,并终止与这些用户进行身份验证的所有连接

> ACL DELUSER antirez
1

4 可用于测试给定用户的权限,而不必启用该用户或导致运行该命令的副作用

> ACL SETUSER VIRGINIA +SET ~*
"OK"
> ACL DRYRUN VIRGINIA SET foo bar
"OK"
> ACL DRYRUN VIRGINIA GET foo
"User VIRGINIA has no permissions to run the 'get' command"

生成安全的用户密码

> ACL GENPASS
"dd721260bfe1b3d9601e7fbab36de6d04e2e67b0ef1c53de59d45950db0dd3cc"

> ACL GENPASS 32
"355ef3dd"

> ACL GENPASS 5
"90"

获取指定 ACL 用户的详细权限信息

> ACL SETUSER sample on nopass +GET allkeys &* (+SET ~key2)
"OK"
> ACL GETUSER sample
1) "flags"
2) 1) "on"
   2) "allkeys"
   3) "nopass"
3) "passwords"
4) (empty array)
5) "commands"
6) "+@all"
7) "keys"
8) "~*"
9) "channels"
10) "&*"
11) "selectors"
12) 1) 1) "commands"
       6) "+SET"
       7) "keys"
       8) "~key2"
       9) "channels"
       10) "&*"

7 显示当前注册的所有用户的详细信息

> ACL LIST
1) "user antirez on #9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08 ~objects:* &* +@all -@admin -@dangerous"
2) "user default on nopass ~* &* +@all"

从配置的 ACL 文件中重新加载 ACL 规则

> ACL LOAD
+OK

> ACL LOAD
-ERR /tmp/foo:1: Unknown command or category name in ACL...

9 处理和查看与 ACL 相关的日志

> AUTH someuser wrongpassword
(error) WRONGPASS invalid username-password pair
> ACL LOG 1
1)  1) "count"
    2) (integer) 1
    3) "reason"
    4) "auth"
    5) "context"
    6) "toplevel"
    7) "object"
    8) "AUTH"
    9) "username"
   10) "someuser"
   11) "age-seconds"
   12) "8.038"
   13) "client-info"
   14) "id=3 addr=127.0.0.1:57275 laddr=127.0.0.1:6379 fd=8 name= age=16 idle=0 flags=N db=0 sub=0 psub=0 ssub=0 multi=-1 qbuf=48 qbuf-free=16842 argv-mem=25 multi-mem=0 rbs=1024 rbp=0 obl=0 oll=0 omem=0 tot-mem=18737 events=r cmd=auth user=default redir=-1 resp=2"
   15) "entry-id"
   16) (integer) 0
   17) "timestamp-created"
   18) (integer) 1675361492408
   19) "timestamp-last-updated"
   20) (integer) 1675361492408

10 将当前的 ACL配置保存到 ACL 文件中

> ACL SAVE
+OK

> ACL SAVE
-ERR There was an error trying to save the ACLs. Please check the server logs for more information

11 如果用户已经存在,除了已经设置的规则之外,还简单地应用所提供的ACL规则

ACL SETUSER virginia on allkeys +set

12 使用另一个ACL SETUSER调用来修改用户规则

ACL SETUSER virginia +get

13 设置了两组权限,一组是在用户上定义的,另一组是用选择器定义的

ACL SETUSER virginia on +GET allkeys (+SET ~app1*)

14 使用特殊规则重置作为第一个规则,以刷新所有其他现有规则

ACL SETUSER antirez reset [... other rules ...]

15 重置用户后,其ACL规则将恢复为默认值:不活动,无密码,不能执行任何命令,也不能访问任何密钥或通道

> ACL SETUSER antirez reset
+OK
> ACL LIST
1) "user antirez off -@all"

16 显示Redis ACL系统中当前配置的所有用户的用户名列表

> ACL USERS
1) "anna"
2) "antirez"
3) "default"

17 返回用于验证当前连接的用户名

> ACL WHOAMI
"default"

18 异步执行 AOF文件的重写操作

BGREWRITEAOF

19 在后台保存数据库

BGSAVE [SCHEDULE]

20 返回此Redis服务器中总命令数的整数回复

redis> COMMAND COUNT
(integer) 241
redis> 

21 返回有关命令的文档信息

redis> COMMAND DOCS SET
1) "set"
2) 1) "summary"
   2) "Sets the string value of a key, ignoring its type. The key is created if it doesn't exist."
   3) "since"
   4) "1.0.0"
   5) "group"
   6) "string"
   7) "complexity"
   8) "O(1)"
   9) "history"
   10) 1) 1) "2.6.12"
          2) "Added the `EX`, `PX`, `NX` and `XX` options."
       2) 1) "6.0.0"
          2) "Added the `KEEPTTL` option."
       3) 1) "6.2.0"
          2) "Added the `GET`, `EXAT` and `PXAT` option."
       4) 1) "7.0.0"
          2) "Allowed the `NX` and `GET` options to be used together."
   11) "arguments"
   12) 1) 1) "name"
          2) "key"
          3) "type"
          4) "key"
          5) "display_text"
          6) "key"
          7) "key_spec_index"
          8) (integer) 0
       2) 1) "name"
          2) "value"
          3) "type"
          4) "string"
          5) "display_text"
          6) "value"
       3) 1) "name"
          2) "condition"
          3) "type"
          4) "oneof"
          5) "since"
          6) "2.6.12"
          7) "flags"
          8) 1) "optional"
          9) "arguments"
          10) 1) 1) "name"
                 2) "nx"
                 3) "type"
                 4) "pure-token"
                 5) "display_text"
                 6) "nx"
                 7) "token"
                 8) "NX"
              2) 1) "name"
                 2) "xx"
                 3) "type"
                 4) "pure-token"
                 5) "display_text"
                 6) "xx"
                 7) "token"
                 8) "XX"
       4) 1) "name"
          2) "get"
          3) "type"
          4) "pure-token"
          5) "display_text"
          6) "get"
          7) "token"
          8) "GET"
          9) "since"
          10) "6.2.0"
          11) "flags"
          12) 1) "optional"
       5) 1) "name"
          2) "expiration"
          3) "type"
          4) "oneof"
          5) "flags"
          6) 1) "optional"
          7) "arguments"
          8) 1) 1) "name"
                2) "seconds"
                3) "type"
                4) "integer"
                5) "display_text"
                6) "seconds"
                7) "token"
                8) "EX"
                9) "since"
                10) "2.6.12"
             2) 1) "name"
                2) "milliseconds"
                3) "type"
                4) "integer"
                5) "display_text"
                6) "milliseconds"
                7) "token"
                8) "PX"
                9) "since"
                10) "2.6.12"
             3) 1) "name"
                2) "unix-time-seconds"
                3) "type"
                4) "unix-time"
                5) "display_text"
                6) "unix-time-seconds"
                7) "token"
                8) "EXAT"
                9) "since"
                10) "6.2.0"
             4) 1) "name"
                2) "unix-time-milliseconds"
                3) "type"
                4) "unix-time"
                5) "display_text"
                6) "unix-time-milliseconds"
                7) "token"
                8) "PXAT"
                9) "since"
                10) "6.2.0"
             5) 1) "name"
                2) "keepttl"
                3) "type"
                4) "pure-token"
                5) "display_text"
                6) "keepttl"
                7) "token"
                8) "KEEPTTL"
                9) "since"
                10) "6.0.0"
redis> 

22 返回完整Redis命令中的键的数组回复

redis> COMMAND GETKEYS MSET a b c d e f
1) "a"
2) "c"
3) "e"
redis> COMMAND GETKEYS EVAL "not consulted" 3 key1 key2 key3 arg1 arg2 arg3 argN
1) "key1"
2) "key2"
3) "key3"
redis> COMMAND GETKEYS SORT mylist ALPHA STORE outlist
1) "mylist"
2) "outlist"
redis> 

23 返回完整Redis命令中键的数组回复及其使用标志

redis> COMMAND GETKEYS MSET a b c d e f
1) "a"
2) "c"
3) "e"
redis> COMMAND GETKEYS EVAL "not consulted" 3 key1 key2 key3 arg1 arg2 arg3 argN
1) "key1"
2) "key2"
3) "key3"
redis> COMMAND GETKEYSANDFLAGS LMOVE mylist1 mylist2 left left
1) 1) "mylist1"
   2) 1) "RW"
      2) "access"
      3) "delete"
2) 1) "mylist2"
   2) 1) "RW"
      2) "insert"
redis> 
本站无任何商业行为
个人在线分享 » Redis命令使用示例(一)
E-->