redis管理客户端
介绍 (Introduction)
Redis is an open-source, in-memory key-value data store. One of its most sought-after features is its support for replication: any Redis server can replicate its data to any number of replicas, allowing for high read scalability and strong data redundancy. Additionally, Redis was designed to allow many clients (up to 10000, by default) to connect and interact with data, making it a good choice for cases where many users need access to the same dataset.
Redis是一个开源的内存中键值数据存储。 它最受追捧的功能之一是对复制的支持:任何Redis服务器都可以将其数据复制到任意数量的副本中,从而实现高读取可伸缩性和强大的数据冗余性。 此外,Redis旨在允许许多客户端(默认情况下最多10000个)连接数据并与之交互,因此对于许多用户需要访问同一数据集的情况而言,它是一个不错的选择。
This tutorial goes over the commands used to manage Redis clients and replicas.
本教程介绍了用于管理Redis客户端和副本的命令。
如何使用本指南 (How To Use This Guide)
This guide is written as a cheat sheet with self-contained examples. We encourage you to jump to any section that is relevant to the task you’re trying to complete.
本指南以备有完整示例的备忘单形式编写。 我们鼓励您跳至与您要完成的任务相关的任何部分。
The commands shown in this guide were tested on an Ubuntu 18.04 server running Redis version 4.0.9. To set up a similar environment, you can follow Step 1 of our guide on How To Install and Secure Redis on Ubuntu 18.04. We will demonstrate how these commands behave by running them with redis-cli
, the Redis command line interface. Note that if you’re using a different Redis interface — Redli, for example — the exact output of certain commands may differ.
本指南中显示的命令已在运行Redis版本4.0.9的Ubuntu 18.04服务器上进行了测试。 要设置类似的环境,您可以按照我们的指南如何在Ubuntu 18.04上安装和保护Redis的 步骤1进行操作。 我们将通过使用Redis命令行界面redis-cli
运行它们来演示这些命令的行为。 请注意,如果您使用其他Redis界面(例如Redli) ,则某些命令的确切输出可能会有所不同。
Alternatively, you could provision a managed Redis database instance to test these commands, but note that depending on the level of control allowed by your database provider, some commands in this guide may not work as described. To provision a DigitalOcean Managed Database, follow our Managed Databases product documentation. Then, you must either install Redli or set up a TLS tunnel in order to connect to the Managed Database over TLS.
另外,您可以提供一个托管的Redis数据库实例来测试这些命令,但是请注意,根据数据库提供者所允许的控制级别,本指南中的某些命令可能无法按所述方式工作。 要配置DigitalOcean托管数据库,请遵循我们的托管数据库产品文档 。 然后, 您必须 安装Redli 或 设置TLS隧道才能通过TLS连接到托管数据库。
Note: The Redis project uses the terms “master” and “slave” in its documentation and in various commands to identify different roles in replication, though the project’s contributors are taking steps to change this language in cases where it doesn’t cause compatibility issues. DigitalOcean generally prefers to use the alternative terms “primary” and “replica”.
注意: Redis项目在其文档和各种命令中使用术语“主”和“从”来标识复制中的不同角色,尽管该项目的贡献者正在采取措施在不引起兼容性问题的情况下更改此语言 。 。 DigitalOcean通常更喜欢使用替代术语“主要”和“副本”。
This guide will default to “primary” and “replica” whenever possible, but note that there are a few instances where the terms “master” and “slave” unavoidably come up.
本指南将在可能的情况下默认为“主要”和“副本”,但请注意,在某些情况下,不可避免地会出现“主”和“从”这两个术语。
管理副本 (Managing Replicas)
One of Redis’s most distinguishing features is its built-in replication. When using replication, Redis creates exact copies of the primary instance. These secondary instances reconnect to the primary any time their connections break and will always aim to remain an exact copy of the primary.
Redis最具特色的功能之一是其内置的复制功能 。 使用复制时,Redis创建主实例的精确副本。 这些辅助实例在它们的连接断开时会随时重新连接到主要实例,并且始终旨在保留主要实例的精确副本。
If you’re not sure whether the Redis instance you’re currently connected to is a primary instance or a replica, you can check by running the role
command:
如果不确定当前连接的Redis实例是主实例还是副本,可以通过运行role
命令进行检查:
- role 角色
This command will return either master
or slave
, or potentially sentinel
if you’re using Redis Sentinel.
此命令将返回master
或slave
,或潜在sentinel
如果你使用Redis的哨兵 。
To designate a Redis instance as a replica of another instance on the fly, run the replicaof
command. This command takes the intended primary server’s hostname or IP address and port as arguments:
要将Redis实例动态指定为另一个实例的副本,请运行replicaof
命令。 此命令将预期的主服务器的主机名或IP地址和端口作为参数:
replicaof hostname_or_IP port
主机名或 IP 端口的副本
If the server was already a replica of another primary, it will stop replicating the old server and immediately start synchronizing with the new one. It will also discard the old dataset.
如果服务器已经是另一台主服务器的副本,它将停止复制旧服务器,并立即开始与新服务器同步。 它还将丢弃旧数据集。
To promote a replica back to being a primary, run the following replicaof
command:
要将副本提升回主副本,请运行以下replicaof
命令:
- replicaof no one 没有人的复制品
This will stop the instance from replicating the primary server, but will not discard the dataset it has already replicated. This syntax is useful in cases where the original primary fails. After running replicaof no one
on a replica of the failed primary, the former replica can be used as the new primary and have its own replicas as a failsafe.
这将阻止实例复制主服务器,但不会丢弃已复制的数据集。 在原始主数据库失败的情况下,此语法很有用。 在发生故障的主数据库的副本上运行replicaof no one
副本后,以前的副本可以用作新的主数据库,并拥有自己的副本作为故障保护。
Note: Prior to version 5.0.0, Redis instead included a version of this command named slaveof
.
注意:在版本5.0.0之前,Redis包括此命令的一个版本,名为slaveof
。
管理客户 (Managing Clients)
A client is any machine or software that connects to a server in order to access a service. Redis comes with several commands that help with tracking and managing client connections.
客户端是连接到服务器以访问服务的任何机器或软件。 Redis附带了一些命令,可帮助跟踪和管理客户端连接。
The client list
command returns a set of human-readable information about current client connections:
client list
命令返回一组有关当前客户端连接的可读信息:
- client list 客户名单
Output
"id=18165 addr=[2001:db8:0:0::12]:47460 fd=7 name=jerry age=72756 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=ping
id=18166 addr=[2001:db8:0:1::12]:47466 fd=8 name= age=72755 idle=5 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=info
id=19381 addr=[2001:db8:0:2::12]:54910 fd=9 name= age=9 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 obl=0 oll=0 omem=0 events=r cmd=client
"
Here is what each of these fields mean:
以下是每个字段的含义:
id
: a unique 64-bit client IDid
:唯一的64位客户端IDname
: the name of the client connection, as defined by a priorclient setname
commandname
:客户端连接的名称,由先前的client setname
命令定义addr
: the address and port from which the client is connectingaddr
:客户端连接的地址和端口fd
: the file descriptor that corresponds to the socket over which the client is connectingfd
:与客户端正在连接的套接字相对应的文件描述符age
: the total duration of the client connection, in secondsage
:客户端连接的总持续时间,以秒为单位flags
: a set of one or more single-character flags that provide more granular detail about the clients; see theclient list
command documentation for more detailsflags
:一组一个或多个单字符标志,这些标志提供有关客户端的更多详细信息; 有关更多详细信息,请参见client list
命令文档 。db
: the current database ID number that the client is connected to (can be from0
to15
)db
:客户端连接到的当前数据库ID号(可以为0
到15
)sub
: the number of channels the client is subscribed tosub
:客户端订阅的频道数psub
: the number of the client’s pattern-matching subscriptionspsub
:客户端的模式匹配订阅数mutli
: the number of commands the client has queued in a transaction (will show-1
if the client hasn’t begun a transaction or0
if it has only started a transaction and not queued any commands)mutli
:客户端已经在排队命令的数量交易 (将显示-1
如果客户还没有开始交易或0
,如果它才开始交易,而不是队列中的任何命令)qbuf
: the client’s query buffer length, with0
meaning it has no pending queriesqbuf
:客户端的查询缓冲区长度,为0
表示没有挂起的查询qbuf-free
: the amount of free space in the client’s query buffer, with0
meaning that the query buffer is fullqbuf-free
:客户端查询缓冲区中的可用空间量,0
表示查询缓冲区已满obl
: the client’s output buffer lengthobl
:客户端的输出缓冲区长度oll
: the length of the client’s output list, where replies are queued when its buffer is fulloll
:客户端的输出列表的长度,当客户端的缓冲区已满时,将在其中排队答复omem
: the memory used by the client’s output bufferomem
:客户端的输出缓冲区使用的内存events
: the client’s file descriptor events, these can ber
for “readable”,w
for “writable,” or bothevents
:客户端的文件描述符事件,可以将r
为“可读”,将w
为“可写”,或者两者都可以cmd
: the last command run by the clientcmd
:客户端运行的最后一条命令
Setting client names is useful for debugging connection leaks in whatever application is using Redis. Every new connection starts without an assigned name, but client setname
can be used to create one for the current client connection. There’s no limit to how long client names can be, although Redis usually limits string lengths to 512 MB. Note, though, that client names cannot include spaces:
设置客户端名称对于调试使用Redis的任何应用程序中的连接泄漏都很有用。 每个新连接都将在开始时没有分配的名称,但是可以使用client setname
名为当前客户端连接创建一个。 尽管Redis通常将字符串长度限制为512 MB,但客户端名称的长度没有限制。 但是请注意,客户端名称不能包含空格:
- client setname elaine 客户端setname elaine
To retrieve the name of a client connection, use the client getname
command:
要检索客户端连接的名称,请使用client getname
命令:
- client getname 客户端getname
Output
"elaine"
To retrieve a client’s connection ID, use the client id
command:
要检索客户端的连接ID,请使用client id
命令:
- client id 客户编号
Output
(integer) "19492"
Redis client IDs are never repeated and are monotonically incremental. This means that if one client has an ID greater than another, then it was established at a later time.
Redis客户端ID永远不会重复,并且会单调递增。 这意味着,如果一个客户端的ID大于另一个客户端,则它是在以后的时间建立的。
阻止客户端并关闭客户端连接 (Blocking Clients and Closing Client Connections)
Replication systems are typically described as being either synchronous or asynchronous. In synchronous replication, whenever a client adds or changes data it must receive some kind of acknowledgement from a certain number of replicas for the change to register as having been committed. This helps to prevent nodes from having data conflicts but it comes at a cost of latency, since the client must wait to perform another operation until it has heard back from a certain number of replicas.
复制系统通常被描述为同步或异步 。 在同步复制中,每当客户端添加或更改数据时,客户端都必须从一定数量的副本中收到某种确认,以使更改注册为已提交。 这有助于防止节点发生数据冲突,但这要付出一定的等待时间,因为客户端必须等待执行另一项操作,直到它从一定数量的副本中收到回音。
In asynchronous replication, on the other hand, the client sees a confirmation that the operation is finished as soon as the data is written to local storage. There can, however, be a lag between this and when the replicas actually write the data. If one of the replicas fails before it can write the change, that write will be lost forever. So while asynchronous replication allows clients to continue performing operations without the latency caused by waiting for replicas, it can lead to data conflicts between nodes and may require extra work on the part of the database administrator to resolve those conflicts.
另一方面,在异步复制中,一旦将数据写入本地存储,客户端就会看到确认操作已完成的确认。 但是,这与副本实际写入数据之间可能会有时滞。 如果其中一个副本在写入更改之前失败,则该写入将永远丢失。 因此,尽管异步复制允许客户端继续执行操作而没有等待副本引起的延迟,但它可能导致节点之间的数据冲突,并且可能需要数据库管理员方面的额外工作来解决这些冲突。
Because of its focus on performance and low latency, Redis implements asynchronous replication by default. However, you can simulate synchronous replication with the wait
command. wait
blocks the current client connection for a specified amount of time (in milliseconds) until all the previous write commands are successfully transferred and accepted by a specified number of replicas. This command uses the following syntax:
由于专注于性能和低延迟,Redis默认情况下实现异步复制。 但是,您可以使用wait
命令模拟同步复制。 wait
在指定的时间段(以毫秒为单位)内阻止当前客户端连接,直到所有先前的写命令成功传输并被指定数量的副本接受为止。 此命令使用以下语法:
wait number_of_replicas number_of_milliseconds
等待number_of_replicas number_of_毫秒
For example, if you want to block your client connection until all the previous writes are registered by at least 3 replicas within a 30 millisecond timeout, your wait
syntax would look like this:
例如,如果您要阻塞客户端连接,直到在30毫秒的超时时间内至少3个副本注册了所有先前的写操作,则wait
语法如下所示:
- wait 3 30 等待3 30
The wait
command returns an integer representing the number of replicas that acknowledged the write commands, even if not every replica does so:
即使不是每个副本都这样做, wait
命令也会返回一个整数,该整数表示已确认写入命令的副本数:
Output
2
To unblock a client connection that has been previously blocked, whether from a wait
, brpop
, or xread
command, you can run a client unblock
command with the following syntax:
要取消阻止先前已被阻止的客户端连接(无论是使用wait
, brpop
还是xread
命令),可以使用以下语法运行client unblock
命令:
client unblock client_id
客户端取消阻止client_id
To temporarily suspend every client currently connected to the Redis server, you can use the client pause
command. This is useful in cases where you need to make changes to your Redis setup in a controlled way. For example, if you’re promoting one of your replicas to be the primary instance, you might pause every client beforehand so you can promote the replica and have the clients connect to it as the new primary without losing any write operations in the process.
要暂时挂起当前连接到Redis服务器的每个客户端,可以使用client pause
命令。 这在需要以受控方式更改Redis设置的情况下很有用。 例如,如果要将一个副本提升为主实例,则可以事先暂停每个客户端,以便提升副本并使客户端作为新的主实例连接到该实例,而不会丢失任何写操作。
The client pause
command requires you to specify the amount of time (in milliseconds) you’d like to suspend the clients. The following example will suspend all clients for one second:
client pause
命令要求您指定要暂停客户端的时间(以毫秒为单位)。 以下示例将所有客户端暂停一秒钟:
- client pause 1000 客户暂停1000
The client kill
syntax allows you to close a single connection or a set of specific connections based on a number of different filters. The syntax looks like this:
client kill
语法允许您基于多个不同的过滤器关闭单个连接或一组特定的连接。 语法如下所示:
client kill filter_1 value_1 ... filter_n value_n
客户端杀死filter_1 value_1 ... filter_n value_n
In Redis versions 2.8.12 and later, the following filters are available:
在Redis 2.8.12和更高版本中,可以使用以下过滤器:
addr
: allows you to close a client connection from a specified IP address and portaddr
:允许您从指定的IP地址和端口关闭客户端连接client-id
: allows you to close a client connection based on its unique ID fieldclient-id
:允许您基于其唯一ID字段关闭客户端连接type
: closes every client of a given type, which can be eithernormal
,master
,slave
, orpubsub
type
:关闭每个给定类型的客户端,这些客户端可以是normal
,master
,slave
或pubsub
-
if
no
is specified, the client calling theclient kill
command will not get skipped, and will be killed if the other filters apply to it如果
no
指定,则调用client kill
命令的client kill
将不会被跳过,并且如果其他过滤器适用于该client kill
,它将被杀死。if
yes
is specified, the client running the command will be skipped and the kill command will have no effect on the client.skipme
is alwaysyes
by default如果指定为
yes
,则将跳过运行该命令的客户端,并且kill命令对客户端无效。 默认情况下,skipme
始终为yes
skipme
: the value options for this filter areyes
andno
:skipme
:此过滤器的值选项为yes
和no
:
结论 (Conclusion)
This guide details a number of commands used to manage Redis clients and replicas. If there are other related commands, arguments, or procedures you’d like to see outlined in this guide, please ask or make suggestions in the comments below.
本指南详细介绍了用于管理Redis客户端和副本的许多命令。 如果您想在本指南中概述其他相关的命令,参数或过程,请在下面的注释中提出疑问或提出建议。
For more information on Redis commands, see our tutorial series on How to Manage a Redis Database.
有关Redis命令的更多信息,请参阅关于如何管理Redis数据库的系列教程。
翻译自: https://www.digitalocean.com/community/cheatsheets/how-to-manage-replicas-and-clients-in-redis
redis管理客户端