RSS

Category Archives: MongoDB

Installing MongoDB on Mac

By: STEPHEN KOCH

After we had a Processing workshop with Jer Thorpe he got to talking about Mongo, so we decided to have a gander at this “scalable, high-performance, open source, document-oriented database.” Read on for instructions on how to get it operational on your Mac OS X development machine, so you can start playing with Mongo using PHP.

Setting up MongoDB was easy enough on OS X (Snow Leopard), but getting it to play nice with PHP was a little odd. I think this is because I am using MAMP as my dev environment.

The first thing you will want to do before installing is to head on over to mongodb.org and do the 5 minute online shell tutorial. This will whet your appetite for Mongo and will quickly show you how easy it is to work with.

Install MongoDB

  • Download mongo: http://www.mongodb.org/downloads
  • Unzip into a location of choice. (Note: This is where you will run the daemon)
  • Open terminal and use the following command to make the directory where mongo will store data:
    $ mkdir -p /data/db
  • Start the mongodb server in a separate terminal window (if you get an error, you may need to ‘sudo’ this command):
    $ ./mongodb-xxxxxxx/bin/mongod
  • In a separate terminal window start the interactive shell:
    $ ./mongodb-xxxxxxx/bin/mongo
    > db.foo.save( { a : 1 } )
    > db.foo.find()

And with that you should have a running daemon and be able to use the shell just like you did in the online tutorial. If you receive an error about not being able ‘to create / open lock file’, you should run the command to make the db directory with ‘sudo’.

Getting PHP to behave

If you aren’t using MAMP you may be able to run the following command to install, and you should definitely give this a shot first. This worked successfully on my laptop, but I had issues on the machine running MAMP.

 

sudo pecl install mongo

These are the steps I took to get it working with MAMP:

  • Go to pecl.php.net to dowload the latest package (1.1.3)
  • Unzip and open README.md file. Instructions are simple:
  • Install package:
    $ phpize
    $ ./configure
    $ make
    $ sudo make install
  • This will create the ‘mongo.so’ file in the /modules dir. Copy this over to your MAMP extensions directory. You can find the location in your php.ini file. On my machine the path is ‘/Applications/MAMP/bin/php5.3/lib/php/extensions/no-debug-non-zts-20090626/.’ This will also copy the ‘mongo.so’ file to ‘/usr/lib/php/extensions/no-debug-non-zts-20090626/mongo.so.’
  • Add the following to your php.ini file found in your MAMP installation:
    extension=mongo.so
  • Start MAMP
  • Copy the example code found on php.net, place somewhere accesible from your browser and hit to test
  • You should see output in the browser as well as in the running server

Special Notes

If you want to run php from Terminal you will need to look in /etc for the php.ini file. If it doesn’t exist, you can open php.ini.default found there and create a new document. You will need to set the ‘extensions_dir’ variable to either as well as adding the ‘extension=mongo.db’ line to the file. You can issue the ‘php -i’ command from Terminal to see that you have the mongo extension properly loaded.

As I stated, I was able to run the pecl command from my laptop which basically issued the above commands and copied things to their proper locations except for the MAMP extensions dir. A shorter method to install so I recommend giving that a try first but don’t forget to copy the ‘mongo.so’ file over to the MAMP extensions directory.

Next steps and useful tips

  • Be sure to peruse the output when you issue the commands when installing the Mongo PHP package. It’s a lot of text, but if you are lost as to where things are, this output will tell you.
  • Experiment with PHP commands and create some test databases and collections.
  • For administering MongoDB I recommend RockMongo.
  • Try out some of the other drivers to play with Mongo.

Happy coding!

 

Copy from: http://www.bigspaceship.com/mongodb-on-mac/

Advertisements
 
Leave a comment

Posted by on September 3, 2015 in DBMS, MongoDB