2017-06-13 235 views
1

連接到Mongodb時出現此錯誤。我不確定這個錯誤是什麼。MongoDB身份驗證錯誤

超時30000ms選擇使用CompositeServerSelector {:主}},{LatencyLimitingServerSelector = AllowedLatencyRange 00:00:00.0150000}選擇器= ReadPreferenceServerSelector {ReadPreference = {}模式的服務器之後發生。客戶端集羣狀態視圖是{ClusterId:「1」,ConnectionMode:「Automatic」,Type:「Unknown」,State:「Disconnected」,Servers:[{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.CommandWireProtocol 1.ProcessReply(ConnectionId connectionId, ReplyMessage 1回覆)在MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.d__11.MoveNext()---從以前的位置拋出異常的堆棧跟蹤結束---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAn在MongoDB.Driver.Core.Authentication.SaslAuthenticator.d__7.MoveNext()的dDebuggerNotification(任務任務)---內部異常堆棧跟蹤結束---在MongoDB.Driver.Core.Authentication.SaslAuthenticator.d__7.MoveNext() - - 從以前引發異常的位置結束堆棧跟蹤---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)上的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)在MongoDB.Driver.Core .Authentication.AuthenticationHelper.d__1.MoveNext()---從之前位置拋出異常的堆棧跟蹤結束---在System.Runtime.CompilerServices.TaskAwaiter的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)。 HandleNonSuccessAndDebuggerNotification(任務任務)在MongoDB.Driver.Core.Connections.ConnectionInitializer.d__3.MoveNext()---從上一個位置拋出異常的堆棧跟蹤結束---在System.Runtime.CompilerServices.TaskAwaiter.Thro System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)上的wForNonSuccess(任務任務),位於MongoDB.Driver.Core.Connections.BinaryConnection.d__48.MoveNext()---內部異常堆棧跟蹤結束---在MongoDB上。 Driver.Core.Connections.BinaryConnection.d__48.MoveNext()---從先前位置拋出異常的堆棧跟蹤結束---在System.Runtime.CompilerServices的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務) .TaskAwaiter.HandleNonSuccessAndDebuggerNotification在System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(工作任務)在MongoDB.Driver.Core.Servers.ServerMonitor.d__27.MoveNext((工作任務))」}]}

人幫幫我?

我正在使用MongoDB版本3.4.4。

請謝謝。

在MongoDB的日誌,它說,

SCRAM-SHA-1認證從客戶111.111.111.111:12312失敗USERNAMEEXAMPLE上Grandnode; UserNotFound:找不到用戶usernameexample @ Grandnode

但是Grandnode是我想要在Grandnode項目中創建的數據庫名稱。

如何解決這個問題?

+0

的錯誤意味着您提供的不正確的身份驗證憑據。檢查你有一個有效的用戶名和密碼。如果您確信自己確實可以驗證這些憑據,則可以從其他客戶端(例如mongo shell)進行連接,然後在代碼中包含與您的問題建立連接的代碼。這比堆棧跟蹤更有用。 –

+0

我可以通過Robomongo連接到mongodb服務器,但是它不能連接到c#代碼。 – Desmond

+0

顯示你所要求的代碼。 –

回答

1

看起來你不設置在連接憑據,添加此塊 -

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); 
+0

這幫助我解決了我的問題,使用.net core 2與2.6版本的驅動程序。 –