David Golding



Installing MongoDB on MAMP 1.9.5

By David Golding | Print This Post Print This Post

Appsolute launched MAMP version 1.9.5 today, so I thought it’d be a great time to add MongoDB to it and improve my NoSQL skills.

1. Prepare MAMP for MongoDB files

Create a new folder at Applications/MAMP/db/mongo with three additional subfolders named bin, data, and tmp. Provide these folders with chmod 0755 access permissions. These folders will be the main runtime location for Mongo once MAMP gets it running.

2. Download MongoDB

Grab the latest Mac OS install package of MongoDB. My server setup called for OS X 64-bit, version 1.6.5. It’ll have a directory named bin. Drop the files from this folder into the /Applications/MAMP/db/mongo/bin folder you already created.

3. Download Mongo Driver for PHP

I’m running PHP 5.3 (why use MongoDB with any earlier version of PHP?), so I’ll need the mongo.so extension to get PHP and Mongo working together. This is available at the MongoDB GitHub repository, under the PHP 5.3 for Mac binary. After unpacking the downloaded file, place the mongo.so extension file in the /Applications/MAMP/bin/php5.3/lib/php/extensions folder.

Update: An alternative is to place the mongo.so file in the /Applications/MAMP/bin/php5.3/lib/php/extensions/no-debug-non-zts-20090626 folder and avoid having to edit the php.ini file. It appears that MAMP 1.9.5 already has the extension=mongo.so line in the extensions block of the php.ini file, even though version 1.9.5 doesn’t come bundled with Mongo.

4. Create Startup Routines for MAMP

Lastly, you’ll need to create the startup routines so that MAMP will launch Mongo along with MySQL and Apache. Create a new file at /Applications/MAMP/bin/startMongo.sh and place in it the following code:

1
2
# /bin/sh
/Applications/MAMP/db/mongo/bin/mongod --dbpath /Applications/MAMP/db/mongo/data --logpath /Applications/MAMP/db/mongo/mongodb.log --pidfilepath /Applications/MAMP/db/mongo/tmp/mongo.pid --fork --logappend

When called, this script will launch Mongo using the MAMP-relative paths rather than Mongo’s system defaults.

Create another file at /Applications/MAMP/bin/stopMongo.sh and place the shutdown method:

1
2
# /bin/sh
/bin/kill `cat /Applications/MAMP/db/mongo/tmp/mongo.pid`

This works like the previous script, except it kills the mongo.pid process, effectively shutting down Mongo.

To have MAMP automatically call these Mongo startup scripts, open the /Applications/MAMP/bin/start.sh and /Applications/MAMP/bin/stop.sh files, and insert the following lines above the startMysql.sh lines, respectively:

1
/Applications/MAMP/bin/startMongo.sh
1
/Applications/MAMP/bin/stopMongo.sh

Now MAMP will automatically launch Mongo upon startup.

The only thing left to do is tell PHP to run the mongo.so extension. If you’re running MAMP Pro, edit the php.ini file by selecting File > Edit Template > PHP 5.3 php.ini, otherwise you’ll need to lookup the path the php.ini file from the MAMP startup screen, under “phpInfo” and “Loaded Configuration File.”

Insert the following line in the php.ini file/template, save the file, then restart MAMP.

1
extension="/Applications/MAMP/bin/php5.3/lib/php/extensions/mongo.so"

Mongo should now run in the background on MAMP, which you can connect with from PHP using the main connection routines listed on the PHP site. Welcome to NoSQL on MAMP!


Comments

15 Responses to “Installing MongoDB on MAMP 1.9.5”

Robert Ross

Mar 15th, 2011, 5:08 pm

This is an awesome tutorial, my co-worker told me a much more hair pulling way of doing this, but luckily I thought to myself “There has to be a better way”, and here it is. Thanks for sharing it!

Oscar Godson

Mar 18th, 2011, 10:22 pm

Hey man, i’ve been trying this for hours and I can’t seem to figure this out. I did exactly what you have here and I have MAMP Pro.

However, after all of this I get this error in my logs:
[18-Mar-2011 22:18:48] PHP Warning: PHP Startup: Unable to load dynamic library ‘/Applications/MAMP/bin/php5.3/lib/php/extensions/mongo.so’ – dlopen(/Applications/MAMP/bin/php5.3/lib/php/extensions/mongo.so, 9): image not found in Unknown on line 0

So i did your update and tried dropping in the .so file and not adding anything to the .ini file but i get this in my browser:
Fatal error: Class ‘Mongo’ not found in /Applications/MAMP/htdocs/gator/index.php on line 4

LASTLY, i tried just putting extension=mongo.so like the others but i get:

Fatal error: Uncaught exception ‘MongoConnectionException’ with message ‘connecting to failed: Invalid argument’ in /Applications/MAMP/htdocs/gator/index.php:4 Stack trace: #0 /Applications/MAMP/htdocs/gator/index.php(4): Mongo->__construct() #1 {main} thrown in /Applications/MAMP/htdocs/gator/index.php on line 4

