如何将redis数据库从一个服务器移动到另一个服务器?

时间:2022-11-15 20:52:51

I currently have a live redis server running on a cloud instance and I want to migrate this redis server to a new cloud instance and use that instance as my new redis server. If it were MySQL, I would export the DB from the old server and import it into the new server. How should I do this with redis?

我目前有一个运行在云实例上的实时redis服务器,我想将这个redis服务器迁移到一个新的云实例,并将该实例用作新的redis服务器。如果是MySQL,我将从旧服务器导出DB并将其导入到新服务器。我该怎么用redis呢?

P.S.: I'm not looking to set-up replication. I want to completely migrate the redis server to a new instance.

注::我不打算设置复制。我想将redis服务器完全迁移到一个新实例。

13 个解决方案

#1


92  

Save a spanshot of the database into a dump.rdb by either running BGSAVE or SAVE from the command line. This will create a file named dump.rdb in the same folder as your redis server. See a list of all server commands.

将该数据库的一个副本保存到转储文件中。通过运行BGSAVE或从命令行保存rdb。这将创建一个名为dump的文件。rdb位于与redis服务器相同的文件夹中。查看所有服务器命令的列表。

Copy this dump.rdb to the other redis server you want to migrate to. When redis starts up, it looks for this file to initialize the database from.

复制这个转储。rdb到要迁移到的另一个redis服务器。当redis启动时,它会查找这个文件来初始化数据库。

#2


182  

First, create a dump on server A.

首先,在服务器a上创建一个转储。

A$ redis-cli
127.0.0.1:6379> CONFIG GET dir
1) "dir"
2) "/var/lib/redis/"
127.0.0.1:6379> SAVE
OK

This ensures dump.rdb is completely up-to-date, and shows us where it is stored (/var/lib/redis/dump.rdb in this case). dump.rdb is also periodically written to disk automatically.

这样可以确保转储。rdb是完全最新的,并向我们显示它存储在哪里(/var/lib/redis/dump)。在这种情况下rdb)。转储。rdb也定期自动写入磁盘。

Next, copy it to server B:

接下来,将它复制到服务器B:

A$ scp /var/lib/redis/dump.rdb myuser@B:/tmp/dump.rdb

Stop the Redis server on B, copy dump.rdb (ensuring permissions are the same as before), then start.

停止B上的Redis服务器,复制转储。rdb(确保权限与以前相同),然后启动。

B$ sudo service redis-server stop
B$ sudo cp /tmp/dump.rdb /var/lib/redis/dump.rdb
B$ sudo chown redis: /var/lib/redis/dump.rdb
B$ sudo service redis-server start

The version of Redis on B must be greater or equal than that of A, or you may hit compatibility issues.

B上的Redis版本必须大于或等于A,否则可能会遇到兼容性问题。

#3


33  

If you have the connectivity between servers it is better to set up replication (which is trivial, unlike with SQL) with the new instance as a slave node - then you can switch the new node to master with a single command and do the move with zero downtime.

如果您有服务器之间的连接,那么最好使用新实例作为从节点来设置复制(与SQL不同,这很简单)——然后您可以使用一个命令将新节点切换为master,并以零停机时间进行移动。

#4


17  

believe or not, I just made article for it:

信不信由你,我只是为它写了一篇文章:

http://redis4you.com/articles.php?id=005&name=Seamless+migration+from+one+Redis+server+to+another

http://redis4you.com/articles.php?id=005&name=Seamless +移民来自服务器+ 1 +复述,+ + + +

But how do I know when the data transfer is complete between the master and the slave? You can use INFO command.

但是,我如何知道在主服务器和从服务器之间的数据传输何时完成呢?您可以使用INFO命令。

#5


13  

Nowadays you can also use MIGRATE, available since 2.6.

现在,您还可以使用migration,从2.6开始就可以使用。

I had to use this since I only wanted to move the data in one database and not all of them. The two Redis instances live on two different machines.

我必须使用它,因为我只想在一个数据库中移动数据,而不是所有的数据库。两个Redis实例位于两个不同的机器上。

If you can't connect directly to Redis-2 from Redis-1, use ssh port binding:

如果不能从Redis-1直接连接到Redis-2,请使用ssh端口绑定:

 ssh user@redis-2.foo.com -L 1234:127.0.0.1:6379

A small script to loop all the keys using KEYS and MIGRATE each key. This is Perl, but hopefully you get the idea:

