Sitecore: Setting up Mongo with Authentication

One of the new features in Sitecore 7.5 was the xDB, a new analytics database that rather than using SQL Server was Mongo based. If your main job is as a Sitecore dev, like me this may be the first time you’ve ever had to use Mongo, or a least the first time you’ve ever had to commercially use it.

Installing Mongo DB is a relatively painless experience, Mongo provide a decent enough guide to installing on Windows. Half way through you may be wondering why its running in a console window, but eventually you get to instructions for setting it up as a service.

However there are a couple of gotchas:

  1. Sitecore doesn’t work with the latest version of Mongo. At time of writing the latest version of Sitecore is 8.0 rev. 150427 and Mongo is 3.0. Sitecore however only supports version 2.6.x of Mongo though.
  2. None of the installation instructions include any details on authentication. Which while this is fine for your local dev machine is not so great for a live site.

So here’s my guide for getting up and running with Mongo DB and Sitecore.

Installing Mongo

First off get Mongo DB installed as a service on your machine. I’m not going to include instructions here for this bit, just follow the guide on the Mongo DB website.

One thing to note though, the Windows installer for Mongo 2.6 will by default install Mongo to C:\Program Files\MongoDB 2.6 Standard\. However the instructions for setting up Mongo include example commands expecting it to be installed in C:\mongodb\

Setting Up Authentication

Once you’ve got Mongo up and running you’ll need to create a user.

  1. Open a command prompt and connect to MongoDB
    C:\Program Files\MongoDB 2.6 Standard\bin\mongo.exe
    
  2. Switch to the admin db by doing
    use admin
    
  3. Create the user using
    db.createUser({user: "admin_mongo",pwd: "your password",roles: [ { role: "userAdminAnyDatabase", db:"admin" }, { role: "root", db:"admin" } ]  })
    
  4. To verify the user has been created you can use the following commands
    db.auth("admin_mongo", "your password")
    db.getUsers()
    

Mongo Create Admin User

Next update your config file to require authentication by adding auth=true. You will also need to restart the service for the change to take effect.

logpath=c:\data\log\mongod.log
dbpath=c:\data\db
auth=true

Sitecore uses 4 db’s in Mongo; Analytics, tracking_live, tracking_history and tracking_contact. We need to create a user in each of them.

Open a mongo shell again, switch to the admin db and connect with your admin login.

Now create each of the users as follows:

use analytics
db.createUser({user: "mongo_user",pwd: "your password",roles: [ { role: "readWrite", db:"analytics" } ]  })
 
use tracking_live
db.createUser({user: "mongo_user",pwd: "your password",roles: [ { role: "readWrite", db:"tracking_live" } ]  })
 
use tracking_history
db.createUser({user: "mongo_user",pwd: "your password",roles: [ { role: "readWrite", db:"tracking_history" } ]  })
 
use tracking_contact
db.createUser({user: "mongo_user",pwd: "your password",roles: [ { role: "readWrite", db:"tracking_contact" } ]  })

Sitecore Connection Strings

All that’s left now is to update the connection strings in Sitecore.

The default connection strings Sitecore give you look like this:

  <add name="analytics" connectionString="mongodb://localhost/analytics"/>
  <add name="tracking.live" connectionString="mongodb://localhost/tracking_live"/>
  <add name="tracking.history" connectionString="mongodb://localhost/tracking_history"/>
  <add name="tracking.contact" connectionString="mongodb://localhost/tracking_contact"/>

The format for a connection string with authentication is

mongodb://[username:password@]host1[:port1]/database

Your connection string should now look something like

  <add name="analytics" connectionString="mongodb://mongo_user:your password@localhost/analytics"/>
  <add name="tracking.live" connectionString="mongodb://mongo_user:your password@localhost/tracking_live"/>
  <add name="tracking.history" connectionString="mongodb://mongo_user:your password@localhost/tracking_history"/>
  <add name="tracking.contact" connectionString="mongodb://mongo_user:your password@localhost/tracking_contact"/>
Advertisements

8 thoughts on “Sitecore: Setting up Mongo with Authentication

  1. amitabhvyas says:

    Hi,
    We are upgrading from 7.2 to 8.1.
    Currently upgraded it from 7.2 to 7.5, when trying to integrate MongoDB, we came to know that Client have already setup Mongo 3.0 with SSL Authentication (Which 7.2 doesn’t support, I guess. It is supported by Sitecore 8.0o Update 5 onwards). Please visit the URL below:

    https://kb.sitecore.net/articles/087164
    https://community.sitecore.net/developers/f/5/t/2286

    I tried the solution given by Sean but that dint.

    So here can I upgrade it to Sitecore 8.0 Update 5 first and then perform the following tasks:
    1. MongoDB Integration
    2. Analytics DB conversion
    3. WFM Module Upgrade and Data Conversion

    Please suggest

  2. amitabhvyas says:

    I got my answer. We have to perform all the xDB operation on 7.5 version first then only we can move to upper versions.

  3. tmamedbekov says:

    Is this section:

    Next update your config file to require authentication by adding auth=true. You will also need to restart the service for the change to take effect.

    logpath=c:\data\log\mongod.log
    dbpath=c:\data\db
    auth=true

    Where do I do it for the Sitecore 8.2? Is it in the Sitecore Config Files? or MongoDB config files? If possible please specify the path.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s