Please, any help at all? :\

Oscar Godson

Mar 19th, 2011, 12:54 am

Awh, never mind… i feel stupid… I had to first install Mongo it’s self on Mac and then boot it up via Terminal (and leave it open) then i was able to use MongoDB via MAMP

websurfshop

Mar 27th, 2011, 9:05 pm

Hey David, What’s up with Lithium? Just checked the rad-dev site and it seems like the trail has gone cold. What’s the future of PHP MVC, cakePHP or are we all going the Zend way (follow the money)? I bought your cakePHP book and enjoyed the study. What are you musings on the future of these technologies?

David Golding

Mar 28th, 2011, 9:11 pm

The Li3 devs seem quite active with regular commits and new developments almost every week. You can keep tabs on their progress best by checking the Twitter account and following the Twitter feeds of the lead devs. There’s a GitHub repo if you prefer that site, http://github.com/UnionOfRAD/framework that mirrors their main repo. The most recent release was 0.9.9, but some important changes have been added since then. I’d stick with downloading the latest release from GitHub and working with that. Documentation remains somewhat sparse, so consult the unit tests source for solid examples of how to use the framework. I’ve benefited from their documentation drafts: http://dev.lithify.me/drafts/source/en

TheDailySpank

Jun 26th, 2011, 8:34 am

Oscar: You need to change permissions on the startMongo.sh and stopMongo.sh files.

chmod 775 startMongo.sh
chmod 755 stopMongo.sh

Joshua

Jun 30th, 2011, 3:36 am

Im having the same issues as Oscar… I’ve set the permissions multiple times, but same Fatal error he showed. Any ideas?

Cody

Jul 3rd, 2011, 3:48 pm

Thanks so much for this! It helped me get setup perfectly! ^^

boris badenov

Oct 27th, 2011, 8:39 pm

I had a devil of a time to get this to work too. there were two things I needed to do which, might be obvious to the experienced coder but to a n00b like me, did take a little more effort.

In step one, setting the permissions, nothing would work only install unless I set permissions like this:

sudo chmod -R 775 bin
sudo chmod -R 775 data
sudo chmod -R 775 tmp

On step 3, inserting the mongo driver, I noticed that the path the author used was slightly different than mine. Instead of using :

/Applications/MAMP/bin/php5.3/lib/php/extensions

I noticed my php5.3 was actually php5.3.6 and it was inside a folder called php so my complete path was:

/Applications/MAMP/bin/php/php5.3.6/lib/php/extensions/

and I put the mongo.so into this folder:

/Applications/MAMP/bin/php/php5.3.6/lib/php/extensions/no-debug-non-zts-20090626/

And then in my php.ini file, I had to correct the path. Somewhere around line 424, you will need to put your path (get that from phpinfo) and put that in this spot

After that, mongo ran perfectly

[...] There is an explanation about that on mongodb.org and also one I found very helpful was done by David Golding. (As a note on that: For step 3 in that particular tutorial, I went with what Golding proposed in [...]

[...] 具体可参考下面的文章: MongoDB with MAMP on OS X Installing MongoDB on MAMP 1.9.5  [...]

Håkan Folkesson

Jan 17th, 2012, 3:45 pm

I had the same trouble as Oscar above to get it working. kept getting this error:

Fatal error: Uncaught exception ‘MongoConnectionException’ with message ‘connecting to failed: Invalid argument’ in…etc

After googling some more and trying to find my way in Terminal with the help of the readme file that came with mongo I ended up writing this in the Terminal:

/Applications/MAMP/db/mongo/bin/mongod –dbpath /Applications/MAMP/db/mongo/data/db

Now it works for me with MAMP. So I guess my problem was that the database path was not defined correctly.

Håkan Folkesson

Jan 17th, 2012, 4:16 pm

In my comment above the Terminal command should be with 2 dashes in front of dbpath:

/Applications/MAMP/db/mongo/bin/mongod –dbpath /Applications/MAMP/db/mongo/data/db

Håkan Folkesson

Jan 18th, 2012, 2:17 am

Has anyone actually got this running just by starting MAMP or are you starting Mongo with the Terminal? I can start Mongo with the instructions from my startMongo.sh in the Terminal and then use it together with MAMP but my startMongo.sh file by itself doesn’t seem to run at all? I have checked all permissions and they are 775.

James Womack

Feb 4th, 2012, 9:53 pm

Excellent, by far the best walk-through out there on the topic. In my case I had to change /Applications/MAMP/bin/php5.3/ to:
/Applications/MAMP/bin/php/php5.3.6



Submit Comment


Beginning CakePHP: From Novice to Professional by David Golding

Other Blogs

David Golding

A blog about CakePHP, web design, and grad studies in religion. © 2008, D. Golding