Install MongoDB Driver for HHVM
Event though SQL database, such as Oracle and MySQL, are still dominant, adapting NoSQL database is a option when you launch a new app. MongoDB is one of most popular NoSQL database and many good tutorials are available on the web nowadays.
In spite of these fact, recently Mongo extension of PHP has been deprecated. So we need to install MongoDB driver instead. One of advantages of using new PHP MongoDB driver is that it supports both PHP and HHVM.
In this article I will show you how to install PHP(HHVM) MongoDB driver in conjunction with MongoDB PHP library that is downloadable via Composer. I test this installation on Ubuntu 14.04, but the process is same for other Linux platforms.
Resources
This article is written based on following resources:
- MongoDB HHVM driver
- PHP: HHVM - Manual
- PHP: Installation with HHVM - Manual
- PHP: Using the library - Manual
Prerequisites
You need these softwares installed on your machine. Inside the square blanket is a version that I have installed for this test on my machine.
- MongoDB (3.2.0)
- HHVM (3.11.0)
- wget
- curl
Install HHVM MongoDB Driver
# Install packages required
sudo apt-get install -y hhvm-dev
sudo apt-get install -y automake autoconf libtool
wget https://s3.amazonaws.com/drivers.mongodb.org/hhvm/hhvm-mongodb-1.0alpha1.tgz
tar -xvzf hhvm-mongodb-1.0alpha1
cd hhvm-mongodb-1.0alpha1
# If you prefer the beta version
# wget https://s3.amazonaws.com/drivers.mongodb.org/hhvm/hhvm-mongodb-1.0beta1.tgz
# tar -xvzf hhvm-mongodb-1.0beta1.tgz
# cd hhvm-mongodb-1.0beta1
# Generate the Makefile
hphpize
cmake .
make configlib
make
sudo make install
# At the end of installation it outputs something like
# => /usr/lib/x86_64-linux-gnu/hhvm/extensions/20150212/mongodb.so
# Modify php.ini to load MongoDB extension
sudo vi /etc/hhvm/php.ini
# Add this two lines below at the end of file replacing the extension_path with what was output at installation
hhvm.dynamic_extension_path=/usr/lib/x86_64-linux-gnu/hhvm/extensions/20150212
hhvm.dynamic_extensions[mongodb]=mongodb.so
# Restart HHVM
sudo service hhvm restart
If you want to test whether the extension is loaded successfully, create a index.php and add the content below.
<?php var_dump(phpversion("mongodb")); ?>
This outputs something similar to string(8) "1.0alpha1".
Install MognoDB PHP library via composer
# Go to your app root directory
cd /path/to/your/app/root
# Install Composer
curl -sS https://getcomposer.org/installer -o installer.php
hhvm installer.php
rm installer.php
# Install library
hhvm composer.phar require mongodb/mongodb:1.0.0-alpha1
Note: To install the library I defined the tag above. When I tried to install without a tag, it installed v0.2.0, which is not compatible with new PHP MongoDB driver. So do not forget to add the tag, and you may want to check what version is available at Packagist.
Coding
<?php
require 'vendor/autoload.php'; // include Composer goodies
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$database = new MongoDB\Database($manager, "DATABASE_NAME");
$documents = $database->selectCollection('COLLECTION')->find();
foreach ($documents as $doc)
{
echo $doc->_id, ': ', $doc->name, "\n";
}
For the more detail of MongoDB PHP library, please refer to its official documentation.