Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 981
Manuel Meurer Avatar asked Aug 05 '12 19:08

Manuel Meurer


People also ask

Can Redis be replicated?

Redis uses asynchronous replication, with asynchronous replica-to-master acknowledges of the amount of data processed. A master can have multiple replicas. Replicas are able to accept connections from other replicas.

Can Redis have multiple databases?

Redis comes with support for multiple databases, which is very similar to the concept in SQL databases. In SQL databases, such as MySQL, PostgreSQL, and Oracle, you can define a name for your databases. However, Redis databases are represented by numbers.

Why do we use different databases numbers for Redis?

Using multiple databases in a single instance may be useful in the following scenario: Different copies of the same database could be used for production, development or testing using real-time data. People may use replica to clone a redis instance to achieve the same purpose.


1 Answers

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)

like image 53
Manuel Meurer Avatar answered Oct 24 '22 05:10

Manuel Meurer