[JAVA-510] connection refused connecting when mongod is bound to 127.0.0.1 Created: 25/Jan/12  Updated: 19/Oct/16  Resolved: 27/Jan/12

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Douglas Hubler Assignee: Jeffrey Yemin
Resolution: Cannot Reproduce Votes: 0
Labels: driver
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Fedora 14 x86_64
Java Driver 2.7.0 and 2.7.2



 Description   

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)



 Comments   
Comment by Douglas Hubler [ 27/Jan/12 ]

you can close this.

I upgraded from mongo-server 2.0.0 (from fedora 14) to mongodb-server 2.0.2 (one i built) and now it works. I'm not sure if it's related, but might be. I thought I tried this test many times. Anyway, I verified mongo shell and java cannot connect when using IP from eth0 interface. I didn't check "localhost" v.s. "127.0.0.1" but I can work with "127.0.0.1".

Thanks Jeff!

Comment by Jeffrey Yemin [ 26/Jan/12 ]

I'm able to run this without error. Does the mongo shell work, or does it also complain?

Generated at Thu Feb 08 08:52:28 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.