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:
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?
Looks like a DNS resolution problem at some point in the stack.
java.net.UnknownHostExceptionis “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.preferIPv4Stacksystem property totrueto see if that fixes the issue.As a parameter to the
javacommand when launching your JVM:Or programmatically:
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
digto 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.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.