Java 未授权使用eclipse查询mongodb上的databasename.collection
我给DB PUPP指定的角色是UserAdmin,我使用Java 未授权使用eclipse查询mongodb上的databasename.collection,java,eclipse,mongodb,authentication,Java,Eclipse,Mongodb,Authentication,我给DB PUPP指定的角色是UserAdmin,我使用mongod--auth--dbpath c:\mongodb\data\DB运行服务器。 首先,我在eclipse中创建了一个java文件来连接DB,它工作正常 在eclipse中创建并运行以下文件后: try { MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("purplista");
mongod--auth--dbpath c:\mongodb\data\DB
运行服务器。
首先,我在eclipse中创建了一个java文件来连接DB,它工作正常
在eclipse中创建并运行以下文件后:
try {
MongoClient mongoClient = new MongoClient("localhost", 27017);
DB db = mongoClient.getDB("purplista");
boolean auth =db.authenticate("purp","123".toCharArray());
System.out.println("Connect to database successfully");
DBCollection doc = db.getCollection("test");
System.out.println("Collection test selected successfully..");
DBCursor cursor = doc.find();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} catch (Exception e) {
e.printStackTrace();
}
出现如下错误:
成功连接到数据库
已成功选择收集测试
com.mongodb.MongoException:未授权查询purplista.test
在
com.mongodb.queryResulterator.throwOnQueryFailure(queryResulterator.java:214)
位于com.mongodb.queryResulterator.init(queryResulterator.java:198)
在
com.mongodb.queryResulterator.initFromQueryResponse(queryResulterator.java:176)
在
QueryResulterator.java:64)
位于com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:80)
com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:61)位于
com.mongodb.DBCursor.\u检查(DBCursor.java:458)
com.mongodb.DBCursor.\u hasNext(DBCursor.java:546)位于
hasNext(DBCursor.java:571)位于
purplista.FindDoc.main(FindDoc.java:34)
我看到的几个问题是:
看起来您没有进行身份验证。你添加用户了吗?您的mongoDb可能正在运行安全模式。只需使用布尔auth=db.authenticate(“db”,“password”.toCharArray())进行检查;