3、 如何查看锁的状态
db.serverStatus()
db.currentOp()
mongotop
mongostat
the MongoDB Monitoring Service (MMS)
4、 哪些操作会对数据库产生锁?
下表列出了常见数据库操作产生的锁。
Changed in version 2.2.
Operation Lock Type
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()
Deprecated since version 3.0.
Write lock. The db.eval() method takes a global write lock while evaluating the JavaScript function. To avoid taking this global write lock, you can use the eval command with nolock: true.
eval
Deprecated since version 3.0.
Write lock. By default, eval command takes a global write lock while evaluating the JavaScript function. If used with nolock: true, the eval command does not take a global write lock while evaluating the JavaScript function. However, the logic within the JavaScript function may take write locks for write operations.
aggregate() Read lock
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
db.copyDatabase() 锁定整个mongod实例
db.repairDatabase() 会获取全局写锁,运行期间会阻塞其它操作。
Journaling 内部操作,短时间锁定所有数据库,所有的数据库共享一个journal。
User authentication requires a read lock on the admin database for deployments using user credentials.
For deployments 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 then the local database for a short time.
The lock for the local database allows the mongod to write to the primary’s oplog and accounts for a small portion of the total time of the operation.
7、 JavaScript 操作的并发
Changed in version 2.4: The V8 JavaScript engine added in 2.4 allows multiple JavaScript operations to run at the same time.
Prior to 2.4, a single mongod could only run a single JavaScript operation at once.
Reads can see data which may subsequently be rolled back in rare cases such as replica set failover or power loss.
It does not mean that read operations can see documents in a partially written or otherwise inconsistent state.
Other systems refer to these semantics as read uncommitted.