mongodb_drupal_Manage_Dynamic_Web_Content.jpg
Blog

Integrating Drupal 7 with MongoDB to Manage Dynamic Web Content

MongoDB can be easily integrated with Drupal 7 to support millions of dynamic web pages on a daily basis to speed up the websites. This document oriented application is a schema less database that can store large files without complicating your stack.

Here’s a look at how to install MongoDB on Linux and integrated it with Drupal 7.

Step I : Install MongoDB on Linux

# Importing the MongoDB public GPG Key

The Ubuntu package management tools (i.e. dpkg and apt) ensure package consistency and authenticity as the distributors need to sign up with GPG keys. The following command is to be issued to import the MongoDB public GPG Key:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

# Creating a list file for MongoDB.

Create a list file called mongodb-org-3.0.list using the following command:

echo "deb https://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
  
sudo apt-get update

# Installing the MongoDB packages.

Finally install the latest stable version of MongoDB.

sudo apt-get install -y mongodb-org

Reference: https://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

 

Step II : Installing the MongoDB PHP driver using Pecl

Install Pecl to install MongoDB PHP if it is not installed already.

# Install PEAR

PEAR is installed to put in place a package and distribution system that is used by both PEAR and PECL

sudo apt-get install php-pear

# The php5-dev package when installed lets you access the PHP5 source files that are needed to compile additional modules:

sudo apt-get install php5-dev

In case you fail to install the php-dev package before trying to install a PECL extension using “pear install”,the following error will show up on your screen :

sh: phpize: not found ERROR: `phpize’ failed

# The PECL_HTTP extension requires an additional dependency package to be installed. sudo apt-get install libcurl3-openssl-dev # Install MongoDB PHP driver using Pecl:

Pecl is the preferred way to install the MongoDB driver.

pecl install mongo

# Add the mongo extension to php.ini file to Configuring PHP To Use The Driver.

extension=mongo.so

Reference: https://www.mkfoster.com/2009/01/04/how-to-install-a-php-pecl-extensionmodule-on-ubuntu/

https://spf13.com/post/getting-started-with-drupal-and-mongodb

Step III : Configuring Drupal to use MongoDB plugin

Download the MongoDB module ( https://drupal.org/project/mongodb) like any other module and place it in sites/all/modules directory.

Although there isn’t an admin interface to configure MongoDB, it is a fairly easier task to configure it manually.First create a file called local.settings.php and it should be in the same directory as settings.php.

Populate the file with the following contents. Make sure you replace the placeholders [YOURDATABASENAME] and [SITENAME

 array( // Connection name/alias
    'host' => 'localhost', // Omit USER:PASS@ if Mongo isn't configured to use authentication.
    'db' => 'mongo' // Database name. Make something up, mongodb will automatically create the database.
  ),
);
include_once('./includes/cache.inc');
# -- Configure Cache
$conf['cache_backends'][] = 'sites/all/modules/mongodb/mongodb_cache/mongodb_cache.inc';
$conf['cache_class_cache'] = 'DrupalMongoDBCache';
$conf['cache_class_cache_bootstrap'] = 'DrupalMongoDBCache';
$conf['cache_default_class'] = 'DrupalMongoDBCache';
# -- Don't touch SQL if in Cache
$conf['page_cache_without_database'] = TRUE;
$conf['page_cache_invoke_hooks'] = FALSE;
# Session Caching
$conf['session_inc'] = 'sites/all/modules/mongodb/mongodb_session/mongodb_session.inc';
$conf['cache_session'] = 'DrupalMongoDBCache';
# Field Storage
$conf['field_storage_default'] = 'mongodb_field_storage';
# Message Queue
$conf['queue_default_class'] = 'MongoDBQueue';

Step IV : Enable the MongoDB module

NB:If you want to use MongoDB Block module then you need to disable the core block module as it conflicts with MongoDB Block which in turn does not allow Drupal to enable the MongoDB plugin.

In our case we did not use the MongoDB Block so we kept using the core block module.

MongoDB Module Installation

Fix common issues in the MongoDB integration with Drupal

I encountered a few issues while using this module. The first issue I faced was while adding a content type field using field storage as it threw back a warning that said:

Notice: Undefined property: EntityFieldQuery::$propertyConditions in mongodb_field_storage_query()

I fixed this issue by applying this patch.

The second issue was with saving a new content or node. The warning issued was about the save method that is being used to save records in MongoDB as it is deprecated and we need to use ‘w’ instead.

I edited the mongodb_field_storage.module and made changes to line number 198

$collection->save($new_entity, array('safe' => TRUE));

to

$collection->save($new_entity, array('safe' => TRUE, 'w' => 1));

We at Valuebound are committed to furthering your business initiatives with our Enterprise level web solutions. For further information on our service offerings, pleaseContact Us