Nearly 3 years ago I discovered a new database that literally changed my life. I know, that’s a pretty bold claim, but it’s true. While leading the engineering team at OpenSky I faced a problem I was well familiar with. How to build a e-commerce product that: 1. Provided performance and scale 2. Handled many verticals and 3. Provided proper indexing on key attributes. In search for a better solution to this problem I encountered MongoDB. I soon experienced a realization that not only was MongoDB the solution to my e-commerce challenge, but fundamentally would change the way all development happens.
This is the first in a series of posts on MongoDB and PHP that will both explain why you should use MongoDB and teach you how. In this first post we will begin with installing the necessary servers and drivers to be able to use MongoDB with PHP.Introduction to MongoDB
MongoDB (from “humongous”) is a scalable, high-performance, open source document database written in C++. While many people hear ‘document database’ and they think of pdf and doc files, the term document really is analogous to an array in PHP. Effectively MongoDB is a persistent storage engine for PHP arrays and objects (as well as any other language) with read through, write thorough memory caching, true high availability, an elegant interface and seamless horizontal scale. Once you write an application with MongoDB you’ll wonder why anyone uses anything else.Installing MongoDB
Installing MongoDB is as easy as using your package installer to install the package. Whether you are using Linux, Mac or Windows, it’s easy to install MongoDB.
The 1st step is to install PECL. PECL is the default installer for all C extensions to the PHP driver. Usually if you have PHP installed you already have PECL. You may be surprised to find that Mac OS X doesn’t include PECL, in the event you don’t have PECL already, here’s how to install it.
Installing PEAR & PECL
- cd /usr/lib/php
- sudo php install-pear-nozlib.phar
- Edit /etc/php.ini and find the line: ;include_path = ".:/php/includes"
change it to: include_path = ".:/usr/lib/php/pear"
make sure to remove the ‘;’ at the beginning of that line.
- sudo pear channel-update pear.php.net
- sudo pecl channel-update pecl.php.net
- sudo pear upgrade-all
Once you have PECL installed the process is easy to install the Mongo DB driver. A simple
pecl install mongo
is all it takes. Once that’s completed you’ll need to add the extension line to your PHP.ini file and restart Apache. For *nix the line is
Once Apache is restarted you should confirm that the MongoDB extension is installed by checking php_info(). A shortcut for this is
$ php --re mongo
Creating your first connection to MongoDB
Creating a connection to MongoDB is pretty easy. We create a new Mongo object and pass in the URI of the server you’re connecting to. The following example will show you how to connect to a Mongo DB server, create a db and a collection and insert a single record into that collection.