MongoDB Setup – Installation, Configure and Set up Windows Service

MongoDB Setup – Installation, Configure and Set up Windows Service

Installation on Windows

1. Downloading MongoDB

MongoDB offers several binary distributions, both in 32 & 64-bit flavors for various operating systems. They may be found at the following URL: http://www.mongodb.org/downloads or from their main website click the on downloads link.

2. Creating the Data Directory

By default MongoDB stores its data in C:\data\db directory, however, since mongo does not automatically create it for you it may be necessary to manually create this data directory.

data_dirUsing a DOS command prompt, we can issue the make directory (mkdir) command to create the necessary directory structure.    To ensure that the directory was created correctly, I simply change directory (cd) to the newly created directory structure.

3. Running MongoDB on Windows

On my system, mongoDB was installed in C:\Program Files\MongoDB\2.6.11.   You will need to ensure you are in the bin folder.   Start the MongoDB daemon process for the system by executing the file mongod.exe.   This will handle all data requests, manage the format of the data, and perform the necessary background data management.

Starting the MongoDB in Windows as a Service

1. Create Configuration File

Create a configuration file in called mongod.cfg or any name or your choosing. Please ensure you specify both a path to the database files dbpath and log file logpath.

2. Sample Configuration file (mongod.cfg)

dbpath=C:\data\db
logpath=C:\data\logs\mongo.log

3. Starting the MongoDB Server

To start the MongoDB database server with default settings (ie: default dbpath C:\data\db) or logging to stdout.

C:\Program Files\MongoDB\2.6.11\bin>mongod.exe

You can choose to set the database path to a directory other the default path of C:\data\db by using the –dbpath argument and instead of using stdout for the logging you can choose to create log files in log_directory by using the –logpath argument.

C:\Program Files\MongoDB\2.6.11\bin>mongod.exe 
   --dbpath <db_directory> --logpath <log_file> 

Showing help available for mongod.exe

To show a list of all of the arguments available for mongod.exe process,use the command mongod.exe –help Note: Shown below is just a partial list of all of the help options available.

C:\Program Files\MongoDB\2.6.11\bin>mongod.exe --help
Options:

General options:
  -h [ --help ]            show this usage information
  --version                show version information
  -f [ --config ] arg      configuration file specifying additional options
  -v [ --verbose ]         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
  --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
  --logappend              append to logpath instead of over-writing
  --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)
  --auth                   run with security
  --noauth                 run without security
  ...

3. Installing MongoDB as a Service

Install the MongoDB service by starting mongod with the –install option and the –config option to specify the configuration file, in our example mongod.cfg. Please Note: You may need to use double quotes “” around the entire directory/logfile as it may contain blanks.

cd  <MONGODB_INSTALLATION_DIRECTORY>
mongod.exe --config "C:\Program Files\MongoDB\2.6.11\bin\mongod.cfg" --install

Component Service

mongod_service_setup

4. Starting MongoDB Service

To start the MongoDB service use the following command:

net start MongoDB

5. Stopping MongoDB Service

To stop the MongoDB service use the following command:

net stop MongoDB

6. Removing the MongoDB Service

To Remove the MongoDB service from Windows use the following command:

mongod.exe --remove

That’s It!

I hope you enjoyed this tutorial. It was certainly a lot of fun putting it together and testing it out. Please continue to share the love and like us so that we can continue bringing you quality tutorials. Happy Coding!!!

mongodb_basics_tutorial

Related Posts

Please Share Us on Social Media

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *