5

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?

Manuel Meurer
  • 3,238
  • 6
  • 35
  • 50

2 Answers2

5

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)

Manuel Meurer
  • 3,238
  • 6
  • 35
  • 50
1

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.

Gurpartap Singh
  • 2,744
  • 1
  • 26
  • 30
  • Thanks for your answer, but it doesn't really help. You write "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." That is exactly my problem! How can I migrate only a single db? – Manuel Meurer Aug 12 '12 at 06:08
  • A "simple" way is to clone the instance (again by replication). i.e. make the "separate" instance a slave of instance A; and when the sync is complete, make it `slaveof no one`. After that, you can remove db 0 from the "separate" instance and db 1 from instance A. This is a simple, easy and reliable way (downtime-less). – Gurpartap Singh Aug 12 '12 at 13:25
  • I must be thick, I still don't get it. Say I have the separate instance which was replicated from instance A. I set it to `slaveof no one`. It has two dbs at this point. Can you explain step-by-step how I get from that situation to the situation where it only has one db? How do I remove one of the dbs from the instance? – Manuel Meurer Aug 13 '12 at 09:14
  • Use `SELECT 0` to select a db and `FLUSHDB` to flush it. – Gurpartap Singh Aug 13 '12 at 17:49
  • Selecting a db and calling `FLUSHDB` deletes all keys in that db, not the db itself. I appreciate your effort, but I hope other users can jump in and answer my original question. – Manuel Meurer Aug 14 '12 at 12:14
  • There isn't anything like creating or deleting a database in redis. Redis has configuration for max number of databases an instance should handle. And the dbs are virtually created when a connection to it creates a key and value in it. And virtually deleted when the keys/values are gone. – Gurpartap Singh Aug 14 '12 at 22:13
  • About the error mentioned, have you matched the configuration of both instances? – Gurpartap Singh Aug 14 '12 at 22:20