redis管理客户端_如何在Redis中管理副本和客户端

news/2024/7/5 23:25:29

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.

此命令将返回masterslave ,或潜在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 ID

    id :唯一的64位客户端ID

  • name: the name of the client connection, as defined by a prior client setname command

    name :客户端连接的名称,由先前的client setname命令定义

  • addr: the address and port from which the client is connecting

    addr :客户端连接的地址和端口

  • fd: the file descriptor that corresponds to the socket over which the client is connecting

    fd :与客户端正在连接的套接字相对应的文件描述符

  • age: the total duration of the client connection, in seconds

    age :客户端连接的总持续时间,以秒为单位

  • flags: a set of one or more single-character flags that provide more granular detail about the clients; see the client list command documentation for more details

    flags :一组一个或多个单字符标志,这些标志提供有关客户端的更多详细信息; 有关更多详细信息,请参见client list命令文档 。

  • db: the current database ID number that the client is connected to (can be from 0 to 15)

    db :客户端连接到的当前数据库ID号(可以为015 )

  • sub: the number of channels the client is subscribed to

    sub :客户端订阅的频道数

  • psub: the number of the client’s pattern-matching subscriptions

    psub :客户端的模式匹配订阅数

  • mutli: the number of commands the client has queued in a transaction (will show -1 if the client hasn’t begun a transaction or 0 if it has only started a transaction and not queued any commands)

    mutli :客户端已经在排队命令的数量交易 (将显示-1如果客户还没有开始交易或0 ,如果它才开始交易,而不是队列中的任何命令)

  • qbuf: the client’s query buffer length, with 0 meaning it has no pending queries

    qbuf :客户端的查询缓冲区长度,为0表示没有挂起的查询

  • qbuf-free: the amount of free space in the client’s query buffer, with 0 meaning that the query buffer is full

    qbuf-free :客户端查询缓冲区中的可用空间量, 0表示查询缓冲区已满

  • obl: the client’s output buffer length

    obl :客户端的输出缓冲区长度

  • oll: the length of the client’s output list, where replies are queued when its buffer is full

    oll :客户端的输出列表的长度,当客户端的缓冲区已满时,将在其中排队答复

  • omem: the memory used by the client’s output buffer

    omem :客户端的输出缓冲区使用的内存

  • events: the client’s file descriptor events, these can be r for “readable”, w for “writable,” or both

    events :客户端的文件描述符事件,可以将r为“可读”,将w为“可写”,或者两者都可以

  • cmd: the last command run by the client

    cmd :客户端运行的最后一条命令

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:

要取消阻止先前已被阻止的客户端连接(无论是使用waitbrpop还是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 port

    addr :允许您从指定的IP地址和端口关闭客户端连接

  • client-id: allows you to close a client connection based on its unique ID field

    client-id :允许您基于其唯一ID字段关闭客户端连接

  • type: closes every client of a given type, which can be either normal, master, slave, or pubsub

    type :关闭每个给定类型的客户端,这些客户端可以是normalmasterslavepubsub

    • if no is specified, the client calling the client 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 always yes by default

      如果指定为yes ,则将跳过运行该命令的客户端,并且kill命令对客户端无效。 默认情况下, skipme始终为yes

    skipme: the value options for this filter are yes and no:

    skipme :此过滤器的值选项为yesno

结论 (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管理客户端


http://www.niftyadmin.cn/n/3649719.html

相关文章

J2me流媒体技术实现讨论[1]

看到很多很多人持续在问这个问题。以前我也听说,好像kvm底层实现不太支持j2me来做streaming video/audio,但我不知道那人为什么这么说。那么现在国外有一个人提出下面这种思路,并且号称在Nokia6260[相关数据:诺基亚 6260 Nokia626…

golang中使用指针_了解Go中的指针

golang中使用指针介绍 (Introduction) When you write software in Go you’ll be writing functions and methods. You pass data to these functions as arguments. Sometimes, the function needs a local copy of the data, and you want the original to remain unchanged…

RSS和社会性书签Chicklet创建器

“RSS and Social Bookmarking Chicklet Creator”,从名称上好像看不出来是做什么的,进去之后,你就明白了,那摸多的web 2.0参与者们,你们不正需要这套工具吗?它可以一次性帮你解决订阅按钮问题:…

命令行curl上传文件_命令行基础知识:使用cURL下载文件

命令行curl上传文件Client URL, or simple cURL is a library and command-line utility for transferring data between systems. It supports a myriad of different protocols and tends to be installed by default on many Unix-like operating systems. Because of it’s…

认识JSON Web令牌(JWT)

什么是JWT Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计为紧凑且安全的,特别适用于分布式站点的单点登录(SSO)场景。JWT的声明一般被用来在身份提供者和服务提…

盘点我这三年

2003年:2003年伊始,就双喜临门,1月1日被正式任命为部门技术总监,同天搬入新家。这一时期必须要感谢田总邓总的悉心栽培和信任。2003年12月底,自己的blogcn网志“跟随大象的舞步”被当时刚刚成立一周年的中国博客网评选…

canvas动画:黑客帝国_使用Canvas API进行动画处理-第2部分:基本碰撞

canvas动画:黑客帝国In Part 1 of this series we went over the basics of rendering reusable objects to the canvas, using our GUI for more intuitive controls, and creating the illusion of basic movement with our animation loop. In this part we’ll get comfort…

体验Windows Live Mail Beta?

Webleon的《立即体验Windows Live Mail Beta》和Joey的《Live Mail beta初体验》,都写了如何升级hotmail到Windows Live Mail Beta来体验微软的最新战略。我倒是这么做了,先是只得到了25MB->250MB的容量升级,没见到界面改变为神奇的“信息…