Java Connection MongoDB Error

Source: Internet
Author: User

<span style= "Background-color:rgb (255, 255, 255); Font-family:arial, Helvetica, Sans-serif; > Write a short test program but error, depressed </span>

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " ></span><pre name= "code" class= "html" >public static void Main (string[] args) {try {Mon           Go MONGO = new MONGO ("10.2.167.27", 27017);           DB db = Mongo.getdb ("Company");           Dbcollection collection = Db.getcollection ("Employees");       Basicdbobject employee = new Basicdbobject ();       Employee.put ("name", "Hannah");           Employee.put ("No", 2);           Collection.insert (employee);       Basicdbobject Searchemployee = new Basicdbobject ();           Searchemployee.put ("No", 2);           dbcursor cursor = Collection.find (searchemployee);       while (Cursor.hasnext ()) {System.out.println (Cursor.next ());          } System.out.println ("The Search Query has executed!");      } catch (Unknownhostexception e) {e.printstacktrace ();      } catch (Mongoexception e) {e.printstacktrace ();     }         }


The error message is as follows:


Java.io.IOException:couldn ' t connect to [/10.2.167.27:27017] Bc:java.net.ConnectException:Connection refused: Connect
At Com.mongodb.dbport._open (dbport.java:224)
At Com.mongodb.DBPort.go (dbport.java:101)
At Com.mongodb.DBPort.go (dbport.java:82)
At Com.mongodb.DBPort.findOne (dbport.java:142)
At Com.mongodb.DBPort.runCommand (dbport.java:151)
At Com.mongodb.DBTCPConnector.fetchMaxBsonObjectSize (dbtcpconnector.java:429)
At Com.mongodb.Mongo.getMaxBsonObjectSize (mongo.java:544)
At Com.mongodb.dbapilayer$mycollection.insert (dbapilayer.java:244)
At Com.mongodb.dbapilayer$mycollection.insert (dbapilayer.java:217)
At Com.mongodb.DBCollection.insert (dbcollection.java:87)
At MongoDB. Mongodbclient.main (mongodbclient.java:28)



Look at the port of the server: TCP 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 31933/mongod


Indicates that the port listener is OK. That's the configuration problem.

Open vim/etc/mongod.conf

# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip=127.0.0.1


See a record like this. The port is open to WHO. Commented out. OK is available for remote access.

Get the correct results as follows:

{"_id": {"$oid": "54a8fcad2d1dc6f5db50564b"}, "name": "Hannah", "No": 2}
The Search Query has executed!

Java Connection MongoDB Error

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.