一个小脚本,使用键循环所有键并迁移每个键。这是Perl,但希望您能理解:

 foreach ( $redis_from->keys('*') ) {

    $redis_from->migrate(
        $destination{host},    # localhost in my example
        $destination{port},    # 1234
        $_,                    # The key
        $destination{db},
        $destination{timeout} 
    );
 }

See http://redis.io/commands/migrate for more info.

见http://redis。io /命令/迁移更多信息。

#6


11  

It is also possible to migrate data using the SLAVEOF command:

也可以使用SLAVEOF命令迁移数据:

SLAVEOF old_instance_name old_instance_port

Check that you have receive the keys with KEYS *. You could test the new instance by any other way too, and when you are done just turn replication of:

检查你是否收到钥匙*。您也可以通过任何其他方式测试新实例,当您完成时,只需将以下内容复制:

SLAVEOF NO ONE

#7


4  

To check where the dump.rdb has to be placed when importing redis data,

检查垃圾场的位置。在导入redis数据时,必须放置rdb,

start client

开始客户端

$redis-cli

and

then

然后

redis 127.0.0.1:6379> CONFIG GET *
 1) "dir"
 2) "/Users/Admin"

Here /Users/Admin is the location of dump.rdb that is read from server and therefore this is the file that has to be replaced.

这里/用户/管理员是转储的位置。从服务器读取的rdb,因此必须替换这个文件。

#8


2  

you can also use rdd

您还可以使用rdd

it can dump & restore a running redis server and allow filter/match/rename dumps keys

它可以转储和恢复正在运行的redis服务器,并允许筛选/匹配/重命名转储文件键

#9


0  

I also want to do the same thing: migrate a db from a standalone redis instance to a another redis instances(redis sentinel).

我还想做同样的事情:将一个db从一个独立的redis实例迁移到另一个redis实例(redis sentinel)。

Because the data is not critical(session data), i will give https://github.com/yaauie/redis-copy a try.

由于数据不重要(会话数据),我将尝试https://github.com/yaauie/redis-copy。

#10


0  

I just published a command line interface utility to npm and github that allows you to copy keys that match a given pattern (even *) from one Redis database to another.

我刚刚发布了一个命令行接口实用程序到npm和github,它允许您复制从一个Redis数据库到另一个的给定模式(甚至*)的密钥。

You can find the utility here:

你可以在这里找到效用:

https://www.npmjs.com/package/redis-utils-cli

https://www.npmjs.com/package/redis-utils-cli

#11


0  

Key elements of a zero-downtime migration is:

零停机迁移的关键要素是:

  • replication (http://redis.io/commands/SLAVEOF)
  • 复制(http://redis.io/commands/SLAVEOF)
  • possibility to write into a slave during application switching (CONFIG SET slave-read-only no)
  • 在应用程序切换期间写入从服务器的可能性(配置集sla-只读no)

In short:

简而言之:

  1. setup a target redis (empty) as slave of a source redis (with your data)
  2. 将目标redis(空)设置为源redis的奴隶(使用数据)
  3. wait for replication finish
  4. 等待复制完成
  5. permit writes to a target redis (which is currently slave)
  6. 允许写入目标redis(当前为从属)
  7. switch your apps to a target redis
  8. 将应用程序切换到目标redis
  9. wait for finish datastream from master to slave
  10. 等待完成从主到从从属的数据转换
  11. turn a target redis from master to slave
  12. 把一个目标从主人变成奴隶

Additionally redis have options which allows to disable a source redis to accept writes right after detaching a target:

此外,redis还有一些选项,可以在释放目标之后禁用源代码redis以接受写操作:

  • min-slaves-to-write
  • min-slaves-to-write
  • min-slaves-max-lag
  • min-slaves-max-lag

This topic covered by

这个话题被

Very good explanation from RedisLabs team https://redislabs.com/blog/real-time-synchronization-tool-for-redis-migration

非常好的解释来自RedisLabs团队https://redislabs.com/blog/real-time- synchroniz-tools -for redis-migration

And even their interactive tool for migrate: https://github.com/RedisLabs/redis-migrate

甚至他们的交互式迁移工具:https://github.com/RedisLabs/redis-migrate

#12


0  

The simple way I found to export / Backup Redis data (create dump file ) is to start up a server via command line with slaveof flag and create live replica as follow (assuming the source Redis is 1.2.3.4 on port 6379):

我发现导出/备份Redis数据(创建转储文件)的简单方法是使用slaveof标志通过命令行启动服务器,并创建活动副本如下(假设源Redis在6379端口1.2.3.4):

/usr/bin/redis-server --port 6399 --dbfilename backup_of_master.rdb --slaveof 1.2.3.4 6397

#13


-1  

redis-dump finally worked for me. Its documentation provides an example how to dump a Redis database and insert the data into another one.

redisdump最终对我有用。它的文档提供了一个如何转储一个Redis数据库并将数据插入另一个数据库的示例。

#1


92  

Save a spanshot of the database into a dump.rdb by either running BGSAVE or SAVE from the command line. This will create a file named dump.rdb in the same folder as your redis server. See a list of all server commands.

将该数据库的一个副本保存到转储文件中。通过运行BGSAVE或从命令行保存rdb。这将创建一个名为dump的文件。rdb位于与redis服务器相同的文件夹中。查看所有服务器命令的列表。

Copy this dump.rdb to the other redis server you want to migrate to. When redis starts up, it looks for this file to initialize the database from.

复制这个转储。rdb到要迁移到的另一个redis服务器。当redis启动时,它会查找这个文件来初始化数据库。

#2


182  

First, create a dump on server A.

首先,在服务器a上创建一个转储。

A$ redis-cli
127.0.0.1:6379> CONFIG GET dir
1) "dir"
2) "/var/lib/redis/"
127.0.0.1:6379> SAVE
OK

