Setting Up MongoDB on a Mac
Join the DZone community and get the full member experience.
Join For FreeMongoDB 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.8.2 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.5, 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 choose file->edit template- choosing the php template you want to use.
Once in there (you can click OK on the warning message), scroll to the extensions section and add the reference to mongo.so:
{% img break https://s3-eu-west-1.amazonaws.com/andyhawthorne.co.uk/mongo2.png ‘Mongo’ ‘Mongo extension’ %}
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:
{% img break https://s3-eu-west-1.amazonaws.com/andyhawthorne.co.uk/mongo1.png ‘Mongo’ ‘Mongo extension’ %}
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, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Testing Applications With JPA Buddy and Testcontainers
-
MLOps: Definition, Importance, and Implementation
-
What Is JHipster?
-
Hibernate Get vs. Load
Comments