# Connection命令

# AUTH

# 语法

AUTH [username] password

# 描述

验证用户密码

# 参数

  • password

用于身份验证的密码。如果配置了 requirepass,这就是服务器密码;如果使用了 ACL(访问控制列表),这就是该用户的密码

  • username

用于身份验证的 ACL 用户名。如果省略此项,将默认使用 default 用户

# 示例

AUTH "temp-pass"

AUTH "test-user" "strong_password"

# CLIENT GETNAME

# 语法

CLIENT GETNAME

# 描述

CLIENT GETNAME 命令会返回当前连接的名称,这个名称是通过 CLIENT SETNAME 命令设置的。因为每个新建立的连接默认都没有关联的名称,所以如果没有分配过名称,该命令将返回nil


# CLIENT HELP

# 语法

CLIENT HELP

# 描述

CLIENT HELP 命令将返回一段描述各子命令的帮助文本


# CLIENT ID

# 语法

CLIENT ID

# 描述

这个命令的作用很简单,就是返回当前连接的 ID。每个连接 ID 都有两个很靠谱的保证:

1.ID 绝对不会重复。所以如果两次调用 CLIENT ID 拿到的数字一样,调用方就可以确定:底层的客户端并没有断开重连,这依然是原来那个连接

2.ID 是单调递增的。也就是说,如果连接 A 的 ID 比连接 B 大,那就可以肯定,连接 A 是在连接 B 之后才跟服务器建立起来的

# 示例

> CLIENT ID
(integer) 12

# CLIENT INFO

# 语法

CLIENT INFO

# 描述

该命令会返回关于当前客户端连接的信息和统计数据

# 示例

> CLIENT INFO
id=12 addr=127.0.0.1:38490 laddr=127.0.0.1:6379 fd=25 name= age=1378049 idle=0 flags=N db=0 sub=0 psub=0 ssub=0 multi=-1 watch=0 qbuf=26 qbuf-free=20448 argv-mem=10 multi-mem=0 rbs=1024 rbp=11 obl=0 oll=0 omem=0 omem-shared=0 omem-unshared=0 tot-mem=22810 events=r cmd=client|info user=interwebz redir=-1 resp=2 lib-name= lib-ver= io-thread=0 tot-net-in=40078202 tot-net-out=11298076 tot-cmds=477029 read-events=477057 avg-pipeline-len-sum=477055 avg-pipeline-len-cnt=477055

# CLIENT LIST

# 语法

CLIENT LIST [TYPE <NORMAL | MASTER | REPLICA | PUBSUB>]
  [ID client-id [client-id ...]]

# 描述

CLIENT LIST 命令会以可读的格式,返回关于服务器客户端连接的信息和统计数据

# 参数

  • TYPE NORMAL | MASTER | REPLICA | PUBSUB

仅列出指定类型的客户端

  • ID client-id

仅列出具有指定 ID 的客户端

# 示例

A: connection to be closed ASAP
b: the client is waiting in a blocking operation
c: connection to be closed after writing entire reply
d: a watched keys has been modified - EXEC will fail
e: the client is excluded from the client eviction mechanism
g: the client is responsible for migrating slots (atomic slot migration)
i: the client is waiting for a VM I/O (deprecated)
M: the client is a master
N: no specific flag set
o: the client is responsible for importing slots (atomic slot migration)
O: the client is a client in MONITOR mode
P: the client is a Pub/Sub subscriber
r: the client is in readonly mode against a cluster node
S: the client is a replica node connection to this instance
u: the client is unblocked
U: the client is connected via a Unix domain socket
x: the client is in a MULTI/EXEC context
t: the client enabled keys tracking in order to perform client side caching
T: the client will not touch the LRU/LFU of the keys it accesses
R: the client tracking target client is invalid
B: the client enabled broadcast tracking mode

r: the client socket is readable (event loop)
w: the client socket is writable (event loop)

# CLIENT REPLY

# 语法

CLIENT REPLY <ON | OFF | SKIP>

# 描述

CLIENT REPLY 命令用于控制服务器是否会对客户端发送的命令进行回复

# 参数

  • ON | OFF | SKIP

    • ON:这是默认模式。在这种模式下,服务器会对客户端发送的每一个命令都返回回复。
    • OFF:在这种模式下,服务器将不再对客户端的任何命令进行回复。
    • SKIP:在这种模式下,服务器只会跳过紧跟在它后面的那一条命令的回复

# CLIENT SETINFO

# 语法

CLIENT SETINFO <LIB-NAME libname | LIB-VER libver>

# 描述

CLIENT SETINFO 命令用于为当前连接分配各种信息属性。这些属性会显示在 CLIENT LIST 和 CLIENT INFO 命令的输出结果中

# 参数

  • LIB-NAME libname

设置当前连接的客户端库名称

  • LIB-VER libverd

设置当前连接的客户端库版本号


# CLIENT SETNAME

# 语法

CLIENT SETNAME connection-name

# 描述

CLIENT SETNAME 命令用于为当前连接指定一个名称

# 参数

  • connection name

要分配给当前连接的名称


# ECHO

# 语法

ECHO message

# 描述

返回消息

# 参数

  • message

返回的消息

# 示例

> ECHO "Hello World!"
"Hello World!"


# HELLO

# 语法

HELLO [protover [AUTH username password] [SETNAME clientname]]

# 描述

切换到不同的协议版本,可选地对连接进行身份验证

# 参数

  • protover

要切换到的 RESP 协议版本(2 或 3)。如果提供了其他任何选项,则此项为必填

  • AUTH username password

在切换到指定协议版本的同时,对连接进行身份验证。这使得在建立新连接时,无需再单独调用 AUTH 命令。

  • SETNAME clientname

设置当前连接的名称。其效果等同于调用 CLIENT SETNAME 命令

# 示例

> HELLO
 1) "server"
 2) "redis"
 3) "version"
 4) "8.8.0"
 5) "proto"
 6) (integer) 2
 7) "id"
 8) (integer) 5
 9) "mode"
10) "standalone"
11) "role"
12) "master"
13) "modules"
14) ...

> HELLO 3
1# "server" => "redis"
2# "version" => "8.8.0"
3# "proto" => (integer) 3
4# "id" => (integer) 10
5# "mode" => "standalone"
6# "role" => "master"
7# "modules" => ...


# PING

# 语法

PING [message]

# 描述

如果不提供任何参数,该命令会返回一个长字符串类型的 PONG;否则,它会原样返回所提供的消息,同样作为长字符串类型

# 参数

  • message

用于替代 PONG 返回的消息

# 示例

> PING
PONG
> PING "hello world"
"hello world"

# QUIT

# 语法

quit

# 描述

请求服务器关闭连接。一旦所有待处理的回复都发送给客户端,连接就会立即关闭


# SELECT

# 语法

SELECT index

# 描述

选择指定零基数字索引的 Redis 逻辑数据库。新建连接默认始终使用 0 号数据库

# 参数

  • index

要切换到的数据库的零基索引