Starting the MongoDB
MongoDB is started with a mongod executable file, it
has many configurable options which can be viewed with mongod--help on the command
line
We
will discuss a few important of them below
--dbpath
Each
mongod process needs its own data directory. if you are running multiple
instances of mongod on the same machine then you need a separate directory for each instance
When
mongod starts up, it creates mongod.lock in the data directory, which prevents any
other mongod process from using the same directory.
--port
By
default, mongod uses a 27017 port for the server to listen. In case another
instance is required to be run on same machine then different port needs to be assigned.
--fork
Fork
the server process, running MongoDB as daemon
--logpath
This
will create a logfile in the directory specified instead of writing everything on the command line, it will overwrite the existing logfile if --logappend option is not specified in addition to --logpath. Before overwriting it take a backup of the existing logfile with the timestamp
--config
Use
configuration file for additional options not specified on the command line such as
mongo --config ~/.mongodb.conf to read the configuration file
So
if we want to run mongo on port 27027 with data directory D:\mongodb\data\db\logs
and logfile directory D:\mongodb\data\db\logs we will use the below command
mongod
--dbpath=D:\mongodb\data\db --port 27027 --logpath
D:\mongodb\data\db\logs\mongodblog.log
Below
are commands executed and their output on the command line
Once
we start mongod with options as mentioned above, it creates lock file and
log file

mongod.lock file in dbpath location
Logfile

Stopping the MongoDB
There
are a couple of ways of stopping MongoDB
- First and basic way to press Ctrl-C, It will send SIGINT or SIGTERM signal.
- Another way is to use the kill command
if PID of the mongod process is xyz then kill -2 xyz will do
the same Ctrl-C. (never use kill -9 as you will need to repair the database on the next startup as there are chances of corruption on datafiles)

Comment below if you need any additional
information
0 comments:
Post a Comment