How to setup, secure and scale MongoDB on Windows Server

Downloading an installer, and running a through the setup are the most common steps for a basic installation. Even for database systems. But for learning something new, that might end up in production, it’s important to consider how to setup, secure and scale the platform – in this case MongoDB.

Grow your stack knowledge with Hostek

Last week we published a guide on installing Node.js on Windows and using iisnode to facilitate connections through IIS. Making the same assumption, that as an organization you’re committed to Microsoft Server products this article is for you.

For this example I’ll use our Windows VPS server platform. I suggest the Silver Windows VPS Plan  if you want to try these steps out for yourself. Some requirements might include the need for a separate instance to for mongoDB exclusively. We can talk about that toward the end – under scaling mongoDB.

Installing mongoDB on Windows

We will install mongoDB as a service in Windows so we can control the user it runs as, easily restart it, or set recovery parameters. Download the latest release.

Installing mongoDB on Windows

Be sure the version matches your platform. There’s already excellent “How to install MongoDB on Windows” instructions at the mongoDB website so here’s the link.

  • The initial instructions don’t cover installing it as a service, so skip ahead about 1/2 way down to the title: Configure a Windows Service for MongoDB
  • Also the instructions mention installing at c:\mongodb, during installation you will need to customize the path to match their examples.
  • You might opt to change the data directory for db and logs as well. To keep things tidy I decided to create it in the root folder for mongoDB: c:\mongodb\data

Production Specific Settings:

I strongly encourage anyone to review the production related notes. Quickly scanning through them there are some changes to the pagefile and tcp keep alive time. Although if you’re not planning for a heavy or mission critical load this can probably be left alone. I did set the keep alive time.

See: To view the keep alive setting
One particular setting regarding Windows Virtual machines is modifying the keepalive time, which you can do quickly with this one command:

reg add HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\ /v KeepAliveTime /d 0x0124c0

Service Adjustments

We recommend adjusting the service recovery to restart on failure. That way if something goes wrong Windows can attempt to restart the mongoDB service without your help. You can do this by opening services, properties, changing the failure actions to “restart”.

MongoDB Properties


MongoDB by default runs without requiring authentication. This is quite different from the traditional relational database installation defaults. MongoDB documents are very explicit about this, and describe that mongoDB is by default for use in “TRUSTED” environments. Meaning, with secured ports or only accessible on local networks.

These are also areas that should be explored, for more security:

Scaling mongoDB (optional):

There are also many ways to scale your mongoDB setup. The first is by performance gains through additional RAM, storage etc. The “hardware” specs, that can easily be changed. These are covered in the production related notes.

Second is through creating a Master/Slave replication. Which seems very similar to MySQL Master/Slave setup except mongoDB seems to handle the initial sync process nicely.

There’s also Replica Sets and Sharding. These are beyond the scope of setting up a new mongoDB installation but as needs grow, these are well documented solutions that don’t cost a penny – just take time and in some cases lots of servers.

Testing your installation

Now that you’ve got mongo running, you can test your installation from the command line by running: mongo

Command Prompt - Hostek

You should see output similar to the screenshot above.

Overall mongoDB is impressive, easy to install but should be treated as any other database system, carefully and with thought.

Tags: ,