将 mongo db oplog 复制到另一个 mongo db
您好,我们在 mongo 上有一个生产数据库,它有一组集合,所有活动都加载到 oplog 中。现在我想编写一个脚本来观察这个 oplog,这样当一条新记录添加到 oplog 中时,我想将其写入另一个虚拟服务器上的数据库。我该怎么办呢。我是 mongo 新手,所以我不确定从哪里开始。任何想法都会对我有帮助。我正在考虑类似的事情
while(true)
{
watch(oplog)
OnNewEntry
{
AddToAnotherMongo(another.server.com,port,dbname,record)
}
}
Hi we have a production DB on mongo which has a set of collections and all the activities are loaded into an oplog. Now i wanna write a script to watch this oplog so that when ever a new record is added to the oplog, i wanna write it to a db on another dummy server. How can i go about this. I am new to mongo, so im unsure of where to start with this. any ideas would be helpful for me. I am thinking of something on the lines of
while(true)
{
watch(oplog)
OnNewEntry
{
AddToAnotherMongo(another.server.com,port,dbname,record)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有各种 oplog 阅读器可以观看并重播到特定服务器。这就是副本集默认执行的操作,并且只有一个主副本(写入器)。如果您只想要数据的副本,那么复制集是最好的选择,并且无需任何代码即可支持。
以下是一些读取 oplog 的代码示例:
There are various oplog readers which can watch and replay to a specific server. This is what replicasets do by default and there is only one primary (writer). If you just want copies of your data then replicasets are the best option, and supported without any code.
Here are some samples of code which read the oplog:
我遇到了一个类似的问题,并按照您在 javascript 中的操作码示例找到了一个非常简单的解决方案,以便在 mongo-shell 中执行。
源代码可在此处
打开主服务器的 oplog 上的可尾游标每个操作都可以应用于另一台服务器(当然您可以按集合甚至数据库的名称空间进行过滤......)
I had a simliar problem and found a quite easy solution following your opcode-example in javascript to be executed in a mongo-shell.
source code available here
With opening a tailable cursor on the oplog of the master server each operation could be applied to another server (of course you can filter by the namespace of the collections or even the databases...)