NoSQL Zone is brought to you in partnership with:

Andy is a freelance journalist based in Coventry, United Kingdom. He also codes web applications. Andy is a DZone MVB and is not an employee of DZone and has posted 26 posts at DZone. You can read more from them at their website. View Full User Profile

Get MongoDB Up and Running on Your Mac

02.29.2012
| 6305 views |
  • submit to reddit

MongoDB is a document oriented, NoSQL database. It is gathering momentum and popularity amongst developers because it is flexible, and scalable at the same time. In this article I will describe how I got it set up and working on my Mac running OS X 10.7.3 and MAMP with PHP 5.2.17, and PHP 5.3.6.

What is MongoDB?

MongoDB is an open source application written in the C++ programming language. It is a NoSQL data storage type database, meaning that it stores data in collections. The collections consist of a set of documents. Documents contain a set of key-value pairs, and don’t have to be defined before you start using the database.

Installing MongoDB

You don’t actually install MongoDB, not really. You can download the precompiled binaries, place the resulting directory somewhere (I chose my home folder), and it is nearly ready to begin. I chose version 2.0.2 and the 64-bit OS X option, and that is what this article is based on.

You need to create a data directory. There is advice on the MongoDB web site how to do that, so that is pretty much what I did:

$ sudo mkdir -p /data/db/
$ sudo chown `id -u` /data/db

Does it work?

Now we can check to see if things are working. Open up the terminal app, change directory into your home folder if you are not there already:

$ cd ~/

I renamed the Mongo folder to just mongodb to make it easier to type. Then you can start the server:

$ ./mongodb/bin/mongod

Then, in a new terminal tab connect to localhost:

$ ./mongodb/bin/mongo

That should return something like this:

MongoDB shell version: 2.0.2
connecting to: test
>

 We are in “it just works” territory here. I have gone through these steps on several Macs now, and it does indeed just work. We’ll tell Mongo that we want to use a particular collection (these commands should be typed in the localhost shell window):

> use testingdb

Mongo will reply that it has switched to the db called testingdb. Next, we will add some data:

> db.testingdb.insert({title: "a test note", note: "This is my first note", 
  tags: "Testing"})

Here, you can see that I have defined 3 fields: title, note, and tags.When you hit enter, the record will be saved. Add some more, in the same way and then we can query the collection to see our records:

> db.testingdb.find()

 

This command will return all the results found in the collection. It also demonstrates that things are working nicely. Now for some drivers.

Installing the PHP Drivers in MAMP

First, you need to visit Github and download the driver version for PHP that you use most commonly with MAMP. There is a binary build for both PHP 5.2 and PHP 5.3 available.

You will then need to move the downloaded binary (mongo.so) into the correct folder for MAMP to find it. The folder can be found at: /applications/MAMP/bin/php/php5.2.17/lib/php/extensions/no-debug-non-zts-20060613

I’m running MAMP 2.0.3, but it should be something similar for other version too. Next, we will need to edit the PHP.ini template in MAMP. From the menu bar:

editing the MAMP PHP.ini template

Once in there (you can click OK on the warning message), scroll to the extensions section and add the reference to mongo.so:

editing the MAMP PHP.ini template

Save, and the re-start MAMP. If you then go to web start and click phpInfo, you should be able to scroll down and see:

Mongo up and running in MAMP

That means you are now ready to start coding with MongoDB and MAMP.

Finally…

This is a quick and easy way to get up and running with MongoDB on OS X Lion running MAMP. There are other installation methods, such as installing via Homebrew, but so far, I have found this to work perfectly.

Because this is experimental to some extent though, please share your experiences below.


Published at DZone with permission of Andy Hawthorne, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)