As a newbie to MongoDB I'm trying to insert a simple document into my freshly installed mongoDB (v3.2.4). MongoDB Driver 3.2.2 is used. Here my minimized code:
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();
}
The programm is executing, but I'm getting the following errors (and informations) during execution:
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}}]
The document is also not inserted.
When I'm commenting the line with the insertOne()-command, it disappears. So I assume it isnt' a problem with the connection, is that right?
I've some ideas:
WritableServerSelector
-Thing)?But for all these ideas I couldn't find real proofs...
(Please improve the question-title, I had no better idea...)
Solved it on my own.
I also should have mentioned, that I'm trying to connect from another machine to the MongoDB-instance. That is not allowed with the MongoDB-presets.
So I had to change in the /etc/mongod.conf File the value behind bindIp
from 127.0.0.1 to 0.0.0.0.
[Source]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With