Configuring and Starting MongoDB Server

As mentioned in the installation "mongod" file will works as a MongoDB server. So we need to run the mongod binary file or we need to start the service of mongod.

>_ Running MongoDB as a Non-Service Mode.

mongod is the primary daemon process for the MongoDB system. It handles data requests, manages data access, and performs background management operations.

Basic syntax of mongod is as follows

mongod options

Options List

Option Description
--version Returns the mongod release number
--config <filename>,
-f <filename>
A configuration file for runtime configuration options (only ASCII encoding file)
--verbose, -v Internal reporting will be returned on standard output
--port <port> The TCP port on which the MongoDB instance listens for client connections.Default: 27017
--bind_ip <ip address> The IP address that mongod binds to in order to listen for connections from applications
--maxConns <number> The maximum number of simultaneous connections that mongod will accept.
--logpath <path> Sends all logging information to a log file instead of to standard output. MongoDB will create a file if not exist but not folder structure.
--dbpath <path> The directory where the mongod instance stores its data.Default: /data/db on Linux and OS X, \data\db on Windows

Let’s view some examples for the usage of the above options

>_ Starting MongoDB without options
  1. Execute 'mongod' file existed in bin folder of mongodb
  2. The above binary will run if the db folder is existed in the path
    • for windows c:/mongodb
    • for linux /var/lib/mongodb ( or /etc/mongod.conf details)
      (We need to create above directory if not present, else mongod server will not run)
>_ Starting MongoDB in the port 24936 instead of 27017
mongod--port 24936
>_ Starting MongoDB using the db location at “d:/mymongo/db” and logs location at "d:/mymongo/logs"
  • Before starting mongod for this the above folder structure we need to create directory structure like in d: need create mymongo,db,logs directories.
  • mongod --logpath “d:/mymongo/logs” --dbpath “d:/mymongo/db”.

>_ Running MongoDB as a Service.

In Windows OS

  1. In Windows Creating and Starting service.
    • Create a .cfg file and mention the systemLog.path and storage.path.
    • Assuming creating mymongod .cfg file in path d:\mymongo\mymongod.cfg.
    • Sample mymongod.cfg file data
      destination: file
      path: c:\mymongo\log\mongod.log
      dbpath: d:\mymongo\data\db
  2. Creating/Installing service
    • "C:\mongodb\bin\mongod.exe" --config "d:\mymongo\mymongod.cfg" --install
  3. Starting server if Service installed
    • net start MongoDB
  4. Stopping MongoDB server
    • Net stop MongoDB

In Linux Machines

  1. Starting MongoDB
    • sudo service mongod start
  2. Starting MongoDB
    • sudo service mongod stop
  3. Restarting MongoDB
    • sudo service mongod restart