Status message

Maintenant, vous regardez: Using MongoDB with Drupal 7

Using MongoDB with Drupal 7

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.

1. Installing the MongoDB packages.

  1. $ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
  2.  
  3. #<a href="https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/
  4. #">https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/
  5. #</a> ubuntu 14.04
  6. echo "deb [ arch=amd64 ] <a href="https://repo.mongodb.org/apt/ubuntu">https://repo.mongodb.org/apt/ubuntu</a> trusty/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
  7.  
  8. #ubuntu 16.04
  9. echo "deb [ arch=amd64,arm64 ] <a href="https://repo.mongodb.org/apt/ubuntu">https://repo.mongodb.org/apt/ubuntu</a> xenial/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
  10.  
  11. #ubuntu 18.04
  12. echo "deb [ arch=amd64 ] <a href="https://repo.mongodb.org/apt/ubuntu">https://repo.mongodb.org/apt/ubuntu</a> bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
  13.  
  14. # update package
  15. sudo apt-get update
  16.  
  17. #install
  18. sudo apt-get install -y mongodb-org
  19.  
  20. # start , stop , restart
  21. sudo service mongod start
  22. sudo service mongod stop
  23. sudo service mongod restart
  24.  
  25. #verify
  26. #check /var/log/mongodb/mongod.log file
  27. [initandlisten] waiting for connections on port 27017

2. Installing the MongoDB PHP driver using Pecl

  1. # PEAR is installed to put in place a package and distribution system that is used by both PEAR and PECL
  2. $ sudo apt-get install php-pear
  3.  
  4. # The php7-dev package when installed lets you access the PHP5 source files that are needed to compile additional modules:
  5. $ sudo apt-get install php7-dev
  6.  
  7. #Pecl is the preferred way to install the MongoDB driver.
  8. $ pecl install mongo
  9.  
  10. # Add the mongo extension to /etc/php/apache2/php7.2/cli/php.ini file to Configuring PHP To Use The Driver.
  11. extension=mongo.so

3. Configuring Drupal to use MongoDB plugin

  1. # download mongodb module
  2. $ drush dl mongodb
  3.  
  4. #local.settings.php
  5. $ touch sites/default/local.settings.php
  6.  
  7. # Add code to local.settings.php file
  8. #MongoDB
  9. $conf['mongodb_connections'] = array(
  10. 'default' => array( // Connection name/alias
  11. 'host' => 'localhost', // Omit USER:PASS@ if Mongo isn't configured to use authentication.
  12. 'db' => 'YOURDATABASENAME' // Database name. Make something up, mongodb will automatically create the database.
  13. ),
  14. );
  15.  
  16.  
  17. include_once('./includes/cache.inc');
  18.  
  19. # -- Configure Cache
  20. $conf['cache_backends'][] = 'sites/SITENAME/modules/mongodb/mongodb_cache/mongodb_cache.inc';
  21. $conf['cache_class_cache'] = 'DrupalMongoDBCache';
  22. $conf['cache_class_cache_bootstrap'] = 'DrupalMongoDBCache';
  23. $conf['cache_default_class'] = 'DrupalMongoDBCache';
  24.  
  25. # -- Don't touch SQL if in Cache
  26. $conf['page_cache_without_database'] = TRUE;
  27. $conf['page_cache_invoke_hooks'] = FALSE;
  28.  
  29. # Session Caching
  30. $conf['session_inc'] = 'sites/SITENAME/modules/mongodb/mongodb_session/mongodb_session.inc';
  31. $conf['cache_session'] = 'DrupalMongoDBCache';
  32.  
  33. # Field Storage
  34. $conf['field_storage_default'] = 'mongodb_field_storage';
  35.  
  36. # Message Queue
  37. $conf['queue_default_class'] = 'MongoDBQueue';

4. Enable mongoDB module

  1. #Enable
  2. $ drush en mogodb

