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

十年热恋 提交于 2019-12-05 19:14:11

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)

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.

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