(Introduction)
There are a number of methods you can use to migrate data from one Redis instance to another, such as replication or snapshotting. However, migrations can get more complicated when you’re moving data to a Redis instance managed by a cloud provider, as managed databases often limit how much control you have over the database’s configuration.
您可以使用多种方法将数据从一个Redis实例迁移到另一个实例,例如复制或快照 。 但是,将数据移动到由云提供商管理的Redis实例时,迁移会变得更加复杂,因为托管数据库通常会限制您对数据库配置的控制权。
This tutorial outlines one method you can use to migrate data to a Redis instance managed by DigitalOcean. The method uses Redis’s internal migrate
command to securely pass data through a TLS tunnel configured with stunnel. This guide will also go over a few other commonly-used migration strategies and why they’re problematic when migrating to a DigitalOcean Managed Database.
本教程概述了可用于将数据迁移到由DigitalOcean管理的Redis实例的一种方法。 该方法使用Redis的内部migrate
命令通过配置了stunnel的TLS隧道安全地传递数据。 本指南还将介绍其他一些常用的迁移策略,以及为什么它们在迁移到DigitalOcean托管数据库时会出现问题。
(Prerequisites)
To complete this tutorial, you will need:
要完成本教程,您将需要:
- One server running Ubuntu 18.04. This server should have a user configured with administrative privileges and a firewall set up with
ufw
. To set up this environment, follow our initial server setup guide for Ubuntu 18.04.
一台运行Ubuntu 18.04的服务器。 该服务器应具有配置了管理特权的用户和使用ufw
设置的防火墙。 要设置此环境,请遵循我们针对Ubuntu 18.04的初始服务器设置指南 。 - Redis installed on your server. To set this up, follow Step 1 of our guide on How To Install and Secure Redis on Ubuntu 18.04.
Redis已安装在您的服务器上。 要进行设置,请遵循我们的指南的“ 如何在Ubuntu 18.04上安装和保护Redis”的 步骤1 。 - A Redis instance managed by a DigitalOcean. To provision one, see our Managed Redis Product Documentation.
由DigitalOcean管理的Redis实例。 要提供一个,请参阅我们的Managed Redis产品文档 。 - Stunnel, an open-source proxy used to create TLS tunnels between machines, installed on your server and configured to maintain a secure connection with your Managed Redis Database. This is necessary because DigitalOcean Managed Databases require connections to be made securely over TLS. Complete our tutorial on How To Connect to a Managed Redis Instance over TLS with Stunnel and redis-cli to set this up. Please note, however, that you do not need to install the
redis-tools
package in Step 1, since you will have already installedredis-cli
when you installed Redis in the previous prerequisite tutorial.
Stunnel ,一个用于在机器之间创建TLS隧道的开源代理,安装在您的服务器上,并配置为维护与Managed Redis数据库的安全连接。 这是必需的,因为DigitalOcean托管数据库要求通过TLS安全地建立连接。 完成关于如何使用Stunnel和redis-cli通过TLS连接到托管Redis实例的教程以进行设置。 但是请注意,您不需要在步骤1中安装redis-tools
软件包,因为在之前的先决条件教程中安装Redis时已经安装了redis-cli
。
Note: To help keep things clear, this guide will refer to the Redis instance hosted on your Ubuntu server as the “source.” Likewise, it will refer to the instance managed by DigitalOcean as either the “target” or the “Managed Database.”
注意:为使内容更清楚,本指南将Ubuntu服务器上托管的Redis实例称为“源”。 同样,它将把DigitalOcean管理的实例称为“目标”或“托管数据库”。
(Things To Consider When Migrating Redis Data to a Managed Database)
There are several methods you can employ to migrate data from one Redis instance to another. However, some of these approaches present problems when you’re migrating data to a Redis instance managed by DigitalOcean.
您可以采用多种方法将数据从一个Redis实例迁移到另一个实例。 但是,将数据迁移到由DigitalOcean管理的Redis实例时,其中一些方法会带来问题。
For example, you can use replication to turn your target Redis instance into an exact copy of the source. To do this, you would connect to the target Redis server and run the replicaof
command with the following syntax:
例如,您可以使用复制将目标Redis实例转换为源的精确副本。 为此,您将连接到目标Redis服务器并使用以下语法运行replicaof
命令:
- replicaof source_hostname_or_ip source_port
source_hostname_or_ip source_port的 副本
This will cause the target instance to replicate all the data held on the source without destroying any data that was previously stored on it. Following this, you would promote the replica back to being a primary instance with the following command:
这将导致目标实例复制源中保存的所有数据,而不会破坏先前存储在其上的任何数据。 之后,您可以使用以下命令将副本提升回主实例:
- replicaof no one
Another method for migrating Redis data is to take a snapshot of the data held on your source instance with either Redis’s save
or bgsave
commands. Both of these commands export the snapshot to a file ending in .rdb
, which you would then transfer to the target server. Following that, you’d restart the Redis service so it can load the data.
迁移Redis数据的另一种方法是使用Redis的save
或bgsave
命令对源实例上save
的数据进行快照。 这两个命令都将快照导出到以.rdb
结尾的文件,然后将其传输到目标服务器。 之后,您将重新启动Redis服务,以便它可以加载数据。
However, each of these three commands — replicaof
, save
, and bgsave
— are disabled on DigitalOcean Managed Databases. These, among other disabled commands, require advanced privileges or access to the managed database server’s underlying file system, making them impractical for a managed database solution. Because of this, DigitalOcean, like other managed database providers, restricts access to these commands, making the associated migration methods impossible.
但是,在DigitalOcean托管数据库上禁用了这三个命令的每个replicaof
, save
和bgsave
。 这些命令以及其他禁用的命令需要高级特权或访问托管数据库服务器的基础文件系统的权限,这使得它们对于托管数据库解决方案不切实际。 因此,与其他托管数据库提供程序一样,DigitalOcean也限制了对这些命令的访问,从而使相关的迁移方法无法实现。
Because the configuration of DigitalOcean’s Managed Databases limit the efficacy of both replication and snapshotting as means of migrating data, this tutorial will instead use Redis’s migrate command to move data from the source to the target. The migrate
command is designed to only move one key at a time, but we will use some handy command line tricks to move an entire Redis database with a single command.
由于DigitalOcean的托管数据库的配置限制了复制和快照作为数据迁移手段的功效,因此本教程将使用Redis的migrate命令将数据从源移动到目标。 migrate
命令被设计为一次只能移动一个键,但是我们将使用一些方便的命令行技巧来通过单个命令移动整个Redis数据库。
(Step 1 — (Optional) Loading Your Source Redis Instance with Sample Data)
This optional step involves loading your source Redis instance with some sample data so you can experiment with migrating data to your Managed Redis Database. If you already have data that you want to migrate over to your target instance, you can move ahead to Step 2.
此可选步骤涉及使用一些示例数据加载源Redis实例,以便您可以尝试将数据迁移到托管Redis数据库。 如果已经有要迁移到目标实例的数据,则可以继续执行第2步 。
To begin, run the following command to access your Redis server:
首先,运行以下命令来访问您的Redis服务器:
- redis-cli
If you’ve configured your Redis server to require password authentication, run the auth
command followed by your Redis password:
如果已将Redis服务器配置为要求密码认证,请运行auth
命令,然后输入Redis密码:
- auth password
验证密码
Then run the following commands. These will create a number of keys holding a few strings, a hash, a list, and a set:
然后运行以下命令。 这些将创建一些包含几个字符串,哈希,列表和集合的键:
- mset string1 "Redis" string2 "is" string3 "fun!"
- hmset hash1 field1 "Redis" field2 "is" field3 "fast!"
- rpush list1 "Redis" "is" "feature-rich!"
- sadd set1 "Redis" "is" "free!"
Additionally, run the following expire
commands to provide a few of these keys with a timeout. This will make them volatile, meaning that Redis will delete them after the specified amount of time, 7500
seconds:
此外,运行以下expire
命令以为其中一些键提供超时。 这将使它们变得不稳定 ,这意味着Redis将在指定的时间( 7500
秒)后删除它们:
- expire string2 7500
- expire hash1 7500
- expire set1 7500
With that, you have some example data you can export to your target Redis instance. You can keep the redis-cli
prompt open for now, since we will run a few more commands from it in the next step in order to back up this data.
这样,您便可以获得一些示例数据,可以将其导出到目标Redis实例。 您现在可以暂时打开redis-cli
提示符,因为我们将在下一步中从中运行更多命令以备份此数据。
(Step 2 — Backing Up Your Data)
Previously, we discussed using Redis’s bgsave
command to take a snapshot of a Redis database and migrate it to another instance. While we won’t use bgsave
as a means of migrating Redis data, we will use it here to back up the data in case we encounter an error during the migration process.
之前,我们讨论过使用Redis的bgsave
命令获取Redis数据库的快照并将其迁移到另一个实例。 虽然我们不会将bgsave
用作迁移Redis数据的方法,但是在迁移过程中遇到错误时,我们将在这里使用它来备份数据。
If you don’t already have it open, start by opening up the Redis command line interface:
如果尚未打开它,请先打开Redis命令行界面:
- redis-cli
Also, if you’ve configured your Redis server to require password authentication, run the auth
command followed by your Redis password:
另外,如果您已将Redis服务器配置为要求密码认证,请运行auth
命令,然后输入Redis密码:
- auth password
验证密码
Next, run the bgsave
command. This will create a snapshot of your current data set and export it to a dump file whose name ends in .rdb
:
接下来,运行bgsave
命令。 这将创建当前数据集的快照,并将其导出到名称以.rdb
结尾的转储文件:
- bgsave
Note: As mentioned in the previous Things To Consider section, you can take a snapshot of your Redis database with either the save
or bgsave
commands. The reason we use the bgsave
command here is that the save
command runs synchronously, meaning it will block any other clients connected to the database. Because of this, the save recommends that this command should almost never be run in a production environment.
注意:如前面的“ 要考虑的事情”部分所述,您可以使用save
或bgsave
命令为Redis数据库创建快照。 我们在这里使用bgsave
命令的原因是save
命令是同步运行的,这意味着它将阻止连接到数据库的任何其他客户端。 因此, save命令文档建议几乎不要在生产环境中运行此命令。
Instead, it suggests using the bgsave command which runs asynchronously. This will cause Redis to fork the database into two processes: the parent process will continue to serve clients while the child saves the database before exiting:
相反,它建议使用异步运行的bgsave命令。 这将导致Redis将数据库分叉到两个进程中:父进程将继续为客户端提供服务,而子进程在退出之前保存数据库:
Note that if clients add or modify data while the bgsave
operation is running or after it finishes, these changes won’t be captured in the snapshot.
请注意,如果客户端在bgsave
操作运行时或完成后添加或修改数据,则这些更改将不会捕获到快照中。
Following that, you can close the connection to your Redis instance by running the exit
command:
之后,您可以通过运行exit
命令关闭与Redis实例的连接:
- exit
If you need it in the future, you can find this dump file in your Redis installation’s working directory. If you’re not sure which directory this is, you can check by opening up your Redis configuration file with your preferred text editor. Here, we’ll use nano
:
如果将来需要,可以在Redis安装的工作目录中找到此转储文件。 如果不确定这是哪个目录,可以使用首选的文本编辑器打开Redis配置文件进行检查。 在这里,我们将使用nano
:
- sudo nano /etc/redis/redis.conf
Navigate to the line that begins with dbfilename
. It will look like this by default:
导航到以dbfilename
开头的行。 默认情况下如下所示:
/etc/redis/redis.conf
/etc/redis/redis.conf
. . .
# The filename where to dump the DB
dbfilename dump.rdb
. . .
This directive defines the file to which Redis will export snapshots. The next line (after any comments) will look like this:
该指令定义Redis将快照导出到的文件。 下一行(在任何注释之后)将如下所示:
/etc/redis/redis.conf
/etc/redis/redis.conf
. . .
dir /var/lib/redis
. . .
The dir
directive defines Redis’s working directory where any Redis snapshots are stored. By default, this is set to /var/lib/redis
, as shown in this example.
dir
指令定义Redis的工作目录,其中存储了所有Redis快照。 默认情况下,它设置为/var/lib/redis
,如本示例所示。
Close the redis.conf
file. Assuming you didn’t make any changes to the file, you can do so by pressing CTRL+X
.
关闭redis.conf
文件。 假设您未对文件进行任何更改,则可以按CTRL+X
Then, list the contents of your Redis working directory to confirm that it’s holding the exported data dump file:
然后,列出您的Redis工作目录的内容,以确认它是否保存了导出的数据转储文件:
- sudo ls /var/lib/redis
须藤ls / var / lib / redis
If the dump file was exported correctly, you will see it in this command’s output:
如果转储文件正确导出,您将在此命令的输出中看到它:
Output
dump.rdb
Once you’ve confirmed that you successfully backed up your data, you can begin the process of migrating it to your Managed Database.
确认已成功备份数据后,就可以开始将其迁移到托管数据库的过程。
(Step 3 — Migrating the Data)
Recall that this guide uses Redis’s internal migrate
command to move keys one by one from the source database to the target. However, unlike the previous steps in this tutorial, we won’t run this command from the redis-cli
prompt. Instead, we’ll run it directly from the server’s bash prompt. Doing so will allow us to use a few bash tricks to migrate all the keys on the source database with one command.
回想一下,本指南使用Redis的内部migrate
命令将密钥从源数据库一个接一个地移动到目标。 但是,与本教程中的先前步骤不同,我们不会在redis-cli
提示符下运行此命令。 相反,我们将直接从服务器的bash提示符下运行它。 这样做将使我们能够使用一些bash技巧来通过一个命令迁移源数据库上的所有键。
Note: If you have clients writing data to your source Redis instance, now would be a good time to configure them to also write data to your Managed Database. This way, you can migrate the existing data from the source to your target without losing any writes that occur after the migration.
注意:如果您有客户端将数据写入源Redis实例,现在是配置它们也将数据写入托管数据库的好时机。 这样,您可以将现有数据从源迁移到目标,而不会丢失迁移后发生的任何写入。
Also, be aware that this migration command will not replace any existing keys on the target database unless one of the existing keys has the same name as a key you’re migrating.
另外,请注意,除非现有密钥之一与要迁移的密钥具有相同的名称,否则此迁移命令不会替换目标数据库上的任何现有密钥。
The migration will occur after running the following command. Before running it, though, we will break it down piece by piece:
运行以下命令后,将进行迁移。 但是,在运行它之前,我们将其细分:
• redis-cli -n source_database -a source_password scan 0 | while read key; do redis-cli -n source_database -a source_password MIGRATE localhost 8000 "$key" target_database 1000 COPY AUTH managed_redis_password; done
redis-cli -n source_database -a source_password扫描0 | 同时读取密钥 做Redis的-CLI -n source_database -a source_password MIGRATE本地主机8000 “$键” target_database 1000 COPY AUTH managed_redis_password; 做完了
Let’s look at each part of this command separately:
让我们分别看一下该命令的每个部分:
- redis-cli -n source_database -a source_password scan 0 . . .
redis-cli -n source_database -a source_password扫描0。 。 。
The first part of the command, redis-cli
, opens a connection to the local Redis server. The -n
flag specifies which of Redis’s logical databases to connect to. Redis has 16 databases out of the box (with the first being numbered 0
, the second numbered 1
, and so on), so source_database
can be any number between 0
and 15
. If your source instance only holds data on the default database (numbered 0
), then you do not need to include the -n
flag or specify a database number.
命令的第一部分redis-cli
打开与本地Redis服务器的连接。 -n
标志指定要连接的Redis 逻辑数据库 。 Redis开箱即用有16个数据库(第一个数据库编号为0
,第二个数据库编号为1
,依此类推),因此source_database
可以是0
到15
之间的任何数字。 如果你的源实例仅保持默认的数据库(编号数据0
),那么你并不需要包括-n
标志或指定的数据库数量。
Next, comes the -a
flag and the source instance’s password, which together authenticate the connection. If your source instance does not require password authentication, then you do not need to include the -a
flag.
接下来是-a
标志和源实例的密码,它们一起对连接进行身份验证。 如果您的源实例不需要密码身份验证,则不需要包括-a
标志。
It then runs Redis’s scan command, which iterates over the keys held in the data set and returns them as a list. scan
requires that you follow it with a cursor — the iteration begins when the cursor is set to 0
, and terminates when the server returns a 0
cursor. Hence, we follow scan
with a cursor of 0
so as to iterate over every key in the set.
然后,它运行Redis的scan命令,该命令遍历数据集中保存的键并将它们作为列表返回。 scan
要求您在其后跟随一个游标 —迭代在游标设置为0
时开始,并在服务器返回0
游标时终止。 因此,我们以0
的游标跟随scan
,以便遍历集合中的每个键。
- . . . | while read key; do . . .
The next part of the command begins with a vertical bar (|
). In Unix-like systems, vertical bars are known as pipes and are used to direct the output of one process to the input of another.
命令的下一部分以竖线( |
)开头。 在类似Unix的系统中,竖线称为管道 ,用于将一个进程的输出定向到另一个进程的输入。
Following this is the start of a while loop. In bash, as well as in most programming languages, a while loop is a control flow statement that lets you repeat a certain process, code, or command as long as a certain condition remains true.
接下来是while循环的开始。 在bash和大多数编程语言中, while循环是控制流语句,只要您满足特定条件,就可以重复执行特定的过程,代码或命令。
The condition in this case is the sub-command read key
, which reads the piped input and assigns it to the variable key
. The semicolon (;
) signifies the end of the while loop’s conditional statement, and the do
following it precedes the action to be repeated as long as the while
expression remains true. Every time the do
statement completes, the conditional statement will read the next line piped from the scan
command and assign that input to the key
variable.
在这种情况下,条件是子命令read key
,该命令read key
管道输入并将其分配给变量key
。 分号( ;
)表示while循环的条件语句的结尾,只要while
表达式保持为真,它后面的do
将在要重复的操作之前。 每当do
语句完成时,条件语句将读取从scan
命令传递的下一行,并将该输入分配给key
变量。
Essentially, this section says “as long as there is output from the scan
command to be read, perform the following action.”
本质上,此部分说:“只要要读取的scan
命令中有输出,请执行以下操作。”
- . . . redis-cli -n source_database -a source_password migrate localhost 8000 "$key" . . .
。 。 。 redis-cli -n source_database -a source_password迁移localhost 8000“ $ key”。 。 。
This section of the command is what performs the actual migration. After another redis-cli
call, it once again specifies the source database number with the -n
flag and authenticates with the -a
flag. You have to include these again because this redis-cli
call is distinct from the one at the start of the command. Again, though, you do not need to include the -n
flag or database number if your source Redis instance only holds data in the default 0
database, and you don’t need to include the -a
flag if it doesn’t require password authentication.
命令的此部分执行实际的迁移。 在另一个redis-cli
调用之后,它再次使用-n
标志指定源数据库号,并使用-a
标志进行身份验证。 您必须再次包含这些内容,因为此redis-cli
调用与命令开始处的调用不同。 同样,但是,如果源Redis实例仅将数据保存在默认的0
数据库中,则无需包括-n
标志或数据库号;如果不需要密码,则无需包括-a
标志。身份验证。
Following this is the migrate command. Any time you use the migrate command, you must follow it with the target database’s hostname or IP address and its port number. Here, we follow the convention established in the prerequisite stunnel tutorial and point the migrate
command to localhost
at port 8000
.
接下来是migrate命令。 任何时候使用迁移命令时,都必须在其后跟随目标数据库的主机名或IP地址及其端口号。 在这里,我们按照既定的惯例前提安全通道的教程 ,并指出了migrate
命令, localhost
的端口8000
。
$key
is the variable defined in the first part of the while
loop, and represents the keys from each line of the scan
command’s output.
$key
是在while
循环的第一部分中定义的变量,它代表scan
命令输出的每一行中的键。
- . . . target_database 1000 copy auth managed_redis_password; done
。 。 。 target_database 1000复制authmanaged_redis_password ; 做完了
This section is a continuation of the migrate
command. It begins with target_database
, which represents the logical database on the target instance where you want to store the data. Again, this can be any number from 0
to 15
.
本节是migrate
命令的续篇。 它以target_database
开头,它表示要存储数据的目标实例上的逻辑数据库。 同样,它可以是0
到15
任何数字。
Next is a number representing a timeout. This timeout is 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 timeout. Both the database number and timeout arguments are required for every migrate
command.
接下来是代表超时的数字。 此超时是两台计算机之间的最大空闲通信时间。 请注意,这不是操作的时间限制,只是操作应始终在定义的超时范围内取得一定程度的进展。 无论是数据库数和超时参数要为每个migrate
命令。
Following the timeout is the optional copy
flag. By default, migrate
will delete each key from the source database after it transfers them to the target; by including this option, though, you’re instructing the migrate
command to merely copy the keys so they will persist on the source.
超时之后是可选的copy
标志。 默认情况下, migrate
将每个密钥传输到目标数据库后将其从源数据库中删除; 但是,通过包含此选项,您将指示migrate
命令仅复制密钥,以便它们将保留在源代码中。
After copy
comes the auth
flag followed by your Managed Redis Database’s password. This isn’t necessary if you’re migrating data to an instance that doesn’t require authentication, but it is necessary when you’re migrating data to one managed by DigitalOcean.
copy
出现auth
标志,后跟您的Managed Redis数据库密码。 如果要将数据迁移到不需要身份验证的实例,则没有必要,但将数据迁移到DigitalOcean管理的实例时,这是必需的。
Following this is another semicolon, indicating the end of the action to be performed as long as the while
condition holds true. Finally, the command closes with done
, indicating the end of the loop. The command checks the condition in the while
statement and repeats the action in the do
statement until it’s no longer true.
接下来是另一个分号,表示只要while
条件成立,将要执行的动作结束。 最后,该命令以done
,指示循环结束。 该命令检查while
语句中的条件,并在do
语句中重复该操作,直到不再正确为止。
All together, this command performs the following steps:
总之,此命令执行以下步骤:
- Scan a database on the source Redis instance and return every key held within it
- Pass each line of the
scan
command’s output into awhile
loop
将scan
命令输出的每一行传递到while
循环中 - Read the first line and assign its content to the
key
variable
阅读第一行并将其内容分配给key
变量 - Migrate any key in the source database that matches the
key
variable to a database on the Redis instance at the other end of the TLS tunnel held onlocalhost
at port8000
将源数据库中与key
变量匹配的任何键迁移到Redis实例上的TLS隧道另一端(位于localhost
的8000
端口)上的数据库 - Go back and read the next line, and repeat the process until there are no more keys to read
Now that we’ve gone over each part of the migration command, you can go ahead and run it.
现在我们已经完成了迁移命令的每个部分,您可以继续运行它。
If your source instance only has data on the default 0
database, you do not need to include either of the -n
flags or their arguments. If, however, you’re migrating data from any database other than 0
on your source instance, you must include the -n
flags and change both occurrences of source_database
to align with the database you want to migrate.
如果你的源实例只有默认的数据0
的数据库,你不需要为包括的-n
标志或它们的参数。 但是,如果要从源实例上除0
以外的任何数据库中迁移数据,则必须包括-n
标志并更改两次出现的source_database
以与要迁移的数据库对齐。
If your source database requires password authentication, be sure to change source_password
to the Redis instance’s actual password. If it doesn’t, though, make sure that you remove both occurrences of -a source_password
from the command. Also, change managed_database_password
to your own Managed Database’s password and be sure to change target_database
to the number of whichever logical database on your target instance that you want to write the data to:
如果您的源数据库需要密码认证,请确保将source_password
更改为Redis实例的实际密码。 但是,如果没有,请确保从命令中删除两次出现的-a source_password
。 另外,将managed_database_password
更改为您自己的托管数据库的密码,并确保将target_database
更改为您要将数据写入到的目标实例上任何逻辑数据库的编号:
Note: If you don’t have your Managed Redis Database’s password on hand, you can find it by first navigating to the DigitalOcean Control Panel. From there, click on Databases in the left-hand sidebar menu and then click on the name of the Redis instance to which you want to migrate the data. Scroll down to the Connection Details section where you’ll find a field labeled password. Click on the show button to reveal the password, then copy and paste it into the migration command — replacing managed_redis_password
— in order to authenticate.
注意:如果您手边没有Managed Redis数据库的密码,可以先导航到DigitalOcean 控制面板来找到它。 在此处,单击左侧边栏菜单中的“ 数据库 ”,然后单击要将数据迁移到的Redis实例的名称。 向下滚动至“ 连接详细信息”部分,您会在其中找到一个标有密码的字段。 单击显示按钮以显示密码,然后将其复制并粘贴到迁移命令中(替换managed_redis_password
)以进行身份验证。
• redis-cli -n source_database -a source_password scan 0 | while read key; do redis-cli -n source_database -a source_password MIGRATE localhost 8000 "$key" target_database 1000 COPY AUTH managed_redis_password; done
redis-cli -n source_database -a source_password扫描0 | 同时读取密钥 做Redis的-CLI -n source_database -a source_password MIGRATE本地主机8000 “$键” target_database 1000 COPY AUTH managed_redis_password; 做完了
You will see output similar to the following:
您将看到类似于以下内容的输出:
Output
NOKEY
OK
OK
OK
OK
OK
OK
Note: Notice the first line of the command’s output which reads NOKEY
. To understand what this means, run the first part of the migration command by itself:
注意:请注意命令输出的第一行显示NOKEY
。 要了解这意味着什么,请自己运行迁移命令的第一部分:
- redis-cli -n source_database -a source_password scan 0
redis-cli -n source_database -a source_password扫描0
If you migrated the sample data added in Step 2, this command’s output will look like this:
如果迁移了在步骤2中添加的样本数据,则此命令的输出将如下所示:
Output
1) "0"
2) 1) "hash1"
2) "string3"
3) "list1"
4) "string1"
5) "string2"
6) "set1"
The value "0"
held in the first line is not a key held in your source Redis database, but a cursor returned by the scan
command. Since there aren’t any keys on the server named “0
”, there’s nothing there for the migrate
command to send to your target instance and it returns NOKEY
.
第一行中保留的值"0"
不是源Redis数据库中保留的键,而是scan
命令返回的游标。 由于国内没有名为“服务器上的任何按键0
”,没有什么存在的migrate
命令发送到目标实例,并将其返回NOKEY
。
However, the command doesn’t fail and exit. Instead, it continues on by reading and migrating the keys found in the next lines of the scan
command’s output.
但是,该命令不会失败并退出。 相反,它通过读取和迁移在scan
命令输出的下一行中找到的键继续进行。
To test whether the migration was successful, connect to your Managed Redis Database:
要测试迁移是否成功,请连接到托管Redis数据库:
- redis-cli -h localhost -p 8000 -a managed_redis_password
redis-cli -h本地主机-p 8000 -amanaged_redis_password
If you migrated data to any logical database other than the default, connect to that database with the select
command:
如果将数据迁移到默认数据库以外的任何逻辑数据库,请使用select
命令连接到该数据库:
- select target_database
选择target_database
Run a scan
command to see what keys are held there:
运行scan
命令以查看其中保留了哪些键:
- scan 0
If you completed Step 2 of this tutorial and added the example data to your source database, you will see output like this:
如果完成了本教程的第2步并将示例数据添加到源数据库中,您将看到如下输出:
Output
1) "0"
2) 1) "set1"
2) "string2"
3) "hash1"
4) "list1"
5) "string3"
6) "string1"
Lastly, run a ttl
command on any key which you’ve set to expire in order to confirm that it is still volatile:
最后,在您设置为过期的任何键上运行ttl
命令,以确认它仍然是易失的:
- ttl string2
Output
(integer) 3944
This output shows that even though you migrated the key to your Managed Database, it still set to expire based on the expireat
command you ran previously.
此输出显示,即使您将密钥迁移到托管数据库,它仍会根据您先前运行的expireat
命令设置为过期。
Once you’ve confirmed that all the keys on your source Redis database were exported to your target successfully, you can close your connection to the Managed Database. If you have clients writing data to the source Redis instance and you’ve already configured them to send their writes to the target, you can at this point configure them to stop sending data to the source.
确认源Redis数据库上的所有键都已成功导出到目标后,就可以关闭与托管数据库的连接。 如果您有客户端将数据写入源Redis实例,并且已经将其配置为将写入的数据发送到目标,则可以在此时配置它们以停止将数据发送到源。
(Conclusion)
By completing this tutorial, you will have moved data from your self-managed Redis data store to a Redis instance managed by DigitalOcean. The process outlined in this guide may not be optimal in every case. For example, you’d have to run the migration command multiple times (once for every logical database holding data) if your source instance is using databases other than the default one. However, when compared to other methods like replication or snapshotting, it is a fairly straightforward process that works well with a DigitalOcean Managed Database’s configuration.
完成本教程后,您已将数据从自我管理的Redis数据存储移至DigitalOcean管理的Redis实例。 本指南中概述的过程可能并非在每种情况下都是最佳的。 例如,如果源实例使用默认数据库以外的数据库,则必须多次运行迁移命令(每个逻辑数据库都必须运行一次)。 但是,与其他方法(如复制或快照)相比,这是一个非常简单的过程,可与DigitalOcean托管数据库的配置很好地配合使用。
Now that you’re using a DigitalOcean Managed Redis Database to store your data, you could measure its performance by running some benchmarking tests. Also, if you’re new to working with Redis, you could check out our series on How To Manage a Redis Database.
现在,您正在使用DigitalOcean托管Redis数据库来存储数据,您可以通过运行一些基准测试来衡量其性能。 另外,如果您不熟悉Redis,可以查看有关如何管理Redis数据库的系列文章 。