Authenticate to Mongo Database that isn’t “admin”

If you have set up your Mongo instance by adding a user to the “admin” database to authenticate against, you may of run into some confusion about how you connect & authenticate against another database on that server.

Let’s say you are trying to use the “local” database… If you using command line, you would need to add the parameter –authenticationDatabase. So your connection would look something like:

mongo --username martin.milsom --authenticationDatabase admin -p myPassword

The extra parameter lets Mongo know where the user is to authenticate against.

Now, if you are using the C# driver for this, as I was, then the answer is to include the same parameter in the connection URL, for example:

var connectionString = "mongodb://";
var database = new MongoClient(connectionString).GetDatabase("local");

Note here, the Url Parameter "?authenticationDatabase=admin" is how you add the parameter.

Leave a Reply

Please log in using one of these methods to post your comment: 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 )

Connecting to %s