This ensures dump.rdb is completely up-to-date, and shows us where it is stored (/var/lib/redis/dump.rdb in this case). dump.rdb is also periodically written to disk automatically.

这样可以确保转储。rdb是完全最新的,并向我们显示它存储在哪里(/var/lib/redis/dump)。在这种情况下rdb)。转储。rdb也定期自动写入磁盘。

Next, copy it to server B:

接下来,将它复制到服务器B:

A$ scp /var/lib/redis/dump.rdb myuser@B:/tmp/dump.rdb

Stop the Redis server on B, copy dump.rdb (ensuring permissions are the same as before), then start.

停止B上的Redis服务器,复制转储。rdb(确保权限与以前相同),然后启动。

B$ sudo service redis-server stop
B$ sudo cp /tmp/dump.rdb /var/lib/redis/dump.rdb
B$ sudo chown redis: /var/lib/redis/dump.rdb
B$ sudo service redis-server start

The version of Redis on B must be greater or equal than that of A, or you may hit compatibility issues.

B上的Redis版本必须大于或等于A,否则可能会遇到兼容性问题。

#3


33  

If you have the connectivity between servers it is better to set up replication (which is trivial, unlike with SQL) with the new instance as a slave node - then you can switch the new node to master with a single command and do the move with zero downtime.

如果您有服务器之间的连接,那么最好使用新实例作为从节点来设置复制(与SQL不同,这很简单)——然后您可以使用一个命令将新节点切换为master,并以零停机时间进行移动。

#4


17  

believe or not, I just made article for it:

信不信由你,我只是为它写了一篇文章:

http://redis4you.com/articles.php?id=005&name=Seamless+migration+from+one+Redis+server+to+another

http://redis4you.com/articles.php?id=005&name=Seamless +移民来自服务器+ 1 +复述,+ + + +

But how do I know when the data transfer is complete between the master and the slave? You can use INFO command.

但是,我如何知道在主服务器和从服务器之间的数据传输何时完成呢?您可以使用INFO命令。

#5


13  

Nowadays you can also use MIGRATE, available since 2.6.

现在,您还可以使用migration,从2.6开始就可以使用。

I had to use this since I only wanted to move the data in one database and not all of them. The two Redis instances live on two different machines.

我必须使用它,因为我只想在一个数据库中移动数据,而不是所有的数据库。两个Redis实例位于两个不同的机器上。

If you can't connect directly to Redis-2 from Redis-1, use ssh port binding:

如果不能从Redis-1直接连接到Redis-2,请使用ssh端口绑定:

 ssh user@redis-2.foo.com -L 1234:127.0.0.1:6379

A small script to loop all the keys using KEYS and MIGRATE each key. This is Perl, but hopefully you get the idea:

一个小脚本,使用键循环所有键并迁移每个键。这是Perl,但希望您能理解:

 foreach ( $redis_from->keys('*') ) {

    $redis_from->migrate(
        $destination{host},    # localhost in my example
        $destination{port},    # 1234
        $_,                    # The key
        $destination{db},
        $destination{timeout} 
    );
 }

See http://redis.io/commands/migrate for more info.

见http://redis。io /命令/迁移更多信息。

#6


11  

