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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6004915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:11:12  来源:igfitidea点击:

How do I move a redis database from one server to another?

databaseredisdata-migrationdatabase-migration

提问by ErJab

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,我会从旧服务器导出数据库并将其导入新服务器。我应该如何用 redis 做到这一点?

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

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

采纳答案by Anurag

Save a snapshot of the database into a dump.rdb by either running BGSAVEor SAVEfrom 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.

通过运行BGSAVESAVE从命令行将数据库的快照保存到 dump.rdb 中。这将在与您的 redis 服务器相同的文件夹中创建一个名为 dump.rdb 的文件。查看所有服务器命令的列表。

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.

将此 dump.rdb 复制到要迁移到的其他 redis 服务器。当 redis 启动时,它会寻找这个文件来初始化数据库。

回答by Wilfred Hughes

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.rdbis completely up-to-date, and shows us where it is stored (/var/lib/redis/dump.rdbin this case). dump.rdbis also periodically written to disk automatically.

这确保dump.rdb是完全最新的,并向我们显示它的存储位置(/var/lib/redis/dump.rdb在这种情况下)。dump.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服务器,复制dump.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 上的版本,否则您可能会遇到兼容性问题

回答by Tom Clarkson

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 不同),将新实例作为从节点 - 然后您可以使用单个命令将新节点切换到主节点,然后使用零停机时间。

回答by nick

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+migration+from+one+Redis+server+to+another

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

但是我怎么知道主机和从机之间的数据传输何时完成呢?您可以使用 INFO 命令。

回答by estani

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:

检查您是否已收到带有 的密钥KEYS *。您也可以通过任何其他方式测试新实例,完成后只需复制:

SLAVEOF NO ONE

回答by ?yvind Skaar

Nowadays you can also use MIGRATE, available since 2.6.

现在您还可以使用 MIGRATE,它从 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 [email protected] -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:

一个使用 KEYS 循环所有键并迁移每个键的小脚本。这是 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/migratefor more info.

有关更多信息,请参阅http://redis.io/commands/migrate

回答by Vinay Vemula

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

要在导入redis数据时检查dump.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.

这里 /Users/Admin 是从服务器读取的 dump.rdb 的位置,因此这是必须替换的文件。

回答by r043v

you can also use rdd

你也可以使用rdd

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

它可以转储和恢复正在运行的 redis 服务器并允许过滤/匹配/重命名转储键

回答by tangxinfa

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

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

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

因为数据不是关键(会话数据),我会尝试一下https://github.com/yaauie/redis-copy

回答by Maoz Zadok

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 6379