MongoDB的local库锁库现象分析

本文深入探讨了MongoDB在并发写操作时,如何影响主副本集,并详细解释了在不同操作场景下,MongoDB是如何进行多库锁定以保持数据一致性的关键机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

并发对"主副本集"的影响?

在副本复制过程中,当在主库执行写操作时,mongodb也同时在写主库的oplog,oplog为一个local库中特殊的集合。(在Replica Set复制集模式下,local.oplog.rs一个capped collection集合,用来存储oplog)。因此,MongoDB必须锁住当前写操作的库和local库。mongod必须锁定这两个库,保持数据的一致性和保证写操作是“全有或全无”的操作。

 

什么情况下会锁多库?

MongoDb如下操作会产生锁多库的情况:

db.copyDatabase() 启用全局锁

Journaling,它是一个内部的操作,将短时间内锁定所有的库。所有的库将共享一个Journal。

User authentication,锁定admin库和用户登录的库

All writes to a replica set’s primary 锁定接收写操作的数据库和local库,锁定local库使得mongod在primary中写oplog。

 

如下为原文:

How does concurrency affect a replica set primary?

In replication, when MongoDB writes to a collection on the primary, MongoDB also writes to the primary’s oplog, which is a special collection in the local database. Therefore, MongoDB must lock both the collection’s database and the local database. The mongod must lock both databases at the same time keep both data consistent and ensure that write operations, even with replication, are “all-or-nothing” operations.

 

Does a MongoDB operation ever lock more than one database?

The following MongoDB operations lock multiple databases:

db.copyDatabase() must lock the entire mongod instance at once.

Journaling, which is an internal operation, locks all databases for short intervals. All databases share a single journal.

User authentication locks the admin database as well as the database the user is accessing.

All writes to a replica set’s primary lock both the database receiving the writes and the local database. The lock for the local database allows the mongod to write to the primary’s oplog.

 

参考链接 http://docs.mongodb.org/manual/faq/concurrency/

转载于:https://www.cnblogs.com/mytech/archive/2013/06/14/3135984.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值