首页 文章

MongoDB身份验证出错

提问于
浏览
2

连接到Mongodb时出现此错误 . 我不确定这个错误是什么 .

使用CompositeServerSelector {Selectors = ReadPreferenceServerSelector {ReadPreference = {Mode:Primary}},LatencyLimitingServerSelector }选择服务器30000ms后发生超时 . 集群状态的客户端视图是{ClusterId:“1”,ConnectionMode:“Automatic”,类型:“Unknown”,状态:“Disconnected”,服务器:[{ServerId:“{ClusterId:1,EndPoint:”123.123.123.123: 27017“}”,EndPoint:“123.123.123.123:27017”,状态:“已断开”,类型:“未知”,HeartbeatException:“MongoDB.Driver.MongoConnectionException:打开与服务器的连接时发生异常.--- > MongoDB.Driver.MongoAuthenticationException:无法使用sasl协议机制SCRAM-SHA-1进行身份验证.---> MongoDB.Driver.MongoCommandException:命令saslStart失败:身份验证失败..在MongoDB.Driver.Core.WireProtocol.CommandWireProtocol1.ProcessReply (ConnectionId connectionId,ReplyMessage1回复),位于MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.d__11.MoveNext()---从抛出异常的上一个位置开始的堆栈跟踪结束---在System.Runtime.CompilerServices.TaskAwaiter System.Runtime.CompilerServices.TaskA上的.ThrowForNonSuccess(任务任务) MongoDB.Driver.Core.Authentication.SaslAuthenticator.d__7.MoveNext()---内部异常堆栈跟踪结束时的waiter.HandleNonSuccessAndDebuggerNotification(任务任务)---在MongoDB.Driver.Core.Authentication.SaslAuthenticator.d__7.MoveNext( )---在抛出异常的前一个位置的堆栈跟踪结束---在MongoDB.Driver的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)处.Core.Authentication.AuthenticationHelper.d__1.MoveNext()---从抛出异常的先前位置开始的堆栈跟踪结束---在System.Runtime.CompilerServices上的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) . MongoDB.Driver.Core.Connections.ConnectionInitializer.d__3.MoveNext()中的TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)---抛出异常的前一个位置的堆栈跟踪结束---在System.Runtime.Compiler MongoDB.Driver.Core.Connections.BinaryConnection.d__48.MoveNext()---内部异常堆栈跟踪结束时的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中的Services.TaskAwaiter.ThrowForNonSuccess(任务任务) - - 在MongoDB.Driver.Core.Connections.BinaryConnection.d__48.MoveNext()---从抛出异常的先前位置开始的堆栈跟踪结束---在系统的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) MongoDB.Driver.Core.Servers.ServerMonitor.d__27.MoveNext()“}]}中System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(任务任务)的.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)

谁能帮我吗?

我使用的是MongoDB版本3.4.4 .

谢谢,麻烦您了 .

在Mongodb日志中,它说

来自客户端111.111.111.111:12312的Grandnode上的usernameexample的SCRAM-SHA-1认证失败; UserNotFound:找不到用户usernameexample @Grandnode

但是Grandnode是我想在Grandnode项目中创建的数据库名称 .

如何解决这个问题呢?

2 回答

  • 0

    看起来您在连接时没有设置凭据,添加此块 -

    string username = "user";
    string password = "password";
    string mongoDbAuthMechanism = "SCRAM-SHA-1";
    MongoInternalIdentity internalIdentity = 
              new MongoInternalIdentity("admin", username);
    PasswordEvidence passwordEvidence = new PasswordEvidence(password);
    MongoCredential mongoCredential = 
         new MongoCredential(mongoDbAuthMechanism, 
                 internalIdentity, passwordEvidence);
    List<MongoCredential> credentials = 
               new List<MongoCredential>() {mongoCredential};
    
    
    MongoClientSettings settings = new MongoClientSettings();
    // comment this line below if your mongo doesn't run on secured mode
    settings.Credentials = credentials;
    String mongoHost = "127.0.0.1";
    MongoServerAddress address = new MongoServerAddress(mongoHost);
    settings.Server = address;
    
    MongoClient client = new MongoClient(settings);          
    
    var mongoServer = client.GetDatabase("myDb");
    var coll = mongoServer.GetCollection<Employee>("Employees");
    
    // any stubbed out class
    Employee emp = new Employee()
    {
        Id = Guid.NewGuid().ToString(),
        Name = "Employee_" + DateTime.UtcNow.ToString("yyyy_MMMM_dd")
    };
    
    coll.InsertOne(emp);
    
  • 2

    我的Azure托管的MongoDb(Cosmos Db)出现了类似的错误 . 结果是网络设置,以至于我阻止了所有访问 . 将其更改为允许从“所有网络”进行访问可修复此问题 .

    该错误是非常误导的,我本来期望连接超时 .

    使用CompositeServerSelector选择服务器30000ms后发生超时{Selectors = MongoDB.Driver.MongoClient AreSessionsSupportedServerSelector,LatencyLimitingServerSelector } . 集群状态的客户端视图是{ClusterId:“1”,ConnectionMode:“ReplicaSet”,类型:“ReplicaSet”,状态:“Disconnected”,服务器:[{ServerId:“{ClusterId:1,EndPoint:”Unspecified / XXXX . documents.azure.com:10255“}”,EndPoint:“Unspecified / XXXX.documents.azure.com:10255”,State:“Disconnected”,Type:“Unknown”,HeartbeatException:“MongoDB.Driver.MongoConnectionException:打开与服务器的连接时发生异常 . ---> MongoDB.Driver.MongoAuthenticationException:无法使用sasl协议机制SCRAM-SHA-1进行身份验证 . ---> MongoDB.Driver.MongoCommandException:命令saslContinue失败:未经过身份验证 .

    麻烦我从MongoDb指南针尝试过,也没用,告诉我这不是代码 .

相关问题