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();
}
您已经添加上“MyDatabase的”或“admin”的用户(这是默认的)?换句话说,在createMongoCRCredential中尝试使用“admin”作为数据库。 – mtj
@Zeus代码似乎是正确的。我也这样做,并没有问题连接到我的服务器。在身份验证期间检查服务器上的日志是否有错误。 https://docs.mongodb.com/manual/reference/system-users-collection/ – neomega
可以请身边的人的问题,调试了几个小时之后,数据库管理员告知,他已经改变了用户名和密码。有时我想知道我为什么成为一名软件工程师。 –