阿里云redis 密钥登录_如何管理Redis数据库和密钥

阿里云redis 密钥登录

介绍 (Introduction)

Redis is an open-source, in-memory key-value data store. A key-value data store is a type of NoSQL database in which keys serve as unique identifiers for their associated values. Any given Redis instance includes a number of databases, each of which can hold many different keys of a variety of data types. In this tutorial, we will go over how to select a database, move keys between databases, and manage and delete keys.

Redis是一个开源的内存中键值数据存储。 键值数据存储区是NoSQL数据库的一种,其中用作其关联值的唯一标识符。 任何给定的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连接到托管数据库。

管理数据库 (Managing Databases)

Out of the box, a Redis instance supports 16 logical databases. These databases are effectively siloed off from one another, and when you run a command in one database it doesn’t affect any of the data stored in other databases in your Redis instance.

开箱即用的Redis实例支持16个逻辑数据库。 这些数据库实际上是相互隔离的,在一个数据库中运行命令时,它不会影响Redis实例中其他数据库中存储的任何数据。

Redis databases are numbered from 0 to 15 and, by default, you connect to database 0 when you connect to your Redis instance. However, you can change the database you’re using with the select command after you connect:

Redis数据库的编号从015 ,默认情况下,当您连接到Redis实例时,您将连接到数据库0 。 但是,您可以在连接后通过select命令更改正在使用的数据库:

  • select 15

    选择15

If you’ve selected a database other than 0, it will be reflected in the redis-cli prompt:

如果您选择的数据库不是0 ,那么它将在redis-cli提示符中反映出来:

To swap all the data held in one database with the data held in another, use the swapdb command. The following example will swap the data held in database 6 with that in database 8, and any clients connected to either database will be able to see changes immediately:

要将一个数据库中保存的所有数据与另一个数据库中保存的数据交换,请使用swapdb命令。 下面的示例将数据库6保存的数据与数据库8的数据交换,并且连接到任一数据库的任何客户端都将能够立即看到更改:

  • swapdb 6 8

    swapdb 6 8

swapdb will return OK if the swap is successful.

如果交换成功, swapdb将返回OK

If you want to move a key to a different Redis instance, you can run migrate. This command ensures the key exists on the target instance before deleting it from the source instance. When you run migrate, the command must include the following elements in this order:

如果要将密钥移动到其他Redis实例,则可以运行migrate 。 此命令确保在从源实例删除密钥之前,该密钥存在于目标实例上。 运行migrate ,命令必须按以下顺序包含以下元素:

  • The hostname or IP address of the destination database

    目标数据库的主机名或IP地址
  • The target database’s port number

    目标数据库的端口号
  • The name of the key you want to migrate

    您要迁移的密钥的名称
  • The database number where you want to store the key on the destination instance

    您要在目标实例上存储密钥的数据库号
  • A timeout, in milliseconds, which defines the maximum amount of idle communication time between the two machines. Note that this isn’t a time limit for the operation, just that the operation should always make some level of progress within the defined length of time

    超时(以毫秒为单位),用于定义两台计算机之间的最大空闲通信时间。 请注意,这不是操作的时间限制,只是操作应始终在定义的时间长度内取得一定程度的进展

To illustrate:

为了显示:

  • migrate 203.0.113.0 6379 key_1 7 8000

    迁移203.0.113.0 6379 key_1 7 8000

Additionally, migrate allows the following options which you can add after the timeout argument:

此外, migrate允许在超时参数后添加以下选项:

  • COPY: Specifies that the key should not be deleted from the source instance

    COPY :指定不应从源实例中删除密钥

  • REPLACE: Specifies that if the key already exists on the destination, the migrate operation should delete and replace it

    REPLACE :指定如果目标上已经存在密钥,则migrate操作应删除并替换它

  • KEYS: Instead of providing a specific key to migrate, you can enter an empty string ("") and then use the syntax from the keys command to migrate any key that matches a pattern. For more information on how keys works, see our tutorial on How To Troubleshoot Issues in Redis.

    KEYS :您可以输入一个空字符串( "" ),然后使用keys命令中的语法来迁移与模式匹配的任何密钥,而不是提供要迁移的特定密钥。 有关keys如何工作的更多信息,请参见我们的有关如何对Redis中的问题进行故障排除的教程。

