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

前端 未结 12 1090
耶瑟儿~
耶瑟儿~ 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.

提交回复
热议问题