MongoDB C#身份验证异常

时间:2016-10-20 03:54:36

标签: c# mongodb authentication exception database

我的数据库托管在mlab.com中,我尝试使用mongodb C#驱动程序连接它,但是当我尝试连接到mlab数据库时,我遇到异常。

MongoCommandException:Command' saslStart'失败:身份验证失败。 (回复:{" ok":0.0,"代码":18," errmsg":"身份验证失败。"})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命令)MongoDB.Driver.Communication.Security.SaslAuthenticationProtocol.Authenticate(MongoDB.Driver.Internal.MongoConnection connection,MongoDB.Driver.MongoCredential credential)Rethrow as MongoSecurityException:错误:18 - 身份验证失败。 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)重新抛出MongoConnectionException:无法连接到服务器dsXXXXXX.mlab.com:XXXXX:错误:18 - 身份验证失败.. MongoDB.Driver.Internal.DirectMongoServerProxy.Connect(TimeSpan超时,MongoDB.Driver.ReadPreference readPreference)MongoDB .Driver.MongoServer.Connect(TimeSpan超时)MongoDB.Driver.MongoServer.Connect()

下面是引发错误的行。

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

并且db用户也有管理员权限...用户名和密码也正确..需要一些帮助: - )

2 个答案:

答案 0 :(得分:1)

我用

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

注意mongolab.com而不是mlab.com,我刚登录到mlab,建议的连接字符串使用mongolab.com,所以仔细检查你的。一旦登录mlab并选择数据库,就应该看到它们。

请注意,如果要连接到复制集

,连接字符串将不同

答案 1 :(得分:1)

谢谢,听从了Pieperu的建议。为数据库设置另一组凭据(可以在“集合”选项卡旁边找到)。将这些新设置的凭据附加到connStr上即可。