原文: http://www.snailinaturtleneck.com/blog/2010/10/12/replication-internals/
This is the first in a three-part series on how replication works.
Replication is awesome: it gives you hot backups, read scaling, and all sorts of other goodies. If you know how it works you can get a lot more out of it, from how it should be configured to what you should monitor to using it directly in your applications. So, how does it work?
MongoDB’s replication is actually very simple: the master keeps a collection that describes writes and the slaves query that collection. This collection is called the oplog (short for “operation log”).
The oplog
Each write (insert, update, or delete) creates a document in the oplog collection, so long as replication is enabled (MongoDB won’t bother keeping an oplog if replication isn’t on). So, to see the oplog in action, start by running the database with the –replSet option:
$ ./mongod --replSet funWithOplogs
Now, when you do operations, you’ll be able to see them in the oplog. Let’s start out by initializing out replica set:
> rs.initiate()
Now if we query the oplog you’ll see this operation:
> use local
switched to db local
> db.oplog.rs.find()
{
"ts" : { "t" : 1286821527000, "i" : 1 },
"h" : NumberLong(0),
"op" : "n",
"ns" : "",
"o" : { "msg" : "initiating set" }
}
This is just an informational message for the slave, it isn’t a “real” operation. Breaking this down, it contains the following fields:
* ts: the time this operation occurred.
* h: a unique ID for this operation. Each operation will have a different value in this field.
* op: the write operation that should be applied to the slave. n indicates a no-op, this is just an informational message.
* ns: the database and collection affected by this operation. Since this is a no-op, this field is left blank.
* o: the actual document representing the op. Since this is a no-op, this field is pretty useless.
To see some real oplog messages, we’ll need to do some writes. Let’s do a few simple ones in the shell:
> use test
switched to db test
> db.foo.insert({x:1})
> db.foo.update({x:1}, {$set : {y:1}})
> db.foo.update({x:2}, {$set : {y:1}}, true)
> db.foo.remove({x:1})
Now look at the oplog:
> use local
switched to db local
> db.oplog.rs.find()
{ "ts" : { "t" : 1286821527000, "i" : 1 }, "h" : NumberLong(0), "op" : "n", "ns" : "", "o" : { "msg" : "initiating set" } }
{ "ts" : { "t" : 1286821977000, "i" : 1 }, "h" : NumberLong("1722870850266333201"), "op" : "i", "ns" : "test.foo", "o" : { "_id" : ObjectId("4cb35859007cc1f4f9f7f85d"), "x" : 1 } }
{ "ts" : { "t" : 1286821984000, "i" : 1 }, "h" : NumberLong("1633487572904743924"), "op" : "u", "ns" : "test.foo", "o2" : { "_id" : ObjectId("4cb35859007cc1f4f9f7f85d") }, "o" : { "$set" : { "y" : 1 } } }
{ "ts" : { "t" : 1286821993000, "i" : 1 }, "h" : NumberLong("5491114356580488109"), "op" : "i", "ns" : "test.foo", "o" : { "_id" : ObjectId("4cb3586928ce78a2245fbd57"), "x" : 2, "y" : 1 } }
{ "ts" : { "t" : 1286821996000, "i" : 1 }, "h" : NumberLong("243223472855067144"), "op" : "d", "ns" : "test.foo", "b" : true, "o" : { "_id" : ObjectId("4cb35859007cc1f4f9f7f85d") } }
You can see that each operation has a ns now: “test.foo”. There are also three operations represented (the op field), corresponding to the three types of writes mentioned earlier: i for inserts, u for updates, and d for deletes.
The o field now contains the document to insert or the criteria to update and remove. Notice that, for the update, there are two o fields (o and o2). o2 gives the update modifications (equivalent to update()‘s second argument).
Using this information
MongoDB doesn’t yet have triggers, but applications could hook into this collection if they’re interested in doing something every time a document is deleted (or updated, or inserted, etc.) Part three of this series will elaborate on this idea.
Next up: what the oplog is and how syncing works.
分享到:
相关推荐
MongoDB: The Definitive Guide by Kristina Chodorow and Michael Dirolf Copyright © 2010 Kristina Chodorow and Michael Dirolf. All rights reserved.
MongoDB Administrator's Guide 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
MongoDB是一种流行的开源文档型数据库,它以其灵活性、可扩展性和高性能在现代数据存储解决方案中占据了一席之地。《MongoDB权威指南》是学习和掌握MongoDB的重要参考资料,该书涵盖了MongoDB的基础知识到高级应用,...
MongoDB架构指南向我们介绍了一个非关系型数据库系统MongoDB的设计原理和管理方式。作为数据库领域的重要组成部分,MongoDB的设计和实现代表了大数据时代对于存储解决方案的新要求。 首先,MongoDB的架构设计与传统...
它通过复制集(Replica Sets)和选举以及故障转移(Elections and Failover)机制提供了高可用性和故障恢复能力。通过配置可写的可用性,确保了即便在发生故障时,系统也能够保持数据的完整性和应用的持续运行。此外...
考虑到本书的版本更新,我们可以合理推测第二版还可能包括了MongoDB的高级功能和管理技巧,如索引、复制集、分片、故障转移机制、数据备份与恢复、安全设置、性能优化等。此外,随着版本的更新,一些新的驱动程序、...
MongoDB: The Definitive Guide MongoDB is a powerful, flexible, and scalable generalpurpose database. It combines the ability to scale out with features such as secondary indexes, range queries, ...
复制集是MongoDB中的一个重要特性,它提供了数据冗余和故障切换能力,以确保数据的高可用性。在本实例中,我们将详细介绍如何搭建MongoDB的复制集。 1. **复制集的概念** 复制集是MongoDB中一组MongoDB节点的集合...
MongoDB是一种流行的开源文档数据库系统,以其灵活性、可扩展性和高性能而受到许多企业和开发者的青睐。作为NoSQL数据库的一种,它采用非关系型数据模型,支持动态schema,适合处理大量结构化和半结构化数据。以下是...
MongoDB Sharding 机制是 MongoDB 中的一种机制,用于将数据水平切分到不同的物理节点,以解决单机性能极限的问题。Sharding 可以利用上更多的硬件资源来解决单机性能极限的问题,并减小每个索引的体积,提高索引...
《MongoDB:终极指南》是Eelco Plugge、Peter Membrey和Tim Hawkins三位作者共同编写的关于MongoDB数据库的权威指南。本书详细介绍了MongoDB作为一种非关系型(NoSQL)数据库在云计算和桌面计算领域的应用,为读者...
在《MongoDB Definitive Guide》这本权威指南中,读者将深入了解到MongoDB的核心概念、安装配置、数据模型设计、操作与查询、性能优化以及高级特性的运用。以下是基于该书内容的知识点详解: 1. **MongoDB基础**:...
在MongoDB中,复制操作可以分为本地复制和远程复制,本地复制通常用于服务器内部的多个节点间同步数据,而远程复制则用于不同地理位置的数据库间的数据同步。为了复制数据,MongoDB提供了如`cloneCollection`命令来...
在《MongoDB 分片复制集集群搭建》博客中,作者详细介绍了如何一步步实现这样的集群,包括配置文件的编写和集群的部署。通过学习这篇博客,你可以深入理解MongoDB集群的内部运作机制,并掌握实际操作技能。