MongoDB README.txt file

  1. CONTENTS OF THIS FILE
  2. ---------------------
  3.  
  4. * Introduction
  5. * Installation
  6. * Variables
  7. * Collections
  8. * Module specific configuration
  9.  
  10.  
  11. INTRODUCTION
  12. ------------
  13.  
  14. MongoDB integration for Drupal. This module is a collection of several modules,
  15. allowing to store different Drupal data in MongoDB.
  16.  
  17. mongodb Support library for the other modules.
  18. mongodb_block Store block information in MongoDB. Very close to the
  19. core block API.
  20. mongodb_cache Store cache in MongoDB.
  21. mongodb_field_storage Store fields in MongoDB.
  22. mongodb_queue DrupalQueueInterface implementation using MongoDB.
  23. mongodb_session Store sessions in MongoDB.
  24. mongodb_watchdog Store watchdog messages in MongoDB.
  25.  
  26.  
  27. INSTALLATION & CONFIGURATION
  28. ------------
  29.  
  30. Install as usual, see <a href="http://drupal.org/node/895232">http://drupal.org/node/895232</a> for further information.
  31.  
  32. The MongoDB module and sub-modules need some amount of configuration before they
  33. will properly work. This guide assumes that a MongoDB instance is already
  34. installed and configured on localhost or remote server. This module additionally
  35. provides Drush integration to make queries against the MongoDB databases used by
  36. Drupal.
  37.  
  38. If MongoDB is installed on localhost, you may view the web admin interface:
  39. > <a href="http://localhost:28017/
  40.  
  41.  
  42. VARIABLES
  43. ------------
  44. MongoDB">http://localhost:28017/
  45.  
  46.  
  47. VARIABLES
  48. ------------
  49. MongoDB</a> uses several specific configuration variables that are not currently
  50. exposed in the UI. Non-developers should note that all $conf variables should
  51. be placed at the bottom of the sites settings.php file under a #MongoDB comment.
  52.  
  53. Configuration Variables
  54.  
  55. #1: mongodb_connections
  56.  
  57. The mongodb_connections variable holds the databases available to the module.
  58. The contents are arranged as an associative array holding the name (alias) of
  59. the connection, the database address, and the name of the database. If not
  60. defined, it makes a single default entry. See the note below *.
  61.  
  62. EXAMPLE:
  63. $conf['mongodb_connections'] = array(
  64. // Connection name/alias
  65. 'default' => array(
  66. // Omit USER:PASS@ if Mongo isn't configured to use authentication.
  67. 'host' => 'mongodb://USER:<a href="mailto:PASS@localhost">PASS@localhost</a>',
  68. // Database name
  69. 'db' => 'drupal_default',
  70. ),
  71. );
  72.  
  73. * If mongodb_connections is not defined, 'default' is automatically
  74. created with the following settings:
  75. 'default' => array('host' => 'localhost', 'db' => 'drupal')
  76.  
  77. Also, connection_options might be specified to allow for connecting to
  78. replicate sets (and any other options listed on
  79. <a href="http://www.php.net/manual/mongo.construct.php">http://www.php.net/manual/mongo.construct.php</a>)
  80.  
  81. $conf['mongodb_connections'] = array(
  82. // Connection name/alias
  83. 'default' => array(
  84. 'host' => 'host1,host2,host3',
  85. // Database name
  86. 'db' => 'drupal_default',
  87. 'connection_options' => array('replicaSet' => 'replicasetname'),
  88. ),
  89. );
  90.  
  91. #2: mongodb_debug
  92.  
  93. A variable primarily for developers, mongodb_debug causes a collection
  94. to return a mongoDebugCollection and mongoDebugCursor instead of their
  95. normal equivalents. If not defined, defaults to FALSE.
  96.  
  97. EXAMPLE:
  98. $conf['mongodb_debug'] = FALSE;
  99.  
  100. #3: mongodb_session
  101.  
  102. This variable holds the name of the collection used by the mongodb_session
  103. sub-module. If not defined, it defaults to "session".
  104.  
  105. EXAMPLE:
  106. $conf['mongodb_session'] = 'anyname';
  107.  
  108. #4: mongodb_slave
  109.  
  110. This variable holds an array of the slaves used for the mongodb field storage
  111. sub-module. If not defined, it defaults to an empty array().
  112.  
  113. #5: mongodb_watchdog
  114.  
  115. This variable holds the name of the collection used by the mongodb_watchdog
  116. module. It defaults to "watchdog".
  117.  
  118. EXAMPLE:
  119. $conf['mongodb_watchdog'] = 'drupalogs';
  120.  
  121. #6: mongodb_watchdog_items
  122.  
  123. This variable defines the maximum item limit on the capped collection used by
  124. the mongodb_watchdog sub-module. If not defined, it defaults to 10000.
  125. The actual (size-based) limit is derived from this variable, assuming 1 kiB
  126. per watchdog entry.
  127.  
  128. EXAMPLE:
  129. $conf['mongodb_watchdog_items'] = 15000;
  130.  
  131. #7: watchdog_limit
  132.  
  133. This variable define the maximum severity level to save into watchdog. Errors
  134. under this level will be ignored by watchdog. If not defined, all errors will
  135. saved.
  136.  
  137. EXAMPLE:
  138. $conf['watchdog_limit'] = WATCHDOG_CRITICAL;
  139.  
  140. See <a href="http://api.drupal.org/api/drupal/includes--common.inc/function/watchdog_severity_levels/7
  141. ">http://api.drupal.org/api/drupal/includes--common.inc/function/watchdog_...</a> for further information about Watchdog severity levels.
  142.  
  143. #8: mongodb_collections
  144.  
  145. See COLLECTIONS below.
  146.  
  147.  
  148. COLLECTIONS
  149. ------------
  150.  
  151. Collections are MongoDB's equivalent of relational database tables. In the
  152. context of this module, they can be used to span data from the multiple
  153. submodules into separate databases. This is accomplished by aliasing the
  154. collection name to a connection name (as defined in the 'mongodb_connections'
  155. variable). If you want everything in a single database, collections do not need
  156. to be configured and everything is written to the local default database using
  157. the default collection names (given below). The dot is allowed in the name of
  158. mongodb collections. Before the dot, mongodb_field_storage uses "fields_current"
  159. or "fields_revision" and puts the entity type after.
  160.  
  161. MODULE COLLECTION NAMES
  162. -----------------------------------------------------------
  163. mongodb_block "block"
  164. mongodb_cache "cache_bootstrap"
  165. "cache_menu"
  166. ...
  167. mongodb_field_storage "fields_current.node",
  168. "fields_current.taxonomy_term"
  169. "fields_revision.user"
  170. ...
  171. mongodb_queue "queue."<queue name foo>
  172. "queue."<queue name bar>
  173. ...
  174. mongodb_session "session" (variable)
  175. mongodb_watchdog "watchdog" (variable)
  176. -----------------------------------------------------------
  177.  
  178. In the following example, the watchdog collection will be handled by
  179. the hypothetical connection alias 'logginghost'
  180.  
  181. EXAMPLE:
  182. $conf['mongodb_collections'] = array('watchdog' => 'logginghost');
  183.  
  184.  
  185. MODULE-SPECIFIC CONFIGURATION
  186. ------------
  187.  
  188. The following configuration variables are needed to use the features provided
  189. by the the following sub-modules.
  190.  
  191. => mongodb_cache
  192.  
  193. EXAMPLE:
  194. # -- Configure Cache
  195. $conf['cache_backends'][] =
  196. 'sites/all/modules/mongodb/mongodb_cache/mongodb_cache.inc';
  197. $conf['cache_default_class'] = 'DrupalMongoDBCache';
  198. # -- Don't touch SQL if in Cache
  199. $conf['page_cache_without_database'] = TRUE;
  200. $conf['page_cache_invoke_hooks'] = FALSE;
  201.  
  202. => mongodb_session
  203.  
  204. EXAMPLE:
  205. # Session Caching
  206. $conf['session_inc'] =
  207. 'sites/all/modules/mongodb/mongodb_session/mongodb_session.inc';
  208. $conf['cache_session'] = 'DrupalMongoDBCache';
  209.  
  210. => mongodb_field_storage
  211.  
  212. EXAMPLE:
  213. # Field Storage
  214. $conf['field_storage_default'] = 'mongodb_field_storage';
  215.  
  216. TROUBLESHOOTING
  217. ------------
  218.  
  219. If installing mongodb_field_storage from an Install Profile:
  220.  
  221. * Do not enable the module in the profiles .info file.
  222. * Do not include the module specific $conf variable in settings.php during
  223. install.
  224. * In the profiles hook_install() function include
  225.  
  226. module_enable(array('mongodb_field_storage'));
  227. drupal_flush_all_caches();