管理金钥 (Managing Keys)

There are a number of Redis commands that are useful for managing keys regardless of what type of data they hold. We’ll go over a few of these in this section.

有许多Redis命令可用于管理密钥,无论它们保存什么类型的数据。 在本节中,我们将介绍其中的一些。

rename will rename the specified key. If it’s successful, it will return OK:

rename将重命名指定的密钥。 如果成功,它将返回OK

  • rename old_key new_key

    重命名old_key new_key

You can use randomkey to return a random key from the currently selected database:

您可以使用randomkey从当前选定的数据库中返回一个随机密钥:

  • randomkey

    随机密钥

   
   
Output
"any_key"

Use type to determine what type of data the given key holds. This command’s output can be either string, list, hash, set, zset, or stream:

使用type确定给定密钥保存的数据类型。 该命令的输出可以是stringlisthashsetzsetstream

  • type key_1

    输入key_1

   
   
Output
"string"

If the specified key doesn’t exist, type will return none instead.

如果指定的键不存在,则type将不返回none

You can move an individual key to another database in your Redis instance with the move command. move takes the name of a key and the database where you want to move the key as arguments. For example, to move the key key_1 to database 8, you would run the following:

你可以在你的Redis实例与移动到另一个数据库中的个人键move命令。 move将键的名称和要将键移动到的数据库作为参数。 例如,要将键key_1移动到数据库8 ,您将运行以下命令:

  • move key_1 8

    移动key_1 8

move will return OK if moving the key was successful.

如果成功移动密钥,则move将返回OK

删除金钥 (Deleting Keys)

To delete one or more keys of any data type, use the del command followed by one or more keys that you want to delete:

要删除任何数据类型的一个或多个键,请使用del命令,后跟要删除的一个或多个键:

  • del key_1 key_2

    del key_1 key_2

If this command deletes the key(s) successfully it will return (integer) 1. Otherwise, it will return (integer) 0.

如果此命令成功删除密钥,它将返回(integer) 1 。 否则,它将返回(integer) 0

The unlink command performs a similar function as del, with the difference being that del blocks the client as the server reclaims the memory taken up by the key. If the key being deleted is associated with a small object, the amount of time it takes for del to reclaim the memory is very small and the blocking time may not even be noticeable.

unlink命令执行与del类似的功能,不同之处在于del在服务器回收密钥占用的内存时会阻塞客户端。 如果要删除的密钥与一个小对象相关联,则del回收内存所花费的时间将非常小,并且阻塞时间甚至可能不会很明显。

However, it can become inconvenient if, for example, the key you’re deleting is associated with many objects, such as a hash with thousands or millions of fields. Deleting such a key can take a noticeably long time, and you’ll be blocked from performing any other operations until it’s fully removed from the server’s memory.

但是,例如,如果要删除的键与许多对象相关联(例如具有数千或数百万个字段的哈希),则可能会带来不便。 删除这样的密钥可能会花费相当长的时间,并且在将其从服务器内存中完全删除之前,您将无法执行任何其他操作。

unlink, however, first determines the cost of deallocating the memory taken up by the key. If it’s small then unlink functions the same way as del by the key immediately while also blocking the client. However, if there’s a high cost to deallocate memory for a key, unlink will delete the key asynchronously by creating another thread and incrementally reclaim memory in the background without blocking the client:

但是, unlink首先确定释放由键占用的内存的成本。 如果它很小,则立即unlink功能与按del键相同,同时也阻止了客户端。 但是,如果为密钥分配内存的成本很高,则unlink将通过创建另一个线程并在后台增量回收内存而不阻塞客户端来异步删除密钥:

  • unlink key_1

    取消key_1的链接

Since it runs in the background, it’s generally recommended that you use unlink to remove keys from your server to reduce errors on your clients, though del will also suffice in many cases.

