无法使用Java驱动程序连接到副本集,可以使用Node.js驱动程序

时间:2015-06-09 13:44:30

标签: java node.js mongodb mongo-java mongojs

以下是我使用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);
    });
});

我在控制台中获得了5,因此它可以正常工作。 使用Java驱动程序就是我所做的:

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]())
}

我得到以下异常:

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}}]

我不知道它为什么要使用一个驱动程序而不使用另一个驱动程序。

编辑:当我尝试仅使用以下网址mongodb://login:pwd@host:27019/database连接到主节点时,只有当我添加第二个网址时,它才会失败。

0 个答案:

没有答案