1

I'm trying to connect to my mongo database which I created via mongolab

There's standard uri mentioned at my database's page at mongolab:enter image description here

I try this code:

String uriString = "mongodb://user:[email protected]:47037/db_test_01";
MongoURI uri = new MongoURI(uriString);
try {
    DB db = uri.connectDB();
    db.authenticate(uri.getUsername(), uri.getPassword());
    Set<String> colls = db.getCollectionNames();
    for (String s : colls) {
        System.out.println(s);
    }
    System.out.println("done");
    } catch (UnknownHostException e) {
        System.out.println("UnknownHostException: " + e);
    } catch(MongoException me) {
        System.out.println("MongoException: " + me);
    }

However, I get UnknownHostException: java.net.UnknownHostException: ds047037.mongolab.com

Docs about UnknownHostException say: "Thrown to indicate that the IP address of a host could not be determined."

What's wrong?

1
  • Do you have the stack trace for the error? It may help in pinpointing the exact cause. You can use java.lang.Throwable.printStackTrace() to get the full trace to go to stderr. Commented Jan 12, 2013 at 22:41

2 Answers 2

5

Looks like a DNS resolution problem at some point in the stack. java.net.UnknownHostException is "Thrown to indicate that the IP address of a host could not be determined".

Problem with Java

The problem may be with Java itself. There is a known issue with Java and IPv6. You can try setting the java.net.preferIPv4Stack system property to true to see if that fixes the issue.

As a parameter to the java command when launching your JVM:

java -Djava.net.preferIPv4Stack=true YourMainClass

Or programmatically:

System.setProperty("java.net.preferIPv4Stack" , "true");

You can also try upgrading to the latest version of Java to get all the bug fixes that come with it.

Problem with your DNS server or configuration

The issue may also be with the DNS configuration on your laptop/machine. You can use dig to see what that DNS lookup returns. Below is the output of what I get from my laptop. You can also use this web version of dig to see what you should be getting.

% dig ds047037.mongolab.com

; <<>> DiG 9.8.3-P1 <<>> ds047037.mongolab.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 20375
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;ds047037.mongolab.com.     IN  A

;; ANSWER SECTION:
ds047037.mongolab.com.  120 IN  CNAME   ds047037-a.mongolab.com.
ds047037-a.mongolab.com. 120    IN  CNAME   h000432.mongolab.com.
h000432.mongolab.com.   120 IN  CNAME   ec2-46-51-159-130.eu-west-1.compute.amazonaws.com.
ec2-46-51-159-130.eu-west-1.compute.amazonaws.com. 300 IN A 46.51.159.130

;; Query time: 23 msec
;; SERVER: 192.168.1.1#53(192.168.1.1)
;; WHEN: Sat Jan 12 13:22:24 2013
;; MSG SIZE  rcvd: 162

If you confirm that your DNS configuration / server is the issue, you will need to determine which DNS servers you're using and then contact the owner / administrator or contact your ISP to make sure they're configured as they should be.

Sign up to request clarification or add additional context in comments.

3 Comments

I used dig and got an answer almost equal as yours. It means that it's ok with DNS?
Did you run dig from the machine that ran the Java you posted or from the web site I linked?
It's also possible you're running into the known bug with the Java IPv6 stack, causing DNS resolution to fail from inside Java only. Try adding the -Djava.net.preferIPv4Stack=true Java option.
0

Try with host and port, so:

Mongo mongo = new Mongo("ds047037.mongolab.com", 47037);
DB db = mongo.getDB("db_test_01");
db.authenticate(user, password); 

3 Comments

Yes, they're correct. From docs about UnknownHostException: "Thrown to indicate that the IP address of a host could not be determined.". Why ip-adr couldn't be determined??
You have a firewall which is blocking the outbound request?
Try to connect with the commandline tool to check if that works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.