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:password@ds047037.mongolab.com: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?

Ilya Blokh
  • 11,923
  • 11
  • 52
  • 84
  • Do you have the stack trace for the error? It may help in pinpointing the exact cause. You can use [`java.lang.Throwable.printStackTrace()`](http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#printStackTrace()) to get the full trace to go to stderr. – jared Jan 12 '13 at 22:41

2 Answers2

4

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.

Community
  • 1
  • 1
jared
  • 5,369
  • 2
  • 21
  • 24
  • I used dig and got an answer almost equal as yours. It means that it's ok with DNS? – Ilya Blokh Jan 12 '13 at 21:38
  • Did you run dig from the machine that ran the Java you posted or from the web site I linked? – jared Jan 12 '13 at 21:45
  • It's also possible you're running into the [known bug with the Java IPv6 stack](http://stackoverflow.com/questions/1608503/domain-name-resolution-not-working-in-java-applications-on-ubuntu64-9-04-machine), causing DNS resolution to fail from inside Java only. Try adding the `-Djava.net.preferIPv4Stack=true` Java option. – jared Jan 12 '13 at 21:53
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); 
asgoth
  • 35,552
  • 12
  • 89
  • 98
  • 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?? – Ilya Blokh Jan 12 '13 at 20:18
  • You have a firewall which is blocking the outbound request? – asgoth Jan 12 '13 at 21:43
  • Try to connect with the commandline tool to check if that works – asgoth Jan 12 '13 at 21:47