0

Here is how I connect to my replica set with Node.js:

var MongoClient = require('mongodb').MongoClient;

var url = 'mongodb://ec2-54-237-60-17.compute-1.amazonaws.com:27017/test?readPreference=secondary';

MongoClient.connect(url, function(err, db) {
    console.log(err);

    db.collection('apps').find({}).limit(5).toArray(function(err, docs) {
        console.log(docs);
    });
});

and I get 5 in the console so it works correctly. With the Java driver here is what I do:

import com.mongodb.MongoClient
import com.mongodb.MongoClientURI
import java.util.ArrayList
import com.mongodb.BasicDBObject

object Main extends App {
    val url = "mongodb://ec2-54-237-60-17.compute-1.amazonaws.com:27017/test?readPreference=secondary"
    val client = new MongoClient(new MongoClientURI(url))

    val db = client.getDatabase("database")
    val collection = db.getCollection("apps")
    val results = collection.find().limit(5).into(new ArrayList[org.bson.Document]())
}

and I get the following exception:

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=secondary}. Client view of cluster state is {type=REPLICA_SET, servers=[{address=belinda-mongo.localdomain:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: belinda-mongo.localdomain}, caused by {java.net.UnknownHostException: belinda-mongo.localdomain}}, {address=detroit-mongo.localdomain:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: detroit-mongo.localdomain}, caused by {java.net.UnknownHostException: detroit-mongo.localdomain}}, {address=dione-mongoar.localdomain:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: dione-mongoar.localdomain}, caused by {java.net.UnknownHostException: dione-mongoar.localdomain}}, {address=pandora-mongo.localdomain:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: pandora-mongo.localdomain}, caused by {java.net.UnknownHostException: pandora-mongo.localdomain}}, {address=riverside-mongo.localdomain:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: riverside-mongo.localdomain}, caused by {java.net.UnknownHostException: riverside-mongo.localdomain}}]
com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=secondary}. Client view of cluster state is {type=REPLICA_SET, servers=[{address=belinda-mongo.localdomain:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: belinda-mongo.localdomain}, caused by {java.net.UnknownHostException: belinda-mongo.localdomain}}, {address=detroit-mongo.localdomain:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: detroit-mongo.localdomain}, caused by {java.net.UnknownHostException: detroit-mongo.localdomain}}, {address=dione-mongoar.localdomain:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: dione-mongoar.localdomain}, caused by {java.net.UnknownHostException: dione-mongoar.localdomain}}, {address=pandora-mongo.localdomain:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: pandora-mongo.localdomain}, caused by {java.net.UnknownHostException: pandora-mongo.localdomain}}, {address=riverside-mongo.localdomain:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: riverside-mongo.localdomain}, caused by {java.net.UnknownHostException: riverside-mongo.localdomain}}]

I have no idea why it's working with one driver but no the other.

Edit: When I'm trying to connect only to the primary node with the following url mongodb://login:pwd@host:27019/database it works, it fails only when I add the second one.

mravey
  • 4,380
  • 2
  • 21
  • 31
  • your error message refers to port `27017` (default mongo port), which seems odd considering your mongo url – user2524973 Jun 09 '15 at 13:49
  • Does including the replicaSet name fix it eg: `mongodb://login:pwd@host:27019,host:27021/database?readPreference=secondary&replicaSet=test` – Ross Jun 10 '15 at 13:27
  • @Ross, no it does not work either. – mravey Jun 16 '15 at 07:25
  • Strange. Seems that non of the hosts are accessible from the Java app eg: `riverside-mongo.localdomain` - can you ping them? As they don't appear to be in you host string where are they set? How is the replicaSet configured? Is `riverside-mongo.localdomain:27017` part of the replicaSet config that you are trying to connect to? – Ross Jun 16 '15 at 08:55
  • @Ross I'm trying to connect to the MongoDB of one of our client, so I'm not the one managing the replica set. Thus the hosts are not defined on our servers. But I do not understand how it can works with the JavaScript driver and not with the Java driver. And why it's working with the Java driver when I include only the master address. – mravey Jun 23 '15 at 09:11
  • Are you connecting with the same hosts as listed in the `rs.conf`? It could be a dns resolution issue. The Java driver follows the Server Discovery and Monitoring Spec: https://github.com/mongodb/specifications/tree/master/source/server-discovery-and-monitoring could be an issue with the configuration. Without knowing more about how its configured I can't help more. – Ross Jun 23 '15 at 12:16
  • @Ross I reproduced the issue without the hosts issue. I've updated the urls in the question. You can try, it will work with the javascript driver, but not with the java one. – mravey Jul 16 '15 at 13:58

0 Answers0