由于它在后台运行,因此通常建议您使用unlink从服务器中删除密钥,以减少客户机上的错误,尽管在许多情况下del也可以满足要求。

Warning: The following two commands are considered dangerous. The flushdb and flushall commands will irreversibly delete all the keys in a single database and all the keys in every database on the Redis server, respectively. We recommend that you only run these commands if you are absolutely certain that you want to delete all the keys in your database or server.

警告:以下两个命令被认为是危险的flushdbflushall命令将不可逆地分别删除Redis服务器上单个数据库中的所有密钥和每个数据库中的所有密钥。 我们建议仅在绝对确定要删除数据库或服务器中的所有键时才运行这些命令。

It may be in your interest to rename these commands to something with a lower likelihood of being run accidentally.

您可能希望将这些命令重命名为偶然运行的可能性较低的名称。

To delete all the keys in the selected database, use the flushdb command:

要删除所选数据库中的所有键,请使用flushdb命令:

  • flushdb

    刷新数据库

To delete all the keys in every database on a Redis server (including the currently selected database), run flushall:

要删除Redis服务器上每个数据库(包括当前选择的数据库)中的所有键,请运行flushall

  • flushall

    冲洗

Both flushdb and flushall accept the async option, which allows you to delete all the keys on a single database or every database in the cluster asynchronously. This allows them to function similarly to the unlink command, and they will create a new thread to incrementally free up memory in the background.

flushdbflushall接受async选项,该选项允许您async删除单个数据库或集群中每个数据库上的所有键。 这使它们的功能类似于unlink命令,并且它们将创建一个新线程以在后台逐渐释放内存。

备份数据库 (Backing Up Your Database)

To create a backup of the currently selected database, you can use the save command:

要创建当前所选数据库的备份,可以使用save命令:

  • save

    保存

This will export a snapshot of the current dataset as an .rdb file, which is a database dump file that holds the data in an internal, compressed serialization format.

这会将当前数据集的快照导出为.rdb文件,该文件是数据库转储文件,以内部压缩的序列化格式保存数据。

save runs synchronously and will block any other clients connected to the database. Hence, the save command documentation recommends that this command should almost never be run in a production environment. Instead, it suggests using the bgsave command. This tells Redis to fork the database: the parent will continue to serve clients while the child process saves the database before exiting:

save将同步运行,并且将阻止连接到数据库的所有其他客户端。 因此, save命令文档建议几乎不要在生产环境中运行此命令。 相反,它建议使用bgsave命令。 这告诉Redis派生数据库:父进程将继续为客户端提供服务,而子进程在退出之前保存数据库:

  • bgsave

    储存

Note that if clients add or modify data while the bgsave operation is occurring, these changes won’t be captured in the snapshot.

请注意,如果客户端在进行bgsave操作时添加或修改数据,则这些更改将不会捕获到快照中。

You can also edit the Redis configuration file to have Redis save a snapshot automatically (known as snapshotting or RDB mode) after a certain amount of time if a minimum number of changes were made to the database. This is known as a save point. The following save point settings are enabled by default in the redis.conf file:

如果对数据库进行了最少的更改,您还可以编辑Redis配置文件以使Redis在一定时间后自动保存快照(称为快照RDB模式)。 这称为保存点 。 默认情况下,在redis.conf文件中启用以下保存点设置:

/etc/redis/redis.conf
/etc/redis/redis.conf
. . .
save 900 1
save 300 10
save 60 10000
. . .
dbfilename "nextfile.rdb"
. . .

With these settings, Redis will export a snapshot of the database to the file defined by the dbfilename parameter every 900 seconds if at least 1 key is changed, every 300 seconds if at least 10 keys are changed, and every 60 seconds if at least 10000 keys are changed.

使用这些设置,如果更改了至少1个键,则Redis将每900秒将数据库快照导出到dbfilename参数定义的文件;如果更改了至少10个键,则每300秒将数据库快照导出一次;如果更改至少10000,则将每60秒导出一次数据库快照。按键已更改。

