如何解决错误:ReadPreferenceServerSelector未选择服务器

3

我对Java和MongoDB(或任何数据库)非常陌生,我一直在构建这个Java程序来测试连接。它只是简单地建立连接并列出所有现有的数据库名称。

import com.mongodb.MongoClient;

import com.mongodb.client.MongoCursor;

public class test {
    public static void main(String[] args) {
        try{
            MongoClient mongoClient = new MongoClient( "localhost" , 27017 );

            MongoCursor<String> dbsCursor = mongoClient.listDatabaseNames().iterator();
            while(dbsCursor.hasNext()) {
                System.out.println(dbsCursor.next());
            }

        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
        }
    }
}

MongoDB服务器已经启动。但是,当我运行此应用程序时,它显示以下错误。
Jun 25, 2016 3:35:06 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Jun 25, 2016 3:35:06 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{address=localhost:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
Jun 25, 2016 3:35:06 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Exception in monitor thread while connecting to server localhost:27017
com.mongodb.MongoSocketWriteException: Exception sending message
    at com.mongodb.connection.InternalStreamConnection.translateWriteException(InternalStreamConnection.java:462)
    at com.mongodb.connection.InternalStreamConnection.sendMessage(InternalStreamConnection.java:205)
    at com.mongodb.connection.CommandHelper.sendMessage(CommandHelper.java:89)
    at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32)
    at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:83)
    at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:43)
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:128)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketException: Software caused connection abort: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
    at com.mongodb.connection.SocketStream.write(SocketStream.java:75)
    at        com.mongodb.connection.InternalStreamConnection.sendMessage(InternalStreamConnection.java:201)
... 7 more

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {java.net.SocketException: Software caused connection abort: socket write error}}]

Process finished with exit code 0

这些意味着什么?我应该设置ReadPreferenceServerSelector吗?但是我在网上找不到任何相关的文档。

你的代码运行得很好。我觉得可能是mongodb出了问题。试着停止然后重新启动它。有没有报错信息? - undefined
这个错误一直在重复...所以在执行之前,我首先在命令行中启动了Mongodb。这是唯一需要做的事情吗? 或者可能是安装问题,因为我基本上把每个Mongodb驱动文件都拖到了项目目录中,因为我无法让maven工作。 - undefined
我改变了安装方法:将.jar文件导入项目结构/模块。然而,这个错误仍然发生。 - undefined
你能逐步描述一下你是如何安装MongoDB的,如何将MongoDB的Java库包含到你的项目中,并且如何编译它吗? - undefined
1个回答

0

在发送请求到Mongo时,内部使用ReadPreferenceServerSelector。在您的情况下,您不需要深入了解这个细节。

对我而言,重要的错误是这个。链接的答案描述了为什么可能出现此错误。但是它不应该在多次尝试中重复出现。

软件导致连接中断:套接字写入错误

  1. 您可以检查一下Mongo是否在端口27017上运行,并且能否通过命令行访问?
  2. 只需使用mongo-java-driver就足以运行这样的测试。只需确保jar版本与已安装的mongodb版本匹配即可。更好的做法是使用像maven一样的工具,如此教程所述。

如果在尝试这些方法后错误仍然存在,请发布。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接