mongodb

最後更新: 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

 


Package

 

mongodb-org

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)

 


Install

 

/etc/yum.repos.d/mongodb-org-3.6.repo

[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc

yum install mongodb-org

service mongod start

systemctl enable mongod

 


Config

 

Main Config

/etc/mongod.conf

stores its data files in ( storage.dbPath )

/var/lib/mongo

log files in ( systemLog.path )

/var/log/mongodb

user account

mongod

Listen port

127.0.0.1:27017

 


Check Version

 

[1]

mongo --host 127.0.0.1:27017

> db.version()
3.6.9

[2]

mongod --version

 


Turning

 

transparent_hugepage

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

/etc/rc.local

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

 


HA

 

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.)

Arbiters

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:

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.

wtimeout

 


Doc