Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect to MongoDB (MongoLabs) via C# client

Question Background:

I have setup in MongoLabs (mLab - https://mlab.com/) a database and have added a very simple Collection. I am using the MongoDB driver to attempt to connect and work with this collection via the C# 3.2 driver.

The Issue:

I am unable to connect to my database via the C# driver with a constant authentication expection, as shown:

System.TimeoutException: A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = ReadPreferenceServerSelector{ ReadPreference = { Mode = Primary, TagSets = [] } }, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "Automatic", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/ds048719.mlab.com:48719" }", EndPoint: "Unspecified/ds048719.mlab.com:48719", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> MongoDB.Driver.MongoAuthenticationException: Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1. ---> MongoDB.Driver.MongoCommandException: Command saslStart failed: Authentication failed.

The Code:

I have tried a number of different ways of trying to authenticate the request. Currently I am trying to simply use the MongoClient class, as shown:

MongoClient client;

var connectionString = "mongodb://userNameGoesHereRemovedForSO:[email protected]:48719/db";

client = new MongoClient(connectionString);

var database = client.GetDatabase("testDB");

var collection = database.GetCollection<string>("test");

Any help or examples of how I can successfully overcome this authentication issue would be highly appreciated.

like image 931
user1352057 Avatar asked Oct 25 '16 19:10

user1352057


1 Answers

Quick workaround : just remove database name from the url

var client = new MongoClient("mongodb://root:example@localhost:27017");

var _db = client.GetDatabase("testDB");
like image 86
Ghominejad Avatar answered Oct 22 '22 06:10

Ghominejad