<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