Sitecore 8: xDB – Using a Shared Database User on MongoDB Results in an Exception

When you use authentication with SQL, you have the choice of using a server level user, or a database level user.  Usually, you would choose to use a server level user and grant access to the user per database.  If you want to do the same thing in MongoDB, you can create a user in the admin database, and then grant rights to each individual database that you would want your user to access.  Then, in your connection string, you set the authSource parameter to the admin database, as in:

<add name="analytics" connectionString="mongodb://shareduser:password@," />

If you are using this model on Sitecore 8 with MongoDB, you may come across the following error:

Cannot obtain number of chunks from config database
Exception: MongoDB.Driver.MongoCommandException
Message: Command 'count' failed: not authorized on config to execute command { count: "chunks", query: { ns: "sc8prod_analytics.Interactions" } } (response: { "ok" : 0.0, "errmsg" : "not authorized on config to execute command { count: \"chunks\", query: { ns: \"sc8prod_analytics.Interactions\" } }", "code" : 13 })
Source: MongoDB.Driver
 at MongoDB.Driver.Operations.CommandOperation`1.Execute(MongoConnection connection)
 at MongoDB.Driver.MongoCollection.RunCommandAs[TCommandResult](IMongoCommand command, IBsonSerializer resultSerializer, IBsonSerializationOptions resultSerializationOptions)
 at MongoDB.Driver.MongoCollection.RunCommandAs[TCommandResult](IMongoCommand command)
 at MongoDB.Driver.MongoCollection.Count(IMongoQuery query)
 at Sitecore.Analytics.Data.DataAccess.MongoDb.MongoDbCollectionShardingInformation.GetNumberOfChunks()
 at Sitecore.Analytics.Data.DataAccess.MongoDb.MongoDbInteractionStorage.MergeVisits(Guid dyingContactId, Guid survivingContactId)

We decompiled the source code and determined the root cause of this to be that the Sitecore code is trying to access the config database (which incidentally may or may not exist).

public int GetNumberOfChunks()
 MongoDatabase sisterDatabase = this.collection.Database.GetSisterDatabase("config");
 MongoCollection<BsonDocument> collection = sisterDatabase.GetCollection("chunks");
 long num = collection.Count(Query.EQ("ns", this.collection.FullName));
 if (num > (long)2147483647)
 throw new Exception("System has detected too many chunks in the collection.");
 return (int)num;

The solution here was to grant the shared user read rights to the config database that didn’t exist:

db.grantRolesToUser(“MONGOUSER”,   [ { role: “read”, db: “config” } ] )


One Response to “Sitecore 8: xDB – Using a Shared Database User on MongoDB Results in an Exception”

  1. Sitecore 8 MongoCommandException using Mongo Db as a service (mLab) – Sitecore.Blog() Says:

    […] In essence Andy Cohen has already described the solution to overcome the MongoCommandException in one of his blog posts. […]

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: