添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more

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

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

If the destination is password protected:

  • redis-cli --raw KEYS '*' | xargs redis-cli MIGRATE my.redis 6379 "" 0 5000 AUTH password-here KEYS
  • I've updated the answer to include example for password protected destination. – lots0logs Sep 26 '19 at 3:14 This works but constrains me to copy data from only the default database 0 – Salathiel Genèse Nov 20 '18 at 19:09 redis-cli -n 2 keys '*' | xargs -I '{}' redis-cli -n 2 migrate my.redis 6379 "" 3 5000 KEYS '{}' to copy from default redis db 2 to my.redis db 3 – Nicholas Nov 22 '18 at 1:29

    For a big DBs with a lot of keys it's better to use --scan instead of keys, to avoid Redis lock on KEYS command:

    redis-cli --scan | xargs redis-cli MIGRATE my.redis 6379 "" 0 5000 KEYS
    

    Not really related to the question, but in case someone will need it: Redis does not support MIGRATE with a password before 3.0. After 3.0, you can add AUTH parameter to check the permission:

    MIGRATE 192.168.0.33 6379 "" 0 5000 AUTH mypassword KEYS user:{info}:age
                    This is authenticating on the local instance I suppose? How do you send password for the destination redis host ?
    – noooooooob
                    Sep 24 '19 at 2:44
    

    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.

    $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)); while this solution is not very "liked", it's the only way I have found so far to copy data from a newer version of redis to an older (looking at you, out of date Alpine repo). Otherwise you always get checksum/version errors – Christian Jan 12 '19 at 9:34

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.