Replicate a single Redis database from an instance that has multiple databases

六眼飞鱼酱① 提交于 2020-01-02 05:48:20

问题


I have one Redis instance that has two databases. Now I want to set up a second instance and replicate the first instance, but the second instance should only have one database and replicate only db 0 from the first instance. When I try to do it (set slaveof ... for the second instance) I get the following error message in the Redis log file:

FATAL: Data file was created with a Redis server configured to handle more than 1 databases. Exiting

I tried using redis-dump but I get an error when I try to import the generated dump into the new instance. (not related to 2 dbs vs. 1 db I think, rather a bug in redis-dump, which is still in alpha.

What to do?


回答1:


This is how I achieved it (with help of Gurpartap Singh):

  • Configure the second Redis instance with databases 2 just like the first one
  • Replicate the first instance (see http://redis.io/topics/replication)
  • Open a command line to the second Redis instance (using redis-cli)
  • On Redis CLI: SELECT 1 (select second db)
  • On Redis CLI: FLUSHDB (delete all keys in the second db)
  • On Redis CLI: SAVE (saves the dataset to disk)
  • Exit Redis CLI and stop Redis
  • Change configuration to databases 1
  • Start Redis again

At this point you should have a running Redis instance with only one db (db 0 from the original Redis instance)

The key here is that when we delete all keys in the second db and then save the dataset to disk, the resulting dump only has one db. (Thanks for the hint to Gurpartap Singh)




回答2:


From what I understand, you have redis instance A, which has db 0 and db 1. You want to replicate db 0 across another instance B (possibly on a separate machine - but doesn't matter). Redis largely focusses on complete instance replication, instead of separate databases.

So, if you still want to replicate db 0, and not db 1, I suggest that you migrate either of the db to a separate instance. Like, migrate db 1 to a separate instance, independent of this replication diagram. And then you can set up a master slave replication for the instance that only has db 0 (instance A).

Here's a detailed explanation about how redis replication can be implemented: http://redis.io/topics/replication

Briefly, you have to connect to a fresh new instance (B) that you want to make slave, and issue the command: slaveof redis-master.webapp.com 6379, where the address and port point to the instance A. It'll then sync the data and follow all updates from master instance.

If you find that it isn't syncing the data, you can inspect about it's sync state with the info command. Try it on master as well. Logs help too.

Hope that helps. Let me know if you need more info about setting up redis replication.



来源:https://stackoverflow.com/questions/11819387/replicate-a-single-redis-database-from-an-instance-that-has-multiple-databases

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!