0
沒有認證插入數據使用蒙戈Java驅動程序3.4.2
我正在運行的MongoDB在CMD「的mongod」這個命令。我沒有使用「mongod - auth」!我不知道爲什麼它仍然給我例外。
像在CMD中一樣,如果服務器沒有在-auth上運行,我們不必提供用戶名和密碼。同樣我想實現。
我創建的應用程序,將數據插入蒙戈DB 其實它插入數據成功只是我仍然得到例外,它的,因爲我用的SwingWorker因此我申請凍結期間異常的時間,而我的應用程序進行慢。
MongoDB驅動程序有限制嗎?我們不能在沒有 認證的情況下插入數據。
public void doJob() {
server.progressbar.setVisible(true);
server.progressbar.setIndeterminate(true);
SwingWorker worker = new SwingWorker() {
@Override
protected void done() {
server.progressbar.setIndeterminate(false);
server.progressbar.setVisible(false);
server.total.setText("");
CoonectToDB obj=new CoonectToDB();
obj.callcollections();
NoAuthCoonectToDB obj2=new NoAuthCoonectToDB();
obj2.NoAuthconnectDB();
}
@Override
protected void process(List chunks) {
// Here you can process the result of "doInBackGround()"
// Set a variable in the dialog or etc.
}
@Override
protected Object doInBackground() {
try{
server.newcol.setEnabled(false);
server.ecoll.setEnabled(false);
server.ubtn.setEnabled(false);
String logid="Log-"+logname.getText();
JList dataList=(loglist);
int sixe=dataList.getModel().getSize();
ArrayList arrayList = new ArrayList();
for (int i = 0; i <sixe; i++) {
arrayList.add(dataList.getModel().getElementAt(i));
server.total.setText("Total Log-"+i);
}
System.out.println(arrayList);
Iterator itr = arrayList.iterator();
String host=hname.getText();
String port=spport.getValue().toString();
Integer pt = Integer.valueOf(port);
MongoClient mongoClient = new MongoClient(new ServerAddress(host, pt),
MongoClientOptions.builder()
.serverSelectionTimeout(2000)
.build());
DB db = mongoClient.getDB(dbname.getText());
DBCollection bookCollection = db.getCollection(collectionss.getSelectedValue().toString());
BasicDBObject doc = new BasicDBObject(logid, arrayList);
bookCollection.insert(doc);
Icon icon = new ImageIcon("src\\images\\done.png");
processimage.setIcon(icon);
server.consolelog.setText("INFO: Data is inserted succsesfully");
}catch(Exception e){
server.consolelog.setText(e.toString());
}finally{
server.newcol.setEnabled(true);
server.ecoll.setEnabled(true);
server.ubtn.setEnabled(true);
}
return null;
}
};
worker.execute();
}
這裏是異常
com.mongodb.MongoTimeoutException: Timed out after 2000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='', source='admin', password=<hidden>, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }}}]
謝謝你幫我
確實顯示身份驗證已啓用。你有沒有指定keyFile?這也會啓用身份驗證。你可以通過mongo shell來驗證嗎? – helmy