[英]Authentication Error While Connecting to Mongo
Exception while connecting to Mongo DB : 连接到Mongo DB时发生异常:
Below is the Stack Trace : 以下是堆栈跟踪:
Exception in thread "main" com.mongodb.CommandFailureException: { "serverUsed" : "127.0.0.1:27017" , "ok" : 0.0 , "code" : 18 , "errmsg" : "Authentication failed."}
at com.mongodb.CommandResult.getException(CommandResult.java:76)
at com.mongodb.CommandResult.throwOnError(CommandResult.java:140)
at com.mongodb.DBPort$SaslAuthenticator.authenticate(DBPort.java:899)
at com.mongodb.DBPort.authenticate(DBPort.java:432)
at com.mongodb.DBPort.checkAuth(DBPort.java:443)
at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:293)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:273)
at com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:84)
at com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:66)
at com.mongodb.DBCursor._check(DBCursor.java:498)
at com.mongodb.DBCursor._hasNext(DBCursor.java:621)
at com.mongodb.DBCursor.hasNext(DBCursor.java:657)
at mongoOperations.mongoOperations.getConnectionMongo(mongoOperations.java:103)
at mongoOperations.mongoOperations.main(mongoOperations.java:138)
Below is the Code Snippet : 以下是代码段:
private static void getConnectionMongo(){
try{
String uri = "mongodb://accountUser:password@127.0.0.1/test";
MongoClientURI mongoClientURI = new MongoClientURI(uri);
MongoURI mongoURI = new MongoURI(mongoClientURI);
Mongo mongo = new Mongo(mongoURI);
UserCredentials userCredentials = new UserCredentials("accountUser", "password");
SimpleMongoDbFactory dbFactory = new SimpleMongoDbFactory(mongo, "test", userCredentials);
MongoTemplate mongoTemplate = new MongoTemplate(dbFactory);
DBCollection collection = mongoTemplate.getCollection("table");
DBCursor cursor = collection.find();
while(cursor.hasNext()){
System.out.println(cursor.next());
}
}
catch(UnknownHostException e){
}
}
Below is the Code snippet from mongo Shell: 以下是mongo Shell的代码段:
db.auth('accountUser','password');
11
These are the jar files used : spring-data-mongodb-1.3.0.RELEASE, mongo-java-driver-2.13.2 这些是使用的jar文件:spring-data-mongodb-1.3.0.RELEASE,mongo-java-driver-2.13.2
Any advice will be appreciable. 任何建议都是可取的。 Thanks !!
谢谢 !!
Either pass this URI 要么通过这个URI
String uri = "mongodb://accountUser:password@127.0.0.1/test";
or pass userCredentials to constructor both can not go together, still can you highlight which line exactly it throws an error 或将userCredentials传递给构造函数都不能同时使用,仍然可以突出显示哪一行会引发错误
MongoTemplate mongoTemplate = new MongoTemplate(dbFactory);
DBCollection collection = mongoTemplate.getCollection("table");
DBCursor cursor = collection.find();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.