MongoDB: installing on windows as a service and your first database

MongoDB is an document-oriented DBMS. Think of it as MySQL but JSON (actually, BSON ) as the data model, not relational. There are no joins. If you have used object-relational mapping layers before in your programs, you will find the Mongo interface similar to use, but faster, more powerful, and less work to set up. In fact it is so easy to get running that I’m going to show you how to get it up and running as a Windows service in a few minutes.

But first, why would you ever want to use MongoDB?

The goal of MongoDB is to bridge the gap between key-value stores (which are fast and highly scalable) and traditional RDBMS systems (which provide rich queries and deep functionality). MongoDB is designed for problems that aren’t easily solved by traditional RDBMSs, including problems that require databases to span many servers.

MongoDB is a document-oriented database. This means that unlike a relational database management system, MongoDB manages collections of JSON-like documents. This allows many applications to model data in a more natural way, as data can be nested in complex hierarchies and still be query-able and indexable.

MongoDB supports an automated sharding architecture, enabling horizontal scaling across multiple nodes. For applications that outgrow the resources of a single database server, MongoDB can convert to a sharded cluster, automatically managing failover and balancing of nodes, with few or no changes to the original application code.

Moving on…

1: Getting the binaries…
Download the windows version here and be sure to get the 64bit version. The 32bit version has a limit of about 2.5Gb whereas the 64bit version has almost none… The reason for this is that the MongoDB storage engine uses memory-mapped files for performance and the goal for the engines code was to keep it as uncluttered as possible.

2: Create some folders
Before opening the downloaded archive you have to create a folder with content at the root of C: called “data”. Inside that create a folder called “db” Note that from now on I’m going to be giving the equivalent of shell commands highlighted and as you continue to read you will see why.

Start -> Run -> CMD
C:
cd C:
mkdir data
mkdir datadb
mkdir datalogs

At this point you should now have this structure on your file system.

3: Extract the downloaded files
You should have downloaded something named like this “mongodb-win32-x86_64-1.4.4.zip”

Extract the archive and move its contents to C: so you should see this type of structure:

4: Set MongoDB as a Windows Service
Looking inside of “bin” we can see a few .exe files. Our focus for now is going to be on “mongod.exe”

Setting it up as a service requires a couple of decisions. At a minimum:

  1. Where shall the logs go? In this example c:mongologs
  2. Where shall the data go? In this example c:mongodata 
  3. What port do want to run the web service on? In this example 1001.

With that decided creating the service is as easy as running the below from the command line.

mongod –bind_ip 127.0.0.1 –logpath c:datalogs –logappend –dbpath c:data –directoryperdb -port 1 –install

! If you wish to remove it just use: mongod –remove

So now Mongo should be visible in Services

Note the path to the executable and the fact that the service is not yet started…

Start the service and go here: http://127.0.0.1:1001/ where you should see the following information.

5: Accessing database
Accessing the database is done via mongo.exe found in bin
Run
C:
cd mongodbbin
mongo

and you should now be in the administrative shell like this.

! If you get an error like below just remove the service and redo without the port definition.

MongoDB shell version: 1.4.4
url: test
connecting to: test
Tue Jul 13 14:44:59 JS Error: Error: couldn’t connect: couldn’t connect to server 127.0.0.1 127.0.0.1:27017 (anon):952
Tue Jul 13 14:44:59 User Exception 12513:connect failed

Saving and retrieving is as easy as below.
db.foo.insert( { a : 1 } )
db.foo.find()

I have also included what the file systems should look like as well for this example.



And that’s it. You now have a working MongoDB service on your Windows machine!
Things to note

  • A performance environment would not have everything on C: 
  • Run binmongd -h for more options, there are not that many…
  • Defining the port rather than running it on the default 27017 requires some extras. Expect errors if you do not read up a bit more on MongoDB.
  • Did you notice that the administrative shell is a JavaScript interface..?

Resources