Installing Mongo and PHP Driver


This guide is a brief introduction to installing MongoDB, and the PHP Driver on Ubuntu 14.04 (other debian based systems should be very similar).

We will also write a short PHP script to test this.

Installing MongoDB

Using the Install MongoDB on Ubuntu guide, the steps are fairly straight forward as follows:

$ sudo apt-key adv --keyserver hkp:// --recv 7F0CEB10
$ echo "deb "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
$ sudo apt-get update
$ sudo apt-get install mongodb-org
$ sudo service mongod start

That should give you a basic installation with defaults. The config file for mongod is located at /etc/mongod.conf

Checking mongo, and a few shell commands

Mongo by default has no security restrictions, we will go over these in another article at some point, so let’s make a small test database, with a new collection and insert some data to make sure that mongod is up and running

$ mongo
MongoDB shell version: 3.0.3
connecting to: test
> use funideas
switched to db funideas
> db.usefullinks.insert({'title': 'Install Mongo and PHP Driver', 'url': '…and-php-driver/'})
WriteResult({ "nInserted" : 1 })
> show collections
> db.usefullinks.find().pretty()
"_id" : ObjectId("556c49a2088aa312641e7ac6"),
"title" : "Install Mongo and PHP Driver",
"url" : "…and-php-driver/"
> exit

Note the “_id” will be unique yo your system, so may vary from what we’ve inserted there.

We will keep this database and collection there (think of a collection as a table in MySQL/other relational database).

Installing PHP Driver

Using the guide from the Installing PHP MongoDB Driver page, we do the following:
$ sudo pecl install mongo
I choice no the sasl support, as this is not needed. You will need to add to any php configurations that require it, I want it to be available for all modules (apache2, fpm, and cli), so I create the a file at /etc/php5/mods-available/mongo.ini

$ echo > sudo tee /etc/php5/mods-available/mongo.ini
$ sudo php5enmod mongo
$ sudo service apache2 restart
$ sudo service php5-fpm restart

And that should have enabled the latest mongo driver for PHP. To check using the command line interpreter use the following:
$ php -r "phpinfo();" | grep mongo
mongo.allow_empty_keys => 0 => 0
mongo.chunk_size => 261120 => 261120
mongo.cmd => $ => $
mongo.default_host => localhost => localhost
mongo.default_port => 27017 => 27017
mongo.is_master_interval => 15 => 15
mongo.long_as_object => 0 => 0
mongo.native_long => 1 => 1
mongo.ping_interval => 5 => 5

Simple PHP script to test the mongo database

I create the following PHP script called test_mongo.php to check that the driver worked, and used the data we created earlier:

# Create connection to localhost, on port 27017
$objMongo = new MongoClient('localhost:27017'); // The 'localhost:27017' can be omitted as these are defaults

# Get a link to the funideas database
$objDB = $objMongo->funideas;

# Select a collection
$objCollection = $objDB->usefullinks;

# Search the usefullinks collection
$objCursor = $objCollection->find();

# Loop through the collection and output each item found
foreach($objCursor AS $objDoc) {

Executed the file, and checked the output:
$ php test_mongo.php
[_id] => MongoId Object
[$id] => 556c4b6950683786bfcd8b6d

[title] => Install Mongo and PHP Driver
[url] =>…and-php-driver/

Clearing up

If you have finished, you can simply remove the funideas database with the following command in mongo:

$ mongo
> db.dropDatabase()
{ "dropped" : "funideas", "ok" : 1 }
> exit



Leave a Reply

Your email address will not be published. Required fields are marked *