摘抄自:http://francs3.blog.163.com/blog/static/405767272012112811268129/
今天了解了 MongoDB 的锁机制,与传统 RDMBS MVCC 机制相差较大,它在一些方面的特性
比较特殊,下面简单介绍下:
--1 MongoDB 使用的锁
MongoDB 使用的是“readers-writer”锁, 可以支持并发但有很大的局限性,当一个读锁存在,许多
读操作可以使用这把锁,然而, 当一个写锁的存在,一个单一的写操作会 exclusively 持有该锁,同时
其它读,写操作不能使用共享这个锁;举个例子,假设一个集合里有 10 个文档,多个 update 操作不能
并发在这个集合上,即使是更新不同的文档。
--2 锁的粒度
在 2.2 版本以前,mongod 只有全局锁;在 2.2 版本开始,大部分读写操作只锁一个库,相对之前版本,
这个粒度已经下降,例如如果一个 mongod 实例上有 5 个库,如果只对一个库中的一个集合执行写操作,那
么在写操作过程中,这个库被锁;而其它 5 个库不影响。相比 RDBMS 来说,这个粒度已经算很大了!
--3 如何查看锁的状态
db.serverStatus()
db.currentOp()
mongotop
mongostat
the MongoDB Monitoring Service (MMS)
--4 哪些操作会对数据库产生锁?
下表列出了常见数据库操作产生的锁。
Issue a query | Read lock |
Get more data from a cursor | 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. db.eval() blocks all other JavaScript processes. |
eval | Write lock. If used with the nolock lock option, the eval option does not take a write lock and cannot write data to the database. |
aggregate() | Read lock |
--5 哪些数据库管理操作会锁数据库?
某些数据库管理操作会 exclusively 锁住数据库,以下命令需要申请 exclusively 锁,并锁定一段时间
db.collection.ensureIndex(),
reIndex,
compact,
db.repairDatabase(),
db.createCollection(), when creating a very large (i.e. many gigabytes) capped collection,
db.collection.validate(),
db.copyDatabase().This operation may lock all databases
以下命令需要申请 exclusively 锁,但锁定很短时间。
db.collection.dropIndex(),
db.collection.getLastError(),
db.isMaster(),
rs.status() (i.e. replSetGetStatus,)
db.serverStatus(),
db.auth(), and
db.addUser().
备注:可见,一些查看命令也会锁库,在比较繁忙的生产库中,也会有影响的。
--6 锁住多个库的操作
以下数据库操作会锁定多个库。
db.copyDatabase() must lock the entire mongod instance at once.
Journeying, 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.
--7 参考
http://docs.mongodb.org/manual/faq/concurrency/
http://blog.163.com/dazuiba_008/blog/static/36334981201211112221314/
相关推荐
MongoDB权威指南:MongoDB:The Definitive Guide第一版 第二版 中、英文4本合集
《Windows上安装MongoDB:完整步骤详解.pdf》是一份适用于Windows操作系统的MongoDB安装教程,适用于初学者和有经验的开发人员。本教程包括完整的步骤详解,可帮助您快速掌握MongoDB的安装配置,包括环境变量设置,...
深入学习MongoDB:Scaling MongoDB && 50 Tips and Tricks for MongoDB Developers深入学习MongoDB中文版Scaling MongoDB英文版50 Tips and Tricks for MongoDB Developers英文版高清完整目录3本打包合集
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:registered:有线协议,允许通过现有的MongoDB:registered:客户端绑定使用MongoDB:registered:API。 所有持久数据都存储在FoundationDB键值存储中。 文档层实现了MongoDB:registered:API(v ...
描述 PM2模块可自动监视mongodb的生命体征: 查询,输入,更新,删除 连接数 已用存储空间 网络速度(输入和输出) 代表名称和状态 pm2-mongodb ... pm2 set pm2-mongodb:ip 42.42.42.42 (我的mongod
NULL 博文链接:https://study121007.iteye.com/blog/2307592
docker run -d --name mongodb -p 27017:27017 -v /data/mongodb:/var/lib/mongodb anapsix/mongodb 作为客户: docker run -it --rm anapsix/mongodb mongo --help 配置 您可以像通常那样通过命令行传递配置选项...
mongodb人偶模块 目录 概述 从OS存储库或从MongoDB社区/企业存储库在RHEL / Ubuntu / Debian上安装MongoDB。 模块说明 ...如果只想使用默认选项安装服务器,则可以运行include mongodb::server 。
mongoDB:MongoDB备忘单
Spring集成MongoDB官方指定jar包:spring-data-mongodb-1.4.1.RELEASE.jar
Written by engineers from 10gen, the company that develops and supports this open source database, MongoDB: The Definitive Guide provides guidance for database developers, advanced configuration for ...
羽毛mongodb 用于数据库适配器,使用用于。 $ npm install --save mongodb feathers-mongodb ... connect ( 'mongodb://localhost:27017/feathers' ) . then ( client => { app . use ( '/messages' ,
安装$ npm install mqemitter-mongodb --save例子var mongodb = require ( 'mqemitter-mongodb' )var mq = mongodb ( { url : 'mongodb://127.0.0.1/mqemitter?auto_reconnect'} )var msg = { topic : 'hello world'...
是一个数据分析和可视化中间件,旨在帮助您快速构建自定义的嵌入式数据分析应用程序。... "dsn" : "mongodb://localhost:27017/joola" } } } } 你们都准备好了。 为确保它正常工作,请运行一个节点并监视日志以查
Stitch SDK弃用通知Stitch SDK功能已移至Realm SDK。 使用之一,可以将使用... 安卓将以下内容添加到您的应用程序模块的build.gradle中: implementation ' org.mongodb:stitch-android-sdk:4.7.0 ' 还将以下内容添加到
MoniDB Yii 2扩展该扩展为提供了集成。 有关许可证信息,请... 'components' => [ 'mongodb' => [ 'class' => '\yii\mongodb\Connection' , 'dsn' => 'mongodb://localhost:27017/mydatabase' , 'options' => [ "user
用于DartORM的MongoDB适配器。 用法示例 import 'package:dart_orm/dart_orm.dart' as ORM; import 'package:dart_orm_adapter_mongodb/... 'mongodb://$ mongoUser :$ mongoPass @127.0.0.1/$ mongoDBName ' ); awa
Written by current and former members of the MongoDB team, the third edition is updated for MongoDB 4.0. You’ll find substantial updates on querying, indexing, aggregation, replica sets, ops manager,...
日志日志 MongoDB MongoDB 日志传输 安装: npm install -S loglog-mongodb ... connection : 'mongodb://my_host/my_db' , collection : 'logs' // (default) // See all options for db.createCollection