同步不同服务器上的mongo数据库 [英] Synchronize mongo databases on different servers

查看:394
本文介绍了同步不同服务器上的mongo数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一种情况.我在不同的服务器上有两个mongodb实例. 例如

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"

现在,我需要将"host1:27017"中的"test1"数据库与 来自"host2:27017"的"test2".

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

通过同步",我的意思是下一个:

By "synchronize" I mean next:

  1. 如果"test2"中不存在"test1"数据库中的某些集合,则该集合应完整复制到"test1"数据库中.

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

如果"test2"数据库中不存在集合中的某些记录,则必须添加该记录,否则进行更新.如果记录在"test1"数据库的A集合中不存在,但在"test2"数据库的A集合中存在,则必须从"test2"中删除记录.

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".

这是问题所在.例如: "test1"数据库包含具有以下文档的集合"A":

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

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

"test2"数据库包含具有以下文档的集合"A":

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

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

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

如果我执行db.copyDatabase('test1','test2',"host2:27017"),则会收到错误消息:

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

"errmsg":例外:E11000重复键错误索引:test1.A.$ id dup键:{:\" 1 \}"

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

cloneDatabase命令相同.我该如何解决?

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.

推荐答案

使用_id代替id.无需在模型中声明它.

Use _id instead of id. There is no need to declare it in your model.

我在每台服务器上使用一个小的预钩,该预钩会创建一个受控的唯一_id.猫鼬_id的构建非常合乎逻辑( https://docs.mongodb .com/manual/reference/method/ObjectId/#ObjectIDs-BSONObjectIDSpecification ),数字0,6是机器标识符.我只控制这些数字,因为我有多台服务器,并且我想确保没有串通.如果您只有少数几个,那么不这样做就没有风险.甚至就我而言,我也认为这太偏执了.

I use on each server a small prehook which creates a controlled unique _id. The mongoose _id is built very logical (https://docs.mongodb.com/manual/reference/method/ObjectId/#ObjectIDs-BSONObjectIDSpecification), the digits 0,6 are the machine identifier. I just control these digits because I have multiple servers and I want to assure there is no collusion. If you have just a few, it is probably no risk to not do this. And even in my case I think it is too paranoid.

exports.useProcessId = ()->
  return process.env.INSTANCE_PROCESS_ID? && process.env.INSTANCE_PROCESS_ID.length == 4

exports.manipulateMongooseId = (id) ->
  id = id.toString()
  newId = new ObjectId(id.slice(0,6) + process.env.INSTANCE_PROCESS_ID + id.slice(10,24))
  return newId

模式

mymOdelSchema.pre('save', (next) ->
  data = @
  async.parallel
    myModel: (next)->
      myModelValidator.base(data, next)
    changeMongooseId: (next)->
      if useProcessId && instanceType == 'manager' then processIdConfig.changeMongooseId(data, next) else return next()
    (err)->
      return

 next new Error(err) if err?
      return next()
)

这篇关于同步不同服务器上的mongo数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