2

My database is hosted in mlab.com and I tried to connect it using mongodb C# driver but I am getting following exception when I try to connect to the mlab database.

MongoCommandException: Command 'saslStart' failed: Authentication failed. 
(response: { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication failed." })   
MongoDB.Driver.Operations.CommandOperation_\`1[MongoDB.Driver.CommandResult].Execute (MongoDB.Driver.Internal.MongoConnection connection)
MongoDB.Driver.Communication.Security.SaslAuthenticationProtocol.RunCommand (MongoDB.Driver.Internal.MongoConnection connection, System.String databaseName, IMongoCommand command) 
MongoDB.Driver.Communication.Security.SaslAuthenticationProtocol.Authenticate (MongoDB.Driver.Internal.MongoConnection connection, MongoDB.Driver.MongoCredential credential)
Rethrow as MongoSecurityException: Error: 18 - Authentication failed.

MongoDB.Driver.Communication.Security.SaslAuthenticationProtocol.Authenticate (MongoDB.Driver.Internal.MongoConnection connection, 
MongoDB.Driver.MongoCredential credential) 
MongoDB.Driver.Communication.Security.Authenticator.Authenticate (MongoDB.Driver.MongoCredential credential) 
MongoDB.Driver.Communication.Security.Authenticator.Authenticate () 
MongoDB.Driver.Internal.MongoConnection.Open () 
MongoDB.Driver.Internal.MongoConnection.GetNetworkStream () 
MongoDB.Driver.Internal.MongoConnection.SendMessage (MongoDB.Bson.IO.BsonBuffer buffer, Int32 requestId)
Rethrow as MongoConnectionException:
Unable to connect to server dsXXXXXX.mlab.com:XXXXX: Error: 18 - Authentication failed.. 

MongoDB.Driver.Internal.DirectMongoServerProxy.Connect (TimeSpan timeout, MongoDB.Driver.ReadPreference readPreference)
MongoDB.Driver.MongoServer.Connect (TimeSpan timeout) MongoDB.Driver.MongoServer.Connect ()

Below is the line which throws the error.

client = new MongoClient (new MongoUrl("mongodb://:@dsxxxxx.mlab.com:xxxxx/mydb"));

And the db user has admin rights as well...username and password are also correct..Need some help :-)

2
  • 1
    correct mongo uri syntax is : mongodb://username:password@host:port/dbname. You don't need to pass :@ in case you don't have auth enabled. Try using : mongodb://dsxxxxx.mlab.com:xxxxx/mydb instead Commented Oct 20, 2016 at 4:44
  • On the mlab when you goto database, it will show both the connection strings. One to use in shell the other with drivers. Can you copy and paste it from there replacing username and password of the db user that you created after creating database? Commented Jul 23, 2017 at 2:05

1 Answer 1

2

I use

var connStr = "mongodb://[username]:[password]@dsxxxx.mongolab.com:xxxx/[dbname]"
IMongoClient client = new MongoClient(connStr);

Notice mongolab.com instead of mlab.com, i just logged into mlab and the suggested connection string uses mongolab.com for me, so double check yours. You should see them as soon as you log into mlab and select your database.

Note that the connection string will be different if you are connecting to a replicaset

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your reply.I still getting the same error. My conncetion string suggests mlab.com and could you please tell me do i have to change any dbuser permissions? I cant even connect using mongoshell..it is not connecting to the db
Have you set up a database user? Your credentials won't be those of your mlab login. You will need to create a user for the database
i am using below command from mongoshell % mongo dsxxxxxx.mlab.com:xxxxx/dbname -u <dbuser> -p <dbpassword> but it says syntax error missing ; before statement @(shell):1:6
are the square brackets required? when connecting with the .net drivers, I found an error when using brackets [] or <>, but success without e.g. let connStr = "mongodb://adminBob:[email protected]:27017/admin"
No they are just place holders to indicate which values go where. Remove squared and angled brackets

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.