You can use the shutdown command to back up your Redis data and then close your connection. This command will block every client connected to the database and then perform a save operation if at least one save point is configured, meaning that it will export the database in its current state to an .rdb file while preventing clients from making any changes.

您可以使用shutdown命令备份Redis数据,然后关闭连接。 如果配置了至少一个保存点,此命令将阻止连接到数据库的每个客户端,然后执行save操作,这意味着它将在不阻止客户端进行任何更改的情况下将当前状态下的数据库导出到.rdb文件。

Additionally, the shutdown command will flush changes to Redis’s append-only file before quitting if append-only mode is enabled. The append-only file mode (AOF) involves creating a log of every write operation on the server in a file ending in .aof after every snapshot. AOF and RDB modes can be enabled on the same server, and using both persistence methods is an effective way to back up your data.

此外,如果启用了仅附加模式shutdown命令将在退出前刷新对Redis的仅附加文件的更改。 仅附加文件模式 (AOF)涉及在每个快照之后以.aof结尾的文件中.aof服务器上每个写入操作的日志。 可以在同一服务器上启用AOF和RDB模式,并且使用两种持久性方法都是备份数据的有效方法。

In short, the shutdown command is essentially a blocking save command that also flushes all recent changes to the append-only file and closes the connection to the Redis instance:

简而言之, shutdown命令本质上是一个阻塞的save命令,该命令还会刷新对仅附加文件的所有最近更改,并关闭与Redis实例的连接:

Warning: The shutdown command is considered dangerous. By blocking your Redis server’s clients, you can make your data unavailable to users and applications that depend on it. We recommend that you only run this command if you are testing out Redis’s behavior or you are absolutely certain that you want to block all your Redis server’s clients.

警告: shutdown命令被认为是危险的 。 通过阻止Redis服务器的客户端,可以使数据对依赖它的用户和应用程序不可用。 我们建议仅在测试Redis的行为或绝对确定要阻止所有Redis服务器的客户端时才运行此命令。

In fact, it may be in your interest to rename this command to something with a lower likelihood of being run accidentally.

实际上, 将这个命令重命名为偶然运行的可能性较小的命令可能符合您的利益。

  • shutdown

    关掉

If you’ve not configured any save points but still want Redis to perform a save operation, append the save option to the `shutdown command:

如果您尚未配置任何保存点,但仍希望Redis执行save操作,请将save选项附加到`shutdown命令中:

  • shutdown save

    关机保存

If you have configured at least one save point but you want to shut down the Redis server without performing a save, you can add the nosave argument to the command:

如果您已配置了至少一个保存点,但要关闭Redis服务器而不执行保存,则可以在命令中添加nosave参数:

  • shutdown nosave

    关机NOSAVE

Note that the append-only file can grow to be very long over time, but you can configure Redis to rewrite the file based on certain variables by editing the redis.conf file. You can also instruct Redis to rewrite the append-only file by running the bgrewriteaof command:

请注意,仅附加文件可能会随着时间增长而变得很长,但是您可以通过编辑redis.conf文件,将Redis配置为基于某些变量redis.conf文件。 您还可以通过运行bgrewriteaof命令指示Redis重写仅附加文件:

  • bgrewriteaof

    bgrewriteaof

bgrewriteaof will create the shortest set of commands needed to bring the database back to its current state. As this command’s name implies, it will run in the background. However, if another persistence command is running in a background process already, that command must finish before Redis will execute bgrewriteaof.

bgrewriteaof将创建使数据库恢复到当前状态所需的最短命令集。 顾名思义,该命令将在后台运行。 但是,如果另一个持久性命令已经在后台进程中运行,则该命令必须在Redis执行bgrewriteaof之前完成。

结论 (Conclusion)

This guide details a number of commands used to manage databases and keys. If there are other related commands, arguments, or procedures you’d like to see in this guide, please ask or make suggestions in the comments below.

本指南详细介绍了许多用于管理数据库和密钥的命令。 如果您想在本指南中看到其他相关的命令,参数或过程,请在下面的评论中提出疑问或提出建议。

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-redis-databases-and-keys

阿里云redis 密钥登录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值