简体   繁体   中英

how do i insert data with no authentication in MongoDB

Software Gui

Using mongo java driver 3.4.2

I am running Mongodb with this command in CMD "mongod". I am not using "mongod --auth" ! I don't know why it's still giving me Exception.
like in CMD we don't have to provide username and password if server is not running on --auth. similarly i want achieve.

i have created application which inserts data into mongo db Actually its inserting data successfully only that i am still getting exception and its making slower to my application since i am using swingworker therefore my application freeze for a while during exception time.

Is that MongoDB driver has limitation ? That we cannot insert data without authentication.

   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();
}

here is the Exception

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" }}}]

Thank you for helping me

Actually its solved i don't know why ist working after i change this line .

       collectionss.getSelectedValue().toString();// Jlist

to

      Object value = collectionss.getSelectedValue().toString() ;   
      DBCollection bookCollection = db.getCollection(value.toString);

i don't know exactly but i think it has to do something with this SCRAM-SHA-1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM