From 2e7757fed70f041b1e045b31b741fbe98e5bb472 Mon Sep 17 00:00:00 2001 From: Steven Date: Thu, 2 Jul 2015 21:24:52 -0500 Subject: [PATCH 01/10] Add cookbook article for using MongoDB to store session data --- .../configuration/mongodb_session_storage.rst | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 cookbook/configuration/mongodb_session_storage.rst diff --git a/cookbook/configuration/mongodb_session_storage.rst b/cookbook/configuration/mongodb_session_storage.rst new file mode 100644 index 00000000000..248c49a576e --- /dev/null +++ b/cookbook/configuration/mongodb_session_storage.rst @@ -0,0 +1,54 @@ +How to Use MongoDbSessionHandler to Store Sessions in a MongoDB Database +======================================================================== + +The default Symfony session storage writes the session information to files. +Some medium to large websites use a NoSQL database called MongoDB to store the +session values instead of files, because databases are easier to use and scale +in a multi-webserver environment. + +Symfony has a built-in solution for NoSQL database session storage called +:class:`Symfony\\Component\\HttpFoundation\\Session\\Storage\\Handler\\MongoDbSessionHandler`. +MongoDB is an open-source document database that provides high performance, +high availability, and automatic scaling. This article assumes that you have +already `installed and configured a MongoDB server`_. To use it, you just +need to change/add some parameters in the main configuration file: + +.. configuration-block:: + + .. code-block:: yaml + + # app/config/config.yml + framework: + session: + # ... + handler_id: session.handler.mongo + cookie_lifetime: 2592000 # optional, it is set to 30 days here + gc_maxlifetime: 2592000 # optional, it is set to 30 days here + + parameters: + # ... + mongo.session.options: + database: session_db # your MongoDB database name + collection: session # your MongoDB collection name + mongodb_host: 1.2.3.4 # your MongoDB server's IP + mongodb_username: my_username + mongodb_password: my_password + + services: + # ... + mongo_client: + class: MongoClient + # if using a username and password + arguments: [mongodb://%mongodb_username%:%mongodb_password%@%mongodb_host%:27017] + # if not using a username and password + arguments: [mongodb://%mongodb_host%:27017] + session.handler.mongo: + class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler + arguments: [@mongo_client, %mongo.session.options%] + +Setting Up the MongoDB Collection +--------------------------------- +Because MongoDB uses dynamic collection schemas, you do not need to do anything to initialize your +session collection. + +.. _installed and configured a MongoDB server: http://docs.mongodb.org/manual/installation/ \ No newline at end of file From 8e20ba441f75bc94ab380609b4392b6616714901 Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 3 Jul 2015 12:52:12 -0500 Subject: [PATCH 02/10] add new cookbook article to index files --- cookbook/configuration/index.rst | 1 + cookbook/map.rst.inc | 1 + 2 files changed, 2 insertions(+) diff --git a/cookbook/configuration/index.rst b/cookbook/configuration/index.rst index 35b87ef0f2e..8bac5cf43be 100644 --- a/cookbook/configuration/index.rst +++ b/cookbook/configuration/index.rst @@ -13,3 +13,4 @@ Configuration apache_router web_server_configuration configuration_organization + mongodb_session_storage \ No newline at end of file diff --git a/cookbook/map.rst.inc b/cookbook/map.rst.inc index 8caed945d0c..36f05e61afc 100644 --- a/cookbook/map.rst.inc +++ b/cookbook/map.rst.inc @@ -38,6 +38,7 @@ * :doc:`/cookbook/configuration/apache_router` * :doc:`/cookbook/configuration/web_server_configuration` * :doc:`/cookbook/configuration/configuration_organization` + * :doc:`/cookbook/configuration/mongodb_session_storage` * :doc:`/cookbook/console/index` From 52d89dc1112e6fede6c80e24a2bcd8801d7423c7 Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 3 Jul 2015 12:57:59 -0500 Subject: [PATCH 03/10] add xml and php configuration blocks --- .../configuration/mongodb_session_storage.rst | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/cookbook/configuration/mongodb_session_storage.rst b/cookbook/configuration/mongodb_session_storage.rst index 248c49a576e..79140e03a34 100644 --- a/cookbook/configuration/mongodb_session_storage.rst +++ b/cookbook/configuration/mongodb_session_storage.rst @@ -46,6 +46,90 @@ need to change/add some parameters in the main configuration file: class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler arguments: [@mongo_client, %mongo.session.options%] + .. code-block:: xml + + + + + + + + + + + + + + + session_db + + session + + + 1.2.3.4 + my_username + my_password + + + + + + mongodb://%mongodb_username%:%mongodb_password%@%mongodb_host%:27017 + + + mongodb://%mongodb_host%:27017 + + + + mongo_client + %mongo.session.options% + + + + .. code-block:: php + + use Symfony\Component\DependencyInjection\Reference; + use Symfony\Component\DependencyInjection\Definition; + + $container->loadFromExtension('framework', array( + 'session' => array( + // ... + 'handler_id' => 'session.handler.mongo', + 'cookie_lifetime' => 2592000, // optional, it is set to 30 days here + 'gc_maxlifetime' => 2592000, // optional, it is set to 30 days here + ), + )); + + $container->setParameter('mongo.session.options', array( + 'database' => 'session_db', // your MongoDB database name + 'collection' => 'session', // your MongoDB collection name + )); + $container->setParameter('mongodb_host', '1.2.3.4'); // your MongoDB server's IP + $container->setParameter('mongodb_username', 'my_username'); + $container->setParameter('mongodb_password', 'my_password'); + + $container->setDefinition('mongo_client', new Definition('MongoClient', array( + // if using a username and password + array('mongodb://%mongodb_username%:%mongodb_password%@%mongodb_host%:27017'), + // if not using a username and password + array('mongodb://%mongodb_host%:27017'), + ))); + + $container->setDefinition('session.handler.mongo', new Definition( + 'Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler', + array(new Reference('mongo_client'), '%mongo.session.options%') + )); + Setting Up the MongoDB Collection --------------------------------- Because MongoDB uses dynamic collection schemas, you do not need to do anything to initialize your From 4048e972fb8fc7d3d59b6c90abc71de271d81d7e Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 3 Jul 2015 12:59:28 -0500 Subject: [PATCH 04/10] fix formatting --- cookbook/configuration/mongodb_session_storage.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cookbook/configuration/mongodb_session_storage.rst b/cookbook/configuration/mongodb_session_storage.rst index 79140e03a34..32c7c69176d 100644 --- a/cookbook/configuration/mongodb_session_storage.rst +++ b/cookbook/configuration/mongodb_session_storage.rst @@ -9,7 +9,7 @@ in a multi-webserver environment. Symfony has a built-in solution for NoSQL database session storage called :class:`Symfony\\Component\\HttpFoundation\\Session\\Storage\\Handler\\MongoDbSessionHandler`. MongoDB is an open-source document database that provides high performance, -high availability, and automatic scaling. This article assumes that you have +high availability and automatic scaling. This article assumes that you have already `installed and configured a MongoDB server`_. To use it, you just need to change/add some parameters in the main configuration file: @@ -132,6 +132,7 @@ need to change/add some parameters in the main configuration file: Setting Up the MongoDB Collection --------------------------------- + Because MongoDB uses dynamic collection schemas, you do not need to do anything to initialize your session collection. From d3ba62f27ed7c4c5de75fac2b0dc831db6521540 Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 4 Jul 2015 10:33:25 -0500 Subject: [PATCH 05/10] move parameter configuration into separate section of the article --- .../configuration/mongodb_session_storage.rst | 85 ++++++++++++------- 1 file changed, 55 insertions(+), 30 deletions(-) diff --git a/cookbook/configuration/mongodb_session_storage.rst b/cookbook/configuration/mongodb_session_storage.rst index 32c7c69176d..b83a43afb29 100644 --- a/cookbook/configuration/mongodb_session_storage.rst +++ b/cookbook/configuration/mongodb_session_storage.rst @@ -25,15 +25,6 @@ need to change/add some parameters in the main configuration file: cookie_lifetime: 2592000 # optional, it is set to 30 days here gc_maxlifetime: 2592000 # optional, it is set to 30 days here - parameters: - # ... - mongo.session.options: - database: session_db # your MongoDB database name - collection: session # your MongoDB collection name - mongodb_host: 1.2.3.4 # your MongoDB server's IP - mongodb_username: my_username - mongodb_password: my_password - services: # ... mongo_client: @@ -68,19 +59,6 @@ need to change/add some parameters in the main configuration file: /> - - - - session_db - - session - - - 1.2.3.4 - my_username - my_password - - @@ -110,14 +88,6 @@ need to change/add some parameters in the main configuration file: ), )); - $container->setParameter('mongo.session.options', array( - 'database' => 'session_db', // your MongoDB database name - 'collection' => 'session', // your MongoDB collection name - )); - $container->setParameter('mongodb_host', '1.2.3.4'); // your MongoDB server's IP - $container->setParameter('mongodb_username', 'my_username'); - $container->setParameter('mongodb_password', 'my_password'); - $container->setDefinition('mongo_client', new Definition('MongoClient', array( // if using a username and password array('mongodb://%mongodb_username%:%mongodb_password%@%mongodb_host%:27017'), @@ -130,6 +100,61 @@ need to change/add some parameters in the main configuration file: array(new Reference('mongo_client'), '%mongo.session.options%') )); +The parameters used above should be defined somewhere in your application, often in your main +parameters configuration: + +.. configuration-block:: + + .. code-block:: yaml + + # app/config/parameters.yml + parameters: + # ... + mongo.session.options: + database: session_db # your MongoDB database name + collection: session # your MongoDB collection name + mongodb_host: 1.2.3.4 # your MongoDB server's IP + mongodb_username: my_username + mongodb_password: my_password + + .. code-block:: xml + + + + + + + + session_db + + session + + + 1.2.3.4 + my_username + my_password + + + + .. code-block:: php + + use Symfony\Component\DependencyInjection\Reference; + use Symfony\Component\DependencyInjection\Definition; + + $container->setParameter('mongo.session.options', array( + 'database' => 'session_db', // your MongoDB database name + 'collection' => 'session', // your MongoDB collection name + )); + $container->setParameter('mongodb_host', '1.2.3.4'); // your MongoDB server's IP + $container->setParameter('mongodb_username', 'my_username'); + $container->setParameter('mongodb_password', 'my_password'); + Setting Up the MongoDB Collection --------------------------------- From dde0ee103ffe27d8f27881a625e78b7119081f71 Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 4 Jul 2015 10:44:25 -0500 Subject: [PATCH 06/10] add link to MongoDB cookbook article --- cookbook/session/sessions_directory.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cookbook/session/sessions_directory.rst b/cookbook/session/sessions_directory.rst index 61922ec6fcd..5ecc133ae9f 100644 --- a/cookbook/session/sessions_directory.rst +++ b/cookbook/session/sessions_directory.rst @@ -93,8 +93,9 @@ that your current sessions aren't lost when you clear Symfony's cache. Using a different session save handler is an excellent (yet more complex) method of session management available within Symfony. See :doc:`/components/http_foundation/session_configuration` for a - discussion of session save handlers. There is also an entry in the cookbook - about storing sessions in the :doc:`database `. + discussion of session save handlers. There are also entries in the cookbook + about storing sessions in a :doc:`relational database ` + or a :doc:`NoSQL database `. To change the directory in which Symfony saves session data, you only need change the framework configuration. In this example, you will change the From 2faa2acef2369c337cb47e052d9c568c3adfaa14 Mon Sep 17 00:00:00 2001 From: Steven Date: Sun, 5 Jul 2015 10:57:18 -0500 Subject: [PATCH 07/10] add information about adding an index to improve garbage collection performance --- cookbook/configuration/mongodb_session_storage.rst | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/cookbook/configuration/mongodb_session_storage.rst b/cookbook/configuration/mongodb_session_storage.rst index b83a43afb29..c2f912e8b9e 100644 --- a/cookbook/configuration/mongodb_session_storage.rst +++ b/cookbook/configuration/mongodb_session_storage.rst @@ -159,6 +159,13 @@ Setting Up the MongoDB Collection --------------------------------- Because MongoDB uses dynamic collection schemas, you do not need to do anything to initialize your -session collection. +session collection. However, you may want to add an index to improve garbage collection performance. +From the `MongoDB shell`_: -.. _installed and configured a MongoDB server: http://docs.mongodb.org/manual/installation/ \ No newline at end of file +.. code-block:: sql + + use session_db + db.session.ensureIndex( { "expireAt": 1 }, { expireAfterSeconds: 0 } ) + +.. _installed and configured a MongoDB server: http://docs.mongodb.org/manual/installation/ +.. _MongoDB shell: http://docs.mongodb.org/v2.2/tutorial/getting-started-with-the-mongo-shell/ \ No newline at end of file From 4238fcc883835821ca446912c61ff6e5794b4401 Mon Sep 17 00:00:00 2001 From: Steven Date: Sun, 5 Jul 2015 10:57:37 -0500 Subject: [PATCH 08/10] add reference to PDO and MongoDB session handlers in session index --- cookbook/session/index.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cookbook/session/index.rst b/cookbook/session/index.rst index 0420126b48e..506ce28fb57 100644 --- a/cookbook/session/index.rst +++ b/cookbook/session/index.rst @@ -8,4 +8,6 @@ Sessions locale_sticky_session sessions_directory php_bridge - avoid_session_start \ No newline at end of file + avoid_session_start + /cookbook/configuration/pdo_session_storage + /cookbook/configuration/mongodb_session_storage \ No newline at end of file From 5f30b073a553afb977daa5cad9ee329c8a1accdb Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 6 Jul 2015 11:00:53 -0500 Subject: [PATCH 09/10] add reference to mongodb session storage to cookbook index --- cookbook/map.rst.inc | 1 + 1 file changed, 1 insertion(+) diff --git a/cookbook/map.rst.inc b/cookbook/map.rst.inc index 36f05e61afc..85b86a76225 100644 --- a/cookbook/map.rst.inc +++ b/cookbook/map.rst.inc @@ -188,6 +188,7 @@ * :doc:`/cookbook/session/sessions_directory` * :doc:`/cookbook/session/php_bridge` * (configuration) :doc:`/cookbook/configuration/pdo_session_storage` + * (configuration) :doc:`/cookbook/configuration/mongodb_session_storage` * :doc:`/cookbook/session/avoid_session_start` * **symfony1** From deccf85689122a458cedfff093fc83c5f7768d90 Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 7 Jul 2015 08:41:22 -0500 Subject: [PATCH 10/10] remove links from incorrect page --- cookbook/session/index.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cookbook/session/index.rst b/cookbook/session/index.rst index 506ce28fb57..0420126b48e 100644 --- a/cookbook/session/index.rst +++ b/cookbook/session/index.rst @@ -8,6 +8,4 @@ Sessions locale_sticky_session sessions_directory php_bridge - avoid_session_start - /cookbook/configuration/pdo_session_storage - /cookbook/configuration/mongodb_session_storage \ No newline at end of file + avoid_session_start \ No newline at end of file