Copying all keys in Redis database using MIGRATE

2019-06-21 19:08发布

Is it possible to copy all keys from one Redis instance to another remote instance using MIGRATE? I've tried COPY, REPLACE and KEYS without any luck. Each time I get a NOKEY response. If I use any of the MIGRATE commands with a single key it works.

Examples:

MIGRATE my.redis 6379 "*" 0 5000 REPLACE // NOKEY
MIGRATE my.redis 6379 "*" 0 5000 COPY // NOKEY
MIGRATE my.redis 6379 "" 0 5000 KEYS * // NOKEY

MIGRATE my.redis 6379 "" 0 5000 KEYS test // OK

标签: redis
3条回答
疯言疯语
2楼-- · 2019-06-21 19:28

This is an improvement on the answer provided by @ezain since I am unable to post comments. The command uses the correct redis syntax for processing batches of keys, but the arguments to xargs result in the command being called once for every key instead of just once with all the keys included (which means it'll take much more time to complete than is necessary). The following will be much faster in all cases:

redis-cli --raw KEYS '*' | xargs redis-cli MIGRATE my.redis 6379 "" 0 5000 KEYS

查看更多
Bombasti
3楼-- · 2019-06-21 19:35

try run in your shell

redis-cli keys '*' | xargs -I '{}' redis-cli migrate my.redis 6379 "" 0 5000 KEYS '{}'
查看更多
男人必须洒脱
4楼-- · 2019-06-21 19:53

I'm not advocating using this, but I tried all of these examples, and many others and did not work. I ended up doing it myself in PHP, so maybe this will help someone else who is stuck.

<?php 

$redisSource = new Redis();
$redisSource->connect('1.2.3.4', 6379);
$redisSource->auth('password');

$redisTarget = new Redis();
$redisTarget->connect('127.0.0.1', 6379);

foreach($redisSource->keys('*') as $key) {
    $redisTarget->set($key, $redisSource->get($key));
}
查看更多
登录 后发表回答