最後更新: 2018-11-16



Document database

MongoDB is a Document database

that provides high performance, high availability, and automatic scaling.

A record in MongoDB is a document

which is a data structure composed of field and value pairs. (similar to JSON objects)

high availability AND automatic scaling

Replica set (a group of MongoDB servers that maintain the same data set), provides:

  • automatic failover
  • data redundancy

Sharding distributes data across a cluster of machines.

Storage Engine

  • WiredTiger Storage Engine (including support for Encryption at Rest)
  • In-Memory Storage Engine
  • MMAPv1 Storage Engine





A metapackage that will automatically install the four component packages listed below.

mongodb-org-server (mongod)

mongodb-org-mongos (mongos daemon)

mongodb-org-shell (mongo shell)

mongodb-org-tools (mongoimport bsondump, mongodump, mongoexport, mongofiles, mongorestore, mongostat, and mongotop)





name=MongoDB Repository

yum install mongodb-org

service mongod start

systemctl enable mongod




Main Config


stores its data files in ( storage.dbPath )


log files in ( systemLog.path )


user account


Listen port


Check Version



mongo --host

> db.version()


mongod --version





  • /sys/kernel/mm/transparent_hugepage/enabled
  • /sys/kernel/mm/transparent_hugepage/defrag


if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
   echo never > /sys/kernel/mm/transparent_hugepage/enabled
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
   echo never > /sys/kernel/mm/transparent_hugepage/defrag




data bearing nodes

  • primary node (only one)
  • secondary nodes

primary node

receives all write operations
records all changes to its data sets in its operation log (oplog)

secondary nodes

replicate the primary 's oplog and apply the operations to their data sets
If the primary is unavailable(electionTimeoutMillis: 10000 (10 seconds)),
an eligible secondary will hold an election to elect itself the new primary.
The replica set cannot process write operations until the election completes successfully.
Secondaries apply operations from the primary asynchronously.

MongoDB drivers (3.6+)

can detect the loss of the primary and automatically retry certain write operations a single time

Read Operations

By default, clients read from the primary
however, clients can specify a read preference to send read operations to secondaries.
( Asynchronous replication to secondaries means that reads from secondaries may return data that does not reflect the state of the data on the primary.)


It do not maintain a data set.
The purpose of an arbiter is to maintain a quorum in a replica set
by responding to heartbeat and election requests by other replica set members.
If your replica set has an even number of members,
add an arbiter to obtain a majority of votes in an election for primary.
(Arbiters do not require dedicated hardware.)

Write Concern

describes the level of acknowledgement requested from MongoDB for write operations to
a standalone mongod or to replica sets or to sharded clusters.
In sharded clusters, mongos instances will pass the write concern on to the shards.

{ w: <value>, j: <boolean>, wtimeout: <number> }


w: 1 (default)

Requests acknowledgement that the write operation has propagated to the standalone mongod or the primary in a replica set.

w: N

valid only for replica sets to request acknowledgement from specified number of members, including the primary.

w: "majority"

Requests acknowledgement that write operations have propagated to the majority of voting nodes, including the primary.

j option

requests acknowledgement from MongoDB that the write operation has been written to the journal

j: true
does not by itself guarantee that the write will not be rolled back due to replica set primary failover.
MongoDB returns only after the requested number of members, including the primary, have written to the journal.