Can not connector to replica set with Java driver, works with Node.js driver

1k views Asked by At

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.

0

There are 0 answers