Java WritableServerSelector未从群集中选择服务器

Java WritableServerSelector未从群集中选择服务器,java,mongodb,Java,Mongodb,作为MongoDB的新手,我正试图在新安装的MongoDB(v3.2.4)中插入一个简单的文档。使用MongoDB驱动程序3.2.2。 下面是我的代码: public <classname>() { public static final String COLLECTION_NAME = "logs"; MongoClient mongoClient = new MongoClient("<server-adress>"); MongoDataba

作为MongoDB的新手,我正试图在新安装的MongoDB(v3.2.4)中插入一个简单的文档。使用MongoDB驱动程序3.2.2。 下面是我的代码:

public <classname>()
{
    public static final String COLLECTION_NAME = "logs";
    MongoClient mongoClient = new MongoClient("<server-adress>");
    MongoDatabase db = mongoClient.getDatabase("test");

    Document  data = new Document ();
    data.append(<whatever>);
    //...inserting more into document...
    db.getCollection(COLLECTION_NAME).insertOne(data); //collection should be created new

    mongoClient.close();
}
public()
{
公共静态最终字符串集合\u NAME=“logs”;
MongoClient MongoClient=新的MongoClient(“”);
MongoDatabase db=mongoClient.getDatabase(“测试”);
文档数据=新文档();
data.append();
//…在文档中插入更多内容。。。
db.getCollection(COLLECTION_NAME).insertOne(data);//应新建集合
mongoClient.close();
}
程序正在执行,但我在执行过程中收到以下错误(和信息):

Mär 16, 2016 3:50:06 PM com.mongodb.diagnostics.logging.JULLogger log
INFORMATION: Cluster created with settings {hosts=[<server-adress>:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Mär 16, 2016 3:50:06 PM com.mongodb.diagnostics.logging.JULLogger log
INFORMATION: No server chosen by WritableServerSelector from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{address=<server-adress>:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
Mär 16, 2016 3:50:07 PM com.mongodb.diagnostics.logging.JULLogger log
INFORMATION: Exception in monitor thread while connecting to server <server-adress>:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114)
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:128)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.net.ConnectException: Connection refused: connect
    (...) 
    at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50)
    at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
    ... 3 more

Error: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=<server-adress>:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]
Mär 1620163:50:06 PM com.mongodb.diagnostics.logging.JULLogger log
信息:使用以下设置创建的群集{hosts=[:27017],mode=SINGLE,requiredClusterType=UNKNOWN,serverSelectionTimeout='30000ms',maxWaitQueueSize=500}
Mär 1620163:50:06 PM com.mongodb.diagnostics.logging.JULLogger log
信息:WritableServerSelector没有从群集描述ClusterDescription{type=UNKNOWN,connectionMode=SINGLE,all=[ServerDescription{address=:27017,type=UNKNOWN,state=Connection}]中选择服务器。在超时之前等待30000毫秒
Mär 1620163:50:07 PM com.mongodb.diagnostics.logging.JULLogger log
信息:连接到服务器时监视器线程出现异常:27017
com.mongodb.MongoSocketOpenException:异常打开套接字
位于com.mongodb.connection.SocketStream.open(SocketStream.java:63)
位于com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114)
位于com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunName.run(DefaultServerMonitor.java:128)
位于java.lang.Thread.run(未知源)
原因:java.net.ConnectException:连接被拒绝:连接
(...) 
位于com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50)
位于com.mongodb.connection.SocketStream.open(SocketStream.java:58)
... 3个以上
错误:等待与WritableServerSelector匹配的服务器时,30000毫秒后超时。群集状态的客户端视图为{type=UNKNOWN,servers=[{address=:27017,type=UNKNOWN,state=CONNECTING,exception={com.mongodb.mongoscocketopenexception:exception opening socket},由{java.net.ConnectException:Connection拒绝:connect}引起]
文档也不会插入

当我用insertOne()-命令注释该行时,它消失了。所以我认为这不是“连接问题”,对吗

我有一些想法:

  • 这和异步有关吗?我很惊讶,我没有说过我不想同步执行它
  • 是否缺少任何权限(由于此
    WritableServerSelector
    -事件)
  • 它是否与“测试”-数据库有关
  • MongoDB的独立模式有问题吗?(我不想使用ReplicaSet。)
但是对于所有这些想法,我找不到真正的证据

(请改进问题标题,我没有更好的主意…

我自己解决了它

我还应该提到,我正在尝试从另一台机器连接到MongoDB实例。MongoDB预设不允许这样做

因此,我必须在/etc/mongod.conf文件中将
bindIp
后面的值从127.0.0.1更改为0.0.0.0


回答得好,节省了我的时间:)我可以在这里添加另一个指定bindIp的选项是命令行参数,如:
mongod--bind_ip=0.0.0
谢谢!