无法通过 C# 客户端连接到 MongoDB (MongoLabs)
Unable to connect to MongoDB (MongoLabs) via C# client
问题背景:
我在 MongoLabs (mLab - https://mlab.com/) 中设置了一个数据库并添加了一个非常简单的集合。我正在使用 MongoDB 驱动程序尝试通过 C# 3.2 驱动程序连接和使用此集合。
问题:
我无法通过具有恒定身份验证期望的 C# 驱动程序连接到我的数据库,如图所示:
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.
代码:
我尝试了多种不同的方法来验证请求。目前我正在尝试简单地使用 MongoClient
class,如图所示:
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");
如能成功解决此身份验证问题,我将不胜感激。
如果我不得不冒险猜测,问题很可能是防火墙问题。您应该检查以下内容
来自 C# 应用程序主机的主机 (ds048719.mlab.com) 的 nslookup
来自 C# 应用程序主机的主机 (ds048719.mlab.com) 的 ping
(可能会失败,具体取决于 mLab 的设置)
- 你的IP地址是whitelisted
- 使用来自 C# 应用程序 运行 的同一主机的 Mongo Shell 测试连接。 mLab 有文档 here.
- 使用原始
telnet
测试连接,例如 telnet ds048719.mlab.com 48719
- 确保您使用的是正确的
authenticationDatabase
(在您的示例中,这是由 /db
指定的),这通常是 admin
,但如果您是在共享实例上。
您可以在 MongoDB C# Driver Docs 中找到有关连接 C# 驱动程序的文档。请务必注意以下几点:
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.
最后,我建议以后在发布到 SO 时混淆主机名和端口。虽然仅通过默默无闻来确保安全是一项糟糕的政策,但它肯定会为您的 MongoDB 部署增加一层防御。
您的连接字符串可能不正确。你最后有 /db,"db" 是你的身份验证数据库的名称吗?
确保您的身份验证数据库是正确的。如果不是,您可能希望将其全部删除并让它回退到管理员
我有类似的问题,在我的例子中,用户不是在数据库中创建的。您可以通过此创建用户
db.createUser({
user: "userNameGoesHereRemovedForSO",
pwd: "passwordGoesHereRemovedForSO",
roles: ["readWrite", "dbAdmin"]
});
快速解决方法:只需从 url
中删除数据库名称
var client = new MongoClient("mongodb://root:example@localhost:27017");
var _db = client.GetDatabase("testDB");
问题背景:
我在 MongoLabs (mLab - https://mlab.com/) 中设置了一个数据库并添加了一个非常简单的集合。我正在使用 MongoDB 驱动程序尝试通过 C# 3.2 驱动程序连接和使用此集合。
问题:
我无法通过具有恒定身份验证期望的 C# 驱动程序连接到我的数据库,如图所示:
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.
代码:
我尝试了多种不同的方法来验证请求。目前我正在尝试简单地使用 MongoClient
class,如图所示:
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");
如能成功解决此身份验证问题,我将不胜感激。
如果我不得不冒险猜测,问题很可能是防火墙问题。您应该检查以下内容
-
来自 C# 应用程序主机的主机 (ds048719.mlab.com) 的
nslookup
来自 C# 应用程序主机的主机 (ds048719.mlab.com) 的 ping
(可能会失败,具体取决于 mLab 的设置)- 你的IP地址是whitelisted
- 使用来自 C# 应用程序 运行 的同一主机的 Mongo Shell 测试连接。 mLab 有文档 here.
- 使用原始
telnet
测试连接,例如telnet ds048719.mlab.com 48719
- 确保您使用的是正确的
authenticationDatabase
(在您的示例中,这是由/db
指定的),这通常是admin
,但如果您是在共享实例上。
您可以在 MongoDB C# Driver Docs 中找到有关连接 C# 驱动程序的文档。请务必注意以下几点:
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.
最后,我建议以后在发布到 SO 时混淆主机名和端口。虽然仅通过默默无闻来确保安全是一项糟糕的政策,但它肯定会为您的 MongoDB 部署增加一层防御。
您的连接字符串可能不正确。你最后有 /db,"db" 是你的身份验证数据库的名称吗?
确保您的身份验证数据库是正确的。如果不是,您可能希望将其全部删除并让它回退到管理员
我有类似的问题,在我的例子中,用户不是在数据库中创建的。您可以通过此创建用户
db.createUser({
user: "userNameGoesHereRemovedForSO",
pwd: "passwordGoesHereRemovedForSO",
roles: ["readWrite", "dbAdmin"]
});
快速解决方法:只需从 url
var client = new MongoClient("mongodb://root:example@localhost:27017");
var _db = client.GetDatabase("testDB");