[Drupal] How to configure Drupal to use MongoDB?

| | 2 min read

MongoDB is a database that was developed to scale efficiently when compared databases like MySQL and many Drupal. users developers who wanted to utilize the advantages of MongoDB in their Drupal sites wanted to know how to configure Drupal to use MongoDB. If you are looking to use MongoDB with your Drupal site and looking to know how to configure Drupal to use MongoDB in your Drupal site then read on to find out more.

MongoDB is an open source database package that stores content in the form of JSON-like documents instead of the traditional method of storing them in a relational database. MongoDB belongs to the NoSQL family of database systems and is designed for scalability. It has significantly faster reads than a conventional database like MySQL and stores data in the form of documents. Follow the steps below to configure it to work with Drupal

  • First we need to install MongoDB and configure it to work with PHP in your server. You could do this yourself or you can request your hosting provider to do so if you on a shared hosting server.
    • Type the following commands to install MongoDB in your Linux based server
      apt-get install mongodb-10gen or yum install mongodb
    • Next we need to install the MongoDB PHP driver
    • Type the following command to install the driver
      pecl install mongo
    • Next we need to configure PHP to use MongoDB
    • Edit the php.ini file of your server and add the following line there
  • Now we to need to configure Drupal to use MongoDB
    • First we need to download and install the Drupal MongoDB module
    • As there is currently no administrative interface for MongoDB we need to create a settings file for it
    • Create a file call local.settings.php in the same directory as settings.php using the command below
      touch local.settings.php
    • Copy the following code there
      $conf['mongodb_connections'] = array(
      'default' => array( // Connection name/alias
      'host' => 'localhost', // Omit USER:PASS@ if Mongo isn't configured to use authentication.
      'db' => '[YOURDATABASENAME]' // Database name. Make something up, mongodb will automatically create the database.
      # -- Configure Cache
      $conf['cache_backends'][] = 'sites/[SITENAME]/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/[SITENAME]/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';
    • Remember to replace the placeholders as and when required

    Hope that helps

    If you want to configure your Drupal site to run on MongoDB then Get in touch with us.

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