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

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

Server

yum install mongodb-org

service mongod start

systemctl enable mongod

Client

yum install mongodb-org-shell

 


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] Server Version

mongod --version

[2] Client Version

mongo --version

[3] Remote Server Version

mongo --host 127.0.0.1:27017

> db.version()

3.6.9

 


mongo

 

The mongo shell is an interactive JavaScript interface to MongoDB

Default port 27017/TCP

Authentication

  • --username USER
  • --password PW
  • --authenticationDatabase DB

Basic CLI

# To list the databases available to the user

show dbs

inspect  0.010GB

# To display the database you are using

db

inspect

# Change db

use <database>

# Print a list of all collections for current database.

show collections

# Show all contents from all collections

db.CollectionName.find()

db.CollectionName.find().pretty()

# Exit the Shellicons/link.png

To exit the shell, type quit() or use the <Ctrl-C> shortcut.

Connect to a MongoDB Replica Set

mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]

/defaultauthdb

The authentication database to use if the connection string includes username:password@ authentication credentials

authSource=STRING

Specify the database name associated with the user's credentials.

If authSource is unspecified, authSource defaults to the defaultauthdb

?replicaSet=STRING

Specifies the name of the replica set, if the mongod is a member of a replica set.

i.e.

mongo "mongodb://User:Pass@Node-A:27017,Node-B:27017/MyDB?replicaSet=replA"

 


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

 


Authentication

 

/etc/mongod.conf

# Default: disabled
security:
  authorization: enabled

SCRAM-SHA-1 is the default authentication mechanism for versions of MongoDB newer than 3.0.

 


Debug

 

db.setProfilingLevel(2)

    Level 0

the profiler is off, does not collect any data. mongod always writes operations longer than the slowOpThresholdMs threshold to its log.

This is the default profiler level.

    Level 1

collects profiling data for slow operations only. By default slow operations are those slower than 100 milliseconds.

You can modify the threshold for “slow” operations with the slowOpThresholdMs runtime option or the setParameter command.

See the Specify the Threshold for Slow Operations section for more information.

    Level 2

collects profiling data for all database operations.

db.getProfilingLevel()

 


Doc