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

2020-02-13 09:00发布

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:passwordGoesHereRemovedForSO@ds048555.mlab.com: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.

3条回答
▲ chillily
2楼-- · 2020-02-13 09:25

If I had to hazard a guess, the issue is most likely a firewall issue. You should check the following

  • nslookup of the host (ds048719.mlab.com) from the C# Application Host
  • ping of the host (ds048719.mlab.com) from the C# Application Host (might fail, depending on mLab's settings)
  • That your IP address is whitelisted
  • Test the connection using the Mongo Shell from the same host where the C# Application is running. mLab has docs here.
  • Test the connection with a raw telnet, eg telnet ds048719.mlab.com 48719
  • Ensure you are using the correct authenticationDatabase (in your example, this is specified by the /db), this is usually admin but could be your database name if you are on a shared instance.

You can find the docs on connecting with the C# driver in the MongoDB C# Driver Docs. It is important to note the following:

The Database Component

The database component is optional and is used to indicate which database to authenticate against. When the database component is not provided, the “admin” database is used.

mongodb://host:27017/mydb

Above, the database by the name of “mydb” is where the credentials are stored for the application.

NOTE:

Some drivers utilize the database component to indicate which database to work with by default. The .NET driver, while it parses the database component, does not use the database component for anything other than authentication.

Finally, I would suggest in the future, obfuscate the hostname and port when posting to SO. While security through obscurity alone is a bad policy, it certainly adds a layer of defense for your MongoDB deployment.

查看更多
唯我独甜
3楼-- · 2020-02-13 09:31

I have similar issue, in my case user wasn't created in database. You can create user by this

db.createUser({
      user: "userNameGoesHereRemovedForSO",
      pwd: passwordGoesHereRemovedForSO",
      roles: ["readWrite", "dbAdmin"]
    });
查看更多
冷血范
4楼-- · 2020-02-13 09:37

Your connection string may be incorrect. You have /db at the end, is "db" the name of your authentication database?

Ensure your authentication database is correct. If it is not you may want to remove it all together and let it fall back to admin

查看更多
登录 后发表回答