It is also possible to migrate data using the SLAVEOF command:

也可以使用SLAVEOF命令迁移数据:

SLAVEOF old_instance_name old_instance_port

Check that you have receive the keys with KEYS *. You could test the new instance by any other way too, and when you are done just turn replication of:

检查你是否收到钥匙*。您也可以通过任何其他方式测试新实例,当您完成时,只需将以下内容复制:

SLAVEOF NO ONE

#7


4  

To check where the dump.rdb has to be placed when importing redis data,

检查垃圾场的位置。在导入redis数据时,必须放置rdb,

start client

开始客户端

$redis-cli

and

then

然后

redis 127.0.0.1:6379> CONFIG GET *
 1) "dir"
 2) "/Users/Admin"

Here /Users/Admin is the location of dump.rdb that is read from server and therefore this is the file that has to be replaced.

这里/用户/管理员是转储的位置。从服务器读取的rdb,因此必须替换这个文件。

#8


2  

you can also use rdd

您还可以使用rdd

it can dump & restore a running redis server and allow filter/match/rename dumps keys

它可以转储和恢复正在运行的redis服务器,并允许筛选/匹配/重命名转储文件键

#9


0  

I also want to do the same thing: migrate a db from a standalone redis instance to a another redis instances(redis sentinel).

我还想做同样的事情:将一个db从一个独立的redis实例迁移到另一个redis实例(redis sentinel)。

Because the data is not critical(session data), i will give https://github.com/yaauie/redis-copy a try.

由于数据不重要(会话数据),我将尝试https://github.com/yaauie/redis-copy。

#10


0  

I just published a command line interface utility to npm and github that allows you to copy keys that match a given pattern (even *) from one Redis database to another.

我刚刚发布了一个命令行接口实用程序到npm和github,它允许您复制从一个Redis数据库到另一个的给定模式(甚至*)的密钥。

You can find the utility here:

你可以在这里找到效用:

https://www.npmjs.com/package/redis-utils-cli

https://www.npmjs.com/package/redis-utils-cli

#11


0  

Key elements of a zero-downtime migration is:

零停机迁移的关键要素是:

  • replication (http://redis.io/commands/SLAVEOF)
  • 复制(http://redis.io/commands/SLAVEOF)
  • possibility to write into a slave during application switching (CONFIG SET slave-read-only no)
  • 在应用程序切换期间写入从服务器的可能性(配置集sla-只读no)

In short:

简而言之:

  1. setup a target redis (empty) as slave of a source redis (with your data)
  2. 将目标redis(空)设置为源redis的奴隶(使用数据)
  3. wait for replication finish
  4. 等待复制完成
  5. permit writes to a target redis (which is currently slave)
  6. 允许写入目标redis(当前为从属)
  7. switch your apps to a target redis
  8. 将应用程序切换到目标redis
  9. wait for finish datastream from master to slave
  10. 等待完成从主到从从属的数据转换
  11. turn a target redis from master to slave
  12. 把一个目标从主人变成奴隶

Additionally redis have options which allows to disable a source redis to accept writes right after detaching a target:

此外,redis还有一些选项,可以在释放目标之后禁用源代码redis以接受写操作:

  • min-slaves-to-write
  • min-slaves-to-write
  • min-slaves-max-lag
  • min-slaves-max-lag

This topic covered by

这个话题被

Very good explanation from RedisLabs team https://redislabs.com/blog/real-time-synchronization-tool-for-redis-migration

非常好的解释来自RedisLabs团队https://redislabs.com/blog/real-time- synchroniz-tools -for redis-migration

And even their interactive tool for migrate: https://github.com/RedisLabs/redis-migrate

甚至他们的交互式迁移工具:https://github.com/RedisLabs/redis-migrate

#12


0  

The simple way I found to export / Backup Redis data (create dump file ) is to start up a server via command line with slaveof flag and create live replica as follow (assuming the source Redis is 1.2.3.4 on port 6379):

我发现导出/备份Redis数据(创建转储文件)的简单方法是使用slaveof标志通过命令行启动服务器,并创建活动副本如下(假设源Redis在6379端口1.2.3.4):

/usr/bin/redis-server --port 6399 --dbfilename backup_of_master.rdb --slaveof 1.2.3.4 6397

#13


-1  

redis-dump finally worked for me. Its documentation provides an example how to dump a Redis database and insert the data into another one.

redisdump最终对我有用。它的文档提供了一个如何转储一个Redis数据库并将数据插入另一个数据库的示例。