An error is reported when java connects to mongodb.

Source: Internet
Author: User

An error is reported when java connects to mongodb.

<Span style = "background-color: rgb (255,255,255); font-family: Arial, Helvetica, sans-serif;"> An error is returned when you write a short test program, depressing </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 {           Mongo 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.20.7.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)



Check the server port: tcp 0 127.0.0.1: 27017 0.0.0.0: * LISTEN 31933/mongod


The port listening is OK. That is 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


You can see a record like this. Who is the port open. Commented out. OK.

The correct result is as follows:

{"_ Id": {"$ oid": "54a8fcad2d1dc6f5db50564b"}, "name": "hanhannah", "no": 2}
The Search Query has Executed!

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.