Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronize mongo databases on different servers

I have next situation. I have two mongodb instances on different servers. For example

Mongodb instance on server "one" (host1:27017) with database: "test1"
Mongodb instance on server "two" (host2:27017) with database: "test2"

Now, i need to synchronize "test1" database from "host1:27017" with "test2" from "host2:27017".

By "synchronize" I mean next:

  1. If some collection from "test1" database doesn't exist in "test2" then this collection should be full copied in "test1" database.

  2. If some record from collection doesn't exist in "test2" database, then must be added otherwise updated. If record not exist in A collection in "test1" database, but exist in A collection in "test2" database, then record must be deleted from "test2".

By the way here is problem. For example: "test1" database has collection "A" with the following documents:

{
 _id: "1",
 name: "some name"
}

"test2" database has collection "A" with the following documents:

{
 _id: "1",
 name: "some name"
}

{
 _id: "2",
 name: "some name2"
}

If I perform db.copyDatabase('test1', 'test2', "host2:27017") I get error:

"errmsg" : "exception: E11000 duplicate key error index: test1.A.$id dup key: { : \"1\" }"

Same with cloneDatabase command. How I can resolve it ?

In general what are the ways to synchronize databases? I know what the simplest way is just copy files from one server to second, but maybe there are better ways.

Please help. I'm newcomer in mongo. Thanks.

like image 577
user1932034 Avatar asked May 21 '13 13:05

user1932034


People also ask

How do I carefully migrate a collection from one Mongo server to another?

You'll need to have Mongo installed on your local machine, including the CLI tools we'll be using: mongodump and mongorestore . Details about the command: mongodump —This is the application that allows us to pull data from a Mongo server. --uri — This is the Mongo connect string of the remote Mongo server.

What is MongoDB sync?

In order to maintain up-to-date copies of the shared data set, secondary members of a replica set sync or replicate data from other members. MongoDB uses two forms of data synchronization: initial sync to populate new members with the full data set, and replication to apply ongoing changes to the entire data set.

What is realm sync?

Realm is a fast, scalable alternative to SQLite with mobile to cloud data sync that makes building real-time, reactive mobile apps easy.


1 Answers

I haven't tried this, but the current MongoDB documents describe a replication set equivalent to master-slave replication:

Deploy Master-Slave Equivalent using Replica Sets

If you want a replication configuration that resembles master-slave replication, using replica sets, consider the following replica configuration document. In this deployment hosts and 1 provide replication that is roughly equivalent to a two-instance master-slave deployment:

{
   _id : 'setName',
   members : [
              { _id : 0, host : "<master>", priority : 1 },
              { _id : 1, host : "<slave>", priority : 0, votes : 0 }
  ]
}

See Replica Set Configuration for more information about replica set configurations.

like image 126
Inactivist Avatar answered Oct 05 '22 06:10

Inactivist