How to start & stop a mongo DB server from the command line

While starting the Mongo DB server from the command line we have various options that can be used to configure the Mongo DB server as per our requirement. Here we will learn different options with which we can start the mongo DB server.

1. Start mongo DB with default values

By default, MongoDB listens for connections from clients on port 27017, and stores data in the /data/db directory.

On Windows, this path is on the drive from which you start MongoDB. For example, starting a MongoDB server on the C:\ drive stores all data files in C:\data\db.

If C:\data\db folder is not exist then create it before running below command

mongod
Start Mongo db service with default parameter
Start mongo db service
Connecting mongo db server
Connecting mongo db server

2. Start mongo DB with custom db path

To specify a dbPath for mongod to use as a data directory, use the --dbpath option. The following invocation will start a mongod instance and store data in the /srv/mongodb path

mongod --dbpath /user/mongodb/
mongod --dbpath D:\mongoData\

3. Start mongo DB with custom port

To specify a port to mongod, use the --port option on the command line. The following command starts mongod listening on port 12345:

mongod --port 12345
Start mongo Db server at custom port
Start mongo d server with custom port

4. Start mongo DB with custom log path

If you donot want to show log in terminal then you can give you log path using --logpath option on the command line. The following invocation will start a mongod instance and store logs in the /log/log.txt path

You need to create log folder before passing on command. It will create automatically file given by you

mongod --logpath /log/log.txt
mongod --logpath C:\log\log.txt
Start mongo db server with log path
Start mongo db server with log path

5. Start mongod as a Daemon or as Service

To run a mongod process as a daemon (i.e. fork), and write its output to a log file, use the --fork and --logpath options. You must create the log directory; however, mongod will create the log file if it does not exist.

The following command starts mongod as a daemon and records log output to /var/log/mongod.log.

--fork command option will not work on window cmd

mongod --fork --logpath /var/log/mongod.log

Stop mongo DB Server

If you want to stop mongo db process

Shut down the mongod from the mongo shell using the db.shutdownServer() method as follows:

Below command will work for both window and Linux base OS.

use admin
db.shutdownServer()
Shutdown mongo DB server

Below command will work only Linux only.

mongod --shutdown

6. Start mongo DB as a window service

While intalling mongoDB it gives option to install mongodb service. If you donot have mongo db as service then you can install it. Use --install as mongod command option and --logpath options. You must create the log directory; however, mongod will create the log file if it does not exist.

mongod --install --logpath D:\log\mlog.txt
Install mongo Db window serivce
Install mongo db service
Mongo Db Service
Mongo Db service

You can also give other options like --dbpath, --port along with above command.

Start & Stop mongo Db service

After above command your service will install in window. you can check it in task manager. Now start your service.

net start MongoDB // To start
net stop MongoDB // To stop
Start Mongo DB service
Start Mongo DB service
Stop Mongo DB service
Stop mongo DB service

If you want to remove your mongo DB service, simply run below command

mongod --remove
Remove mongo db window service
Remove mongo db service
sc.exe delete MongoDB
Delete Mongo Db window service
Delete mongo db service

All above command should be run as administrator.

Reference : MongoDB

Leave a Reply