java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused

JIRA | Douglas Hubler | 5 years ago
  1. 0

    WAIT! Before you close this as a duplicate, I do not believe anyone has given the full details. The problem is you simply cannot connect to a mongod from java when it is bound to the 127.0.0.1 interface only. Step 1: configure mongod to 127.0.0.1 only -------------------------- logpath=/var/log/mongo/mongod.log logappend=true fork = true dbpath=/var/lib/mongo replSet =sipxecs port=27017 bind_ip=127.0.0.1 ------------------------- Step 2: Compile and run this code ----------------------------- import com.mongodb.MongoURI; import com.mongodb.Mongo; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.BasicDBObject; public class Test { public static void main(String[] args) throws Exception { Mongo m = new Mongo("127.0.0.1", 27017); DB d = m.getDB("imdb"); DBCollection col = d.getCollection("imdb"); BasicDBObject o = new BasicDBObject(); col.findOne(o); } } -------------------------- You will get the following error ---------------------------- Jan 25, 2012 4:44:21 PM com.mongodb.DBTCPConnector fetchMaxBsonObjectSize WARNING: Exception determining maxBSON size using0 java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused at com.mongodb.DBPort._open(DBPort.java:228) at com.mongodb.DBPort.go(DBPort.java:112) at com.mongodb.DBPort.go(DBPort.java:93) at com.mongodb.DBPort.findOne(DBPort.java:146) at com.mongodb.DBPort.runCommand(DBPort.java:157) at com.mongodb.DBTCPConnector.fetchMaxBsonObjectSize(DBTCPConnector.java:457) at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:444) at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:209) at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:305) at com.mongodb.DBCollection.findOne(DBCollection.java:647) at com.mongodb.DBCollection.findOne(DBCollection.java:626) at Test.main(Test.java:14) Jan 25, 2012 4:44:21 PM com.mongodb.DBPortPool gotError WARNING: emptying DBPortPool to 127.0.0.1:27017 b/c of error java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused at com.mongodb.DBPort._open(DBPort.java:228) at com.mongodb.DBPort.go(DBPort.java:112) at com.mongodb.DBPort.call(DBPort.java:79) at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:218) at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:305) at com.mongodb.DBCollection.findOne(DBCollection.java:647) at com.mongodb.DBCollection.findOne(DBCollection.java:626) at Test.main(Test.java:14) Exception in thread "main" com.mongodb.MongoException$Network: can't call something : /127.0.0.1:27017/imdb at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:227) at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:305) at com.mongodb.DBCollection.findOne(DBCollection.java:647) at com.mongodb.DBCollection.findOne(DBCollection.java:626) at Test.main(Test.java:14) Caused by: java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused at com.mongodb.DBPort._open(DBPort.java:228) at com.mongodb.DBPort.go(DBPort.java:112) at com.mongodb.DBPort.call(DBPort.java:79) at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:218)

    JIRA | 5 years ago | Douglas Hubler
    java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused
  2. 0

    [mongodb-user] db error [not master]

    Google Groups | 7 years ago | Zsombor
    java.io.IOException: connection to server closed unexpectedly
  3. 0

    Connection refused

    GitHub | 3 years ago | aurabhi
    java.io.IOException: couldn't connect to [localhost/127.0.0.1:37017] bc:java.net.ConnectException: Connection refused
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    MongoDB-Java driver: Catch exception when insert fails

    Stack Overflow | 5 years ago | Tigel
    java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect
  6. 0

    mongo couldn't connect to [localhost/127.0.0.1:27017]

    Stack Overflow | 3 years ago | asaf app
    java.io.IOException: couldn't connect to [localhost/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.io.IOException

      couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused

      at com.mongodb.DBPort._open()
    2. MongoDB Java Driver
      DBCollection.findOne
      1. com.mongodb.DBPort._open(DBPort.java:228)
      2. com.mongodb.DBPort.go(DBPort.java:112)
      3. com.mongodb.DBPort.call(DBPort.java:79)
      4. com.mongodb.DBTCPConnector.call(DBTCPConnector.java:218)
      5. com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:305)
      6. com.mongodb.DBCollection.findOne(DBCollection.java:647)
      7. com.mongodb.DBCollection.findOne(DBCollection.java:626)
      7 frames
    3. Unknown
      Test.main
      1. Test.main(Test.java:14)
      1 frame