MongoDB is one of the most popular open-source document NoSQL databases. In this tutorial we will learn the following things:
- Installation of MongoDB
- Creating data directory
- Starting and Stopping MongoDB
- Running Shell Commands
Installation of MongoDB
OSX installation
We can install MongoDB on OSX (Mac OS) in two ways:
- Using HomeBrew Installer
- Manual Installation
HomeBrew Installation
Reader system should have HomeBrew Installed and brew command available on PATH.
Step 1: Update the HomeBrew Installation Package Repository
$ brew update
Step 2: Install MongoDB
(a) Install Binaries
$ brew install mongodb
(b) Install MongoDB from source with TLS/SSL support
$ brew install mongodb --with-openssl
(c) Install MongoDB edge releases (Still in development, can have bugs)
$ brew install mongodb --devel
Manual Installation
Step 1: Download Mac Binaries from the MongoDB download page.
MongoDB Download Web Page as of 18 June 2015 |
You can also download the binaries using the given curl command:
curl -O https://fastdl.mongodb.org/osx/mongodb-osx-x86_64-3.0.4.tgz
You can change the MongoDB version 3.0.4
with relevant one.
Step 2: Unzip (Extract) compressed package in a folder
Unzipped MongoDB App Package |
Step 3: Copy the extracted folder at some location where you wanted to keep your MongoDB and rename it to MongoDB (You are free to choose a folder name you like)
Renamed MongoDB App Package |
Step 4: MongoDB binaries executable like mongo etc should be on PATH so we can access that from any directory
Please read this tutorial for setting up the PATH on Mac OSX.
This ends the MongoDB installation.
Data storage, starting and stopping MongoDB
mongod
command in bin directory is used to run the MongoDB server. mongod
takes several parameters to configure the behavior of the server according to our needs.
Important: MongoDB has two different types of database storage engines.
- mmapv1 (default)
- wiredTiger
One data directory belongs to only one storage engine. If we force to use a different storage engine (–storageEngine option) for the data directory, the server will refuse to start.
$ mongod
By default data directory is /data/db/
and storage engine is mmapv1
. Hence above command is equivalent to:
$ mongod --dbpath "/data/db/" --storageEngine "mmapv1"
We can change the data directory (say /data/mongodb/db/
) as well as a storage engine to wiredTiger
, using startup command:
$ mongod --dbpath "/data/mongodb/db/" --storageEngine "wiredTiger"
2015-06-19T11:53:05.906+0530 I JOURNAL [initandlisten] journal dir=/data/db/journal
2015-06-19T11:53:05.907+0530 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed
2015-06-19T11:53:05.920+0530 I JOURNAL [durability] Durability thread started
2015-06-19T11:53:05.920+0530 I CONTROL [initandlisten] MongoDB starting : pid=31488 port=27017 dbpath=/data/db 64-bit host=GEEKMJ-RT-MBP.local
2015-06-19T11:53:05.920+0530 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-06-19T11:53:05.920+0530 I JOURNAL [journal writer] Journal writer thread started
2015-06-19T11:53:05.920+0530 I CONTROL [initandlisten]
2015-06-19T11:53:05.920+0530 I CONTROL [initandlisten]
2015-06-19T11:53:05.920+0530 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2015-06-19T11:53:05.920+0530 I CONTROL [initandlisten] db version v3.0.3
2015-06-19T11:53:05.920+0530 I CONTROL [initandlisten] git version: b40106b36eecd1b4407eb1ad1af6bc60593c6105
2015-06-19T11:53:05.920+0530 I CONTROL [initandlisten] build info: Darwin bs-osx108-7 12.5.0 Darwin Kernel Version 12.5.0: Sun Sep 29 13:33:47 PDT 2013; root:xnu-2050.48.12~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
2015-06-19T11:53:05.920+0530 I CONTROL [initandlisten] allocator: system
2015-06-19T11:53:05.920+0530 I CONTROL [initandlisten] options: {}
2015-06-19T11:53:06.008+0530 I NETWORK [initandlisten] waiting for connections on port 27017
In simple mongod
command run MongoDB server with default values for all configuration options. MongoDB server is running in the foreground (interactive mode).
To stop the MongoDB server, we should press command + c
, it will gracefully stop the server.
Running MongoDB server in the background (as daemon or fork) and stopping
Starting MongoDB in background as Daemon (or fork):
$ mongod --fork --logpath "/var/log/mongodb.log"
For graceful shutdown of MongoDB server (use anyone):
$ mongod --shutdown
Note: Above command only works in the Linux environment.
$ mongo
MongoDB shell version: 3.0.3
connecting to: test
> use admin
switched to db admin
> db.shutdownServer()
$ kill <mongodb-process-id>
Note Never use :
$ kill -9 <mongodb-process-id>
It terminates the MongoDB process and you may have an unexpected outcome.
mongod options
We can see all the options and sub-command available for mongod by running the command
$ mongod --help
Options:
General options:
-h [ --help ] show this usage information
--version show version information
-f [ --config ] arg configuration file specifying additional options
-v [ --verbose ] [=arg(=v)] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
--quiet quieter output
--port arg specify port number - 27017 by default
--bind_ip arg comma separated list of ip addresses to listen on
- all local ips by default
--ipv6 enable IPv6 support (disabled by default)
--maxConns arg max number of simultaneous connections - 1000000
by default
--logpath arg log file to send write to instead of stdout - has
to be a file, not directory
--syslog log to systems syslog facility instead of file
or stdout
--syslogFacility arg syslog facility used for mongodb syslog message
--logappend append to logpath instead of over-writing
--logRotate arg set the log rotation behavior (rename|reopen)
--timeStampFormat arg Desired format for timestamps in log messages.
One of ctime, iso8601-utc or iso8601-local
--pidfilepath arg full path to pidfile (if not set, no pidfile is
created)
--keyFile arg private key for cluster authentication
--setParameter arg Set a configurable parameter
--httpinterface enable http interface
--clusterAuthMode arg Authentication mode used for cluster
authentication. Alternatives are
(keyFile|sendKeyFile|sendX509|x509)
--nounixsocket disable listening on unix sockets
--unixSocketPrefix arg alternative directory for UNIX domain sockets
(defaults to /tmp)
--filePermissions arg permissions to set on UNIX domain socket file -
0700 by default
--fork fork server process
--auth run with security
--noauth run without security
--jsonp allow JSONP access via http (has security
implications)
--rest turn on simple rest api
--slowms arg (=100) value of slow for profile and console log
--profile arg 0=off 1=slow, 2=all
--cpu periodically show cpu and iowait utilization
--sysinfo print some diagnostic system information
--noIndexBuildRetry dont retry any index builds that were
interrupted by shutdown
--noscripting disable scripting engine
--notablescan do not allow table scans
Replication options:
--oplogSize arg size to use (in MB) for replication op log. default is
5% of disk space (i.e. large is good)
Master/slave options (old; use replica sets instead):
--master master mode
--slave slave mode
--source arg when slave: specify master as
--only arg when slave: specify a single database to replicate
--slavedelay arg specify delay (in seconds) to be used when applying
master ops to slave
--autoresync automatically resync if slave data is stale
Replica set options:
--replSet arg arg is [/]
--replIndexPrefetch arg specify index prefetching behavior (if secondary)
[none|_id_only|all]
Sharding options:
--configsvr declare this is a config db of a cluster; default port
27019; default dir /data/configdb
--shardsvr declare this is a shard db of a cluster; default port
27018
Storage options:
--storageEngine arg (=mmapv1) what storage engine to use
--dbpath arg directory for datafiles - defaults to /data/db
--directoryperdb each database will be stored in a separate
directory
--noprealloc disable data file preallocation - will often
hurt performance
--nssize arg (=16) .ns file size (in MB) for new databases
--quota limits each database to a certain number of
files (8 default)
--quotaFiles arg number of files allowed per db, implies --quota
--smallfiles use a smaller default file size
--syncdelay arg (=60) seconds between disk syncs (0=never, but not
recommended)
--upgrade upgrade db if needed
--repair run repair on all dbs
--repairpath arg root directory for repair files - defaults to
dbpath
--journal enable journaling
--nojournal disable journaling (journaling is on by default
for 64 bit)
--journalOptions arg journal diagnostic options
--journalCommitInterval arg how often to group/batch commit (ms)
WiredTiger options:
--wiredTigerCacheSizeGB arg maximum amount of memory to allocate
for cache; defaults to 1/2 of physical
RAM
--wiredTigerStatisticsLogDelaySecs arg (=0)
seconds to wait between each write to a
statistics file in the dbpath; 0 means
do not log statistics
--wiredTigerJournalCompressor arg (=snappy)
use a compressor for log records
[none|snappy|zlib]
--wiredTigerDirectoryForIndexes Put indexes and data in different
directories
--wiredTigerCollectionBlockCompressor arg (=snappy)
block compression algorithm for
collection data [none|snappy|zlib]
--wiredTigerIndexPrefixCompression arg (=1)
use prefix compression on row-store
leaf pages
Checking mongoDB version:
$ mongod --version
db version v3.0.3
git version: b40106b36eecd1b4407eb1ad1af6bc60593c6105
MongoDB mongod configurations in the external file
For better management of the MongoDB server, we need an external configuration file for all the options which we provide to mongod
command as parameters.
A YAML format configuration file is used.
MongoDB mongod
and mongos
YAML based configuration same given below:
systemLog:
verbosity: 0
quiet: false
traceAllExceptions: false
syslogFacility: false
path: "/var/log/mongodb.log"
logAppend: false
#logRotate can be either "rename" or "reopen"
logRotate: "rename"
destination: "file"
timeStampFormat: "iso8601-local"
net:
bindIp: 127.0.0.1
port: 27017
maxIncomingConnections: 65536
storage:
dbPath: "/data/db"
engine" "wiredTiger"
setParameter:
enableLocalhostAuthBypass: false
...
Starting server with configuration: (Say config file is at /conf/mogodb.cnf)
$ mongod --config "conf/mongodb.cnf"
or
$ mongod -f "conf/mongodb.cnf"
Interacting with MongoDB using the mongo shell
We can connect to interactive mongo
shell without any hassle if the MongoDB server is running on localhost.
$ mongo
It results in an interactive shell where we can issue commands.
MongoDB shell version: 3.0.3
connecting to: test
>
Basic mongo shell commands
1. Display the current database name
> db
test
2. Display the list of databases name
> show dbs
test
3. Switching & Creating a new database and creating a collection
There is no explicit command to create MongoDB database. It will be created when we save a document first time in a collection.
>show dbs
local 0.000GB
>use db geekmjdb
> use geekmjdb
switched to db geekmjdb
> show dbs
local 0.000GB
> db.testCollection.save ({col1:"col1val",col2:"col2val"})
WriteResult({ "nInserted" : 1 })
> db.testCollection.find()
{ "_id" : ObjectId("55845fdff7febc6f192ddcc9"), "col1" : "col1val", "col2" : "col2val" }
> show dbs
geekmjdb 0.000GB
local 0.000GB
We had seen changing database using use geekmjdb
doesn’t created database. It got created when we added our first record (document) to a collection.
Collection is also created implicitly when the first document is added.
Though there is an explicit method for creating capped collection.