2016-12-15 88 views
0

MongoDB的身份驗證失敗我試圖連接使用蒙戈Java客戶端3.2版,並收到以下異常,任何想法有什麼錯在這裏去蒙戈DB 3.2版?從蒙戈Java客戶端

com.mongodb.MongoSecurityException: Exception authenticating 
at com.mongodb.connection.NativeAuthenticator.authenticate(NativeAuthenticator.java:48) 
at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:99) 
at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:44) 
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) 
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:128) 
at java.lang.Thread.run(Thread.java:745) 
Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'auth failed' on server 10.100.5.41:27017. The full response is { "ok" : 0.0, "errmsg" : "auth failed", "code" : 18 } 
at com.mongodb.connection.CommandHelper.createCommandFailureException(CommandHelper.java:170) 
at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:123) 
at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32) 
at com.mongodb.connection.NativeAuthenticator.authenticate(NativeAuthenticator.java:46) 
... 5 more 

以下是我用來連接的代碼。

MongoClient mongoClient = null; 

    try { 
     MongoCredential credential = MongoCredential.createMongoCRCredential("admin", "mydatabase", "admin123".toCharArray()); 
     mongoClient = new MongoClient(new ServerAddress("10.100.5.41", 27017), Arrays.asList(credential)); 

     MongoDatabase database = mongoClient.getDatabase("mydatabase"); 
     System.out.println(database.getName()); 
     MongoCollection collection = database.getCollection("user"); 

     MongoCursor cursor = collection.find().iterator(); 
     while (cursor.hasNext()) { 
      System.out.println(cursor.next()); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
+0

您已經添加上「MyDatabase的」或「admin」的用戶(這是默認的)?換句話說,在createMongoCRCredential中嘗試使用「admin」作爲數據庫。 – mtj

+1

@Zeus代碼似乎是正確的。我也這樣做,並沒有問題連接到我的服務器。在身份驗證期間檢查服務器上的日誌是否有錯誤。 https://docs.mongodb.com/manual/reference/system-users-collection/ – neomega

+0

可以請身邊的人的問題,調試了幾個小時之後,數據庫管理員告知,他已經改變了用戶名和密碼。有時我想知道我爲什麼成爲一名軟件工程師。 –

回答

1

經過數小時的調試,db管理員告訴他,他已經更改了用戶名和密碼。有時我想知道我爲什麼成爲一名軟件工程師。