Running MongoDB on Windows

MongoDbLogoI’ve been working with MonogDB lately. Getting it setup and running on a development box is easy, but I wanted to document the steps for reference.

Install MongoDB

Download MongoDB from their website. For windows, I prefer the 64-bit Zip file. The MSI installs into Program Files, but I prefer to keep the binaries and the databases together and not have to run as an Admin.

Extract the Zip file to c:\MongoDB

In the MongoDB directory, create a data subdirectory and in it add db and log subdirectories. The bin subdirectory should already be there. Your directory structure should now look like this,

c:\mongodb
    \bin
    \data
        \db
        \log

Configure MongoDB

Add a text file called  c:\MongoDB\mongod.cfg that contains the following,

systemLog:
   destination: file
   path: c:\mongodb\data\log\mongodb.log
   logAppend: true
storage:
    dbPath: c:\mongodb\data\db
net:
   bindIp: 127.0.0.1
   port: 27017

This sets the log and the db path and also sets the port to the default so it is easy to change later. Notice that I am only binding to 127.0.0.1 because my server is currently insecure so I do not want to expose it on the network. For more configuration options, see the documentation.

Test MongoDB

Now that I have it set up, it is time to test the installation. Start it at the command prompt like this,

C:\MongoDb\bin>mongod.exe -f ..\mongod.cfg

Then in another command window, connect to the running server to make sure it is up and running. I am shutting the server down while I am connected to it, thus the errors.

D:\MongoDb\bin>mongo
MongoDB shell version: 2.6.1
connecting to: test
> use admin
switched to db admin
> db.shutdownServer()
2014-06-10T14:28:35.567-0400 DBClientCursor::init call() failed
server should be down...
2014-06-10T14:28:35.571-0400 trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2014-06-10T14:28:36.579-0400 warning: Failed to connect to 127.0.0.1:27017, reason: errno:10061 No connection could be made because the target machine actively refused it.
2014-06-10T14:28:36.579-0400 reconnect 127.0.0.1:27017 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
> exit
bye

Install as a service

We now know that our configuration is correct, so now I want to install it as a service so that it is always running,

Start an Administrator command prompt. On Windows 7, press WIN+R, then type cmd, them press CTRL+SHIFT+ENTER. On Windows 8, press WIN+X, then press A.

Now run the command,

sc.exe create MongoDB binPath="C:\MongoDB\bin\mongod.exe --service --config=C:\MongoDB\mongod.cfg" DisplayName="MongoDB 2.6 Standard" start="auto"

You should then see,

[SC] CreateService SUCCESS

You can now start and stop the service with the following commands,

net start MongoDB
net stop MongoDB

If you ever want to uninstall the service, the command is,

sc.exe delete MongoDB

Next Steps

You now have a MongoDB development server running on your local system. It does not require authentication, but is not exposed on the network, only through 127.0.0.1. When you are ready to deploy to production, you will need to set up security and replication.

For development in .NET you will want to start with the Official MongoDB C# driver NuGet package.

  • Pavan Nayakanti

    I know this is bit outdated thread – but thought to share in the benefit of future references.

    Try this –
    mongod.exe –storageEngine=mmapv1

  • SH

    I’ve got an issue with creating the Service. I’m running cmd as an administrator on WIn7.

    I’m using the same version of Mongo as you, but have installed it to its default of “c:\program files\mongodb 2.6 standard\”

    I altered the command to reflect the appropriate paths, but the command runs without error and simply lists the instructions for correctly using the command. No service is created, and no hint as to what went wrong.

    Any insights would be most welcome 🙂

  • javaMan

    when I put the command of mongo.exe.
    I frequently get the following, “Failed to connect to 127.0.0.1.
    How can I solve this matter?
    plz help me out!!!

  • serguey

    Thanks for your help

  • Chris

    Hey Robert,

    We’d love it if you give MongoLab a try for your next hack! Fully-managed MongoDB-as-a-Service. Installed and configured with a click of a button.

    Cheers!