`
sjk2013
  • 浏览: 2192119 次
文章分类
社区版块
存档分类
最新评论

mongodb 锁

 
阅读更多

mongodb不同于其他关系型数据库,mongodb锁的粒度很大,2.2之前是整个server级别的锁,也叫全局锁,但是支持并发读,也就是说读可以同时读,但是只要有写操作,那么就加上锁,其他人既不能读也不能写,从2.2开始,这个锁的粒度缩小到了库级别,如果你有3个库,一个库有写锁,那么其他2个库还是继续可以读写操作,希望以后能像关系数据库一样到表级别。

2.2锁引发的条件:

Operation Lock Type
Issue a query Read lock
Get more data from acursor Read lock
Insert data Write lock
Remove data Write lock
Update data Write lock
Map-reduce Read lock and write lock, unless operations are specified as non-atomic. Portions of map-reduce jobs can run concurrently.
Create an index Building an index in the foreground, which is the default, locks the database for extended periods of time.
db.eval() Write lock or no lock. If this is used without thenolockoption, all JavaScript is blocked.
aggregate() Read lock

The following administrative operations require an exclusive (i.e. write) lock to a the database for extended periods:

db.collection.ensureIndex(), when issuedwithoutsettingbackgroundtotrue,
reIndex,
compact,
db.repairDatabase(),
db.createCollection(), when creating a very large (i.e. many gigabytes) capped collection,
db.collection.validate(), and
db.copyDatabase(). This operation may lock all databases.

Thedb.collection.group()operation takes a read lock and does not allow any other threads to execute JavaScript while it is running.

The following administrative commands lock the database but only hold the lock for a very short time:

db.collection.dropIndex(),
db.collection.getLastError(),
db.isMaster(),
rs.status()(i.e.replSetGetStatus,)
db.serverStatus(),
db.auth(), and
db.addUser().
Does a read or write operation ever yield the lock?
New in version 2.0.

A read and write operations will yield their locks if themongodreceives apage faultorfetches data that is unlikely to be in memory. Yielding allows other operations that only need to access documents that are already in memory to complete whilemongodloads documents into memory.

Additionally, write operations that affect multiple documents (i.e.update()<db.collection.update()with themultiparameter,) will yield periodically to allow read operations during these log write operations. Similarly, long running read locks will yield periodically to ensure that write operations have the opportunity to complete.

Changed in version 2.2:The use of yielding expanded greatly in MongoDB 2.2. Including the “yield for page fault.” MongoDB tracks the contents of memory and predicts whether data is available before performing a read. If MongoDB predicts that the data is not in memory a read operation yields its lock while MongoDB loads the data to memory. Once data is available in memory, the read will reacquire the lock to completes the operation.


至于page fault,以及mmap请参阅http://docs.mongodb.org/manual/faq/storage/#faq-storage-page-faults

分享到:
评论

相关推荐

    mongolock:带有 mongodb 后端的 Python 分布式锁

    用法 from mongolock import MongoLocklock = MongoLock()# you can use it as context:# (if lock is already taken by another worker, MongoLockLocked will be raised)with lock('key', 'my_worker_name', ...

    MongoDB权威指南(中文版)高清

    1168.4 备份和修复 1168.4.1 数据文件备份 1178.4.2 mongodump和mongorestore 1178.4.3 fsync和锁 1188.4.4 从属备份 1198.4.5 修复 119第9章 复制 1219.1 主从复制 1219.1.1 选项 1229.1.2 添加...

    深入云计算 MongoDB管理与开发实战详解pdf.part1

    8.8.4 fsync和锁 8.8.5 从属备份 8.8.6 修复 8.9 本章小结 第4篇 性能篇 第9章 索引 9.1 索引简介 9.1.1 基础索引 9.1.2 文档索引 9.1.3 组合索引 9.1.4 唯一索引 9.1.5 强制使用索引 ...

    电子书:MongoDB权威指南(中文版)

    1158.3.3 其他安全考虑 1168.4 备份和修复 1168.4.1 数据文件备份 1178.4.2 mongodump 和mongorestore 1178.4.3 fsync 和锁 1188.4.4 从属备份 1198.4.5 修复 119第9 章 复制 1219.1 主从复制 ...

    MongoDB性能评估和分析-Golang开发

    锁Kong-像带有钥匙Kong的Pro一样对MongoDB集群进行性能分析:第1部分像带钥匙Kong的Pro那样对MongoDB集群进行外观:第2部分毛笔:锁Kong报告生成器Keyhole是一种用GO(Golang)编写的性能分析工具,用于从MongoDB...

    mongo-lock:使用MongoDB进行分布式锁定

    MongoDB中的分布式锁 该软件包提供了一个Go客户端,用于在MongoDB中创建分布式锁。 设置 使用“ go get”安装软件包。 go get "github.com/square/mongo-lock" 为了使用它,您必须有一个运行有可用于存储锁的集合...

    MongoDB中的一些坑(最好不要用)

    MongoDB 是目前炙手可热的 NoSQL 文档型数据库,它提供的一些特性很棒:如自动 failover 机制,自动 sharding,无模式 schemaless...MongoDB的锁机制和一般关系数据库如 MySQL(InnoDB), Oracle 有很大的差异,InnoDB

    深入云计算 MongoDB管理与开发实战详解pdf.part2

    8.8.4 fsync和锁 8.8.5 从属备份 8.8.6 修复 8.9 本章小结 第4篇 性能篇 第9章 索引 9.1 索引简介 9.1.1 基础索引 9.1.2 文档索引 9.1.3 组合索引 9.1.4 唯一索引 9.1.5 强制使用索引 ...

    MongoDB常见18道面试题及答案.docx

    MongoDB是目前最好的面向文档的免费开源NoSQL数据库。...这些MongoDB NoSQL面试问答涵盖了NoSQL数据库基本的概念,复制(Replication),分片(Sharding),事务和锁,跟踪分析工具(Profiler),Nuances和日志等特性。

    28个MongoDB经典面试题详解

    这些MongoDB NoSQL面试问答涵盖了NoSQL数据库基本的概念,复制(Replication),分片(Sharding),事务和锁,跟踪分析工具(Profiler),Nuances和日志等特性。 让我们看看下面的这些MongoDB NoSQL数据库的面试问答吧: 1...

    Mac下安装配置mongodb并创建用户的方法

    mongodb数据库相信不用多介绍,大家都不陌生,好久之前曾经安装过mongodb,在一个偶然的情况下被一个大佬直接登陆进来了,刚开始惊呼还有这种操作,后来才发现自己并没有给数据库上锁,并且在安装完mongodb默认启动的...

    锁Kong:调查您的Mongo土地-MongoDB性能分析

    锁Kong入路-调查您的Mongo土地 Keyhole是一种用GO(Golang)编写的性能分析工具,用于从MongoDB实例收集统计信息并分析MongoDB集群的性能。 选择Golang是为了消除安装解释器或软件模块的需要。 网志 像带Keyhole的...

    mongodb eval 执行服务器端脚本

    db.eval可以用来模拟多文档事务:db.eval锁住数据库,然后执行javascript,再解锁.虽然没有内置的回滚机制,但这能确保一系列操作按照指定的数序发生. 发送代码有两种方式,封装一个函数或者不封装,如: 代码如下:

    gridfs-locks:基于MongoDB的分布式读写锁,旨在让GridFS安全并发访问

    gridfs-locks gridfs-locks实现了基于分布式,专门用于使 MongoDB 的文件存储。 它是一个建立在之上的 ,并且兼容原生的实现。 注意:如果您使用并需要此包的锁定功能(并且您可能这样做...请参阅本自述文件底部的...

    MongoDB整库备份与还原以及单个collection备份、恢复方法

    mongodump.exe备份的原理是通过一次查询获取当前服务器快照,并将快照写入磁盘中,因此这种方式保存的也不是实时的,因为在获取快照后,服务器还会有数据写入,为了保证备份的安全,同样我们还是可以利用fsync锁使...

    gameserver:gameserver 是一个分布式java游戏框架,基于Spring+netty+redis+自定义rpc(或者dubbo)+MyBatis + mysql(或者mongodb)实现

    socket通信实现,登录系统,玩家线程模型,db系统,分布式id生成器,分布式锁,缓存系统,热更新机制,rpc系统,全服组队等等组织结构gameserver├── client -- 测试用netty客户端├── rpc-api -- 提供分布式的rpc基础├...

    proplock:用于防止由于在 mongodb 中传播而导致数据损坏的实用程序,以增强并发控制

    mongoose.connect('mongodb://&lt;\your.mongo.url&gt;', function(err, db) { if (err) throw err; console.log("Some Kind of affirmation"); }); /* to enforce concurrency */ problock.lock(mongoose

    mongodb-java:模仿monogdb写一个数据库

    Fsync锁,数据修复 用户管理,安全认证 主从复制 副本集(复制) 分片 学习MongoDB 练习环境 MongoDB v3.6.2 简介: MongoDB 是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据...

    Python操作redis和mongoDB的方法

    一、操作redis redis是一个key-value存储系统,value的类型包括string(字符串),list(链表),set(集合),zset(有序集合),hash(哈希类型)。为了保证效率,数据都是缓冲在内存中,在处理大规模数据读写的场景下运用...

    mongo-tx:适用于nodejs的灵活,可扩展的mongodb事务库

    灵活且可扩展的mongodb事务库。 安装 npm i --save mongo-tx 要么 yarn add mongo-tx 如何 介绍 创建事务并运行它时,需要在事务期间使用模型包装器修改数据,每个模型包装器将在查找/查找一个/创建/修改/删除...

Global site tag (gtag.js) - Google Analytics