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

前端 未结 12 1044
耶瑟儿~
耶瑟儿~ 2020-12-12 09:27

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

相关标签:
12条回答
  • 2020-12-12 10:03

    Nowadays you can also use MIGRATE, available since 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.

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

     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:

     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.

    0 讨论(0)
  • 2020-12-12 10:04

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

    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.

    0 讨论(0)
  • 2020-12-12 10:05

    Save a snapshot 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.

    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.

    0 讨论(0)
  • 2020-12-12 10:05

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

    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
    
    0 讨论(0)
  • 2020-12-12 10:05

    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):

    /usr/bin/redis-server --port 6399 --dbfilename backup_of_master.rdb --slaveof 1.2.3.4 6379
    
    0 讨论(0)
  • 2020-12-12 10:07

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

    0 讨论(0)
提交回复
热议问题