Java com.mongodb.MongoTimeoutException:等待连接时 10000 毫秒后超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38432880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting to connect
提问by Tonyukuk
I assumed this question was asked several times but I had to reask it again. Because solutions provided for this question did not give me an exact answer to get rid of this bloody error.
我以为这个问题被问了好几次,但我不得不再问一次。因为为这个问题提供的解决方案没有给我一个确切的答案来摆脱这个血腥的错误。
I use mongo-java-driver-2.12.4
and mongo.jar
when I try to insert document to db I get following error. Any help is appreciated.
我使用mongo-java-driver-2.12.4
,mongo.jar
当我尝试将文档插入数据库时,出现以下错误。任何帮助表示赞赏。
Error :
错误 :
Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting to connect. Client view of cluster state is {type=Unknown, servers=[{address=127.0.0.1:27000, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused: connect}}, {address=127.0.0.1:27001, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused: connect}}, {address=127.0.0.1:27002, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused: connect}}]
at com.mongodb.BaseCluster.getDescription(BaseCluster.java:128)
Code :
代码 :
public class MongoDbConnectDatabase {
public static void main(String[] args) {
// To connect to mongodb server
try {
List<ServerAddress> lstServer = new ArrayList<ServerAddress>();
lstServer.add(new ServerAddress("127.0.0.1", 27000));
lstServer.add(new ServerAddress("127.0.0.1", 27002));
lstServer.add(new ServerAddress("127.0.0.1", 27001));
MongoClient mongoClient = new MongoClient(lstServer);
// Now connect to your database
DB db = mongoClient.getDB("test");
System.out.println("connect to database successfully");
DBCollection coll = db.createCollection("mycol", null);
System.out.println("Collection created successfully");
DBCollection colReceived= db.getCollection("mycol");
System.out.println("Collection mycol selected successfully");
BasicDBObject doc = new BasicDBObject("title", "MongoDB").
append("description", "database").
append("likes", 100).
append("url", "http://www.tutorialspoint.com/mongodb/").
append("by", "tutorials point");
colReceived.insert(doc);
System.out.println("Document inserted successfully");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
采纳答案by chf
You obtain a Connection refused. Are you sure mongod is running?
您获得连接被拒绝。你确定 mongod 正在运行?
Try to connect with mongoclient:
尝试与 mongoclient 连接:
mongo 127.0.0.1:27000/test
蒙戈 127.0.0.1:27000/测试
and this for all the three instances (27000, 27002, 27001).
这适用于所有三个实例(27000、27002、27001)。
If you have problem also with mongoclient, check your logs.
如果您对 mongoclient 也有问题,请检查您的日志。
回答by Jokeur
another reason for this error can be that the version of mongo-java-driver is not compatible with your mongo application. My case : I was using mongo-java-driver version 2.12.3 with mongo 3.0.8 -> doesn't work. (https://docs.mongodb.com/ecosystem/drivers/driver-compatibility-reference/#reference-compatibility-mongodb-java)
此错误的另一个原因可能是 mongo-java-driver 的版本与您的 mongo 应用程序不兼容。我的情况:我使用 mongo-java-driver 版本 2.12.3 和 mongo 3.0.8 -> 不起作用。(https://docs.mongodb.com/ecosystem/drivers/driver-compatibility-reference/#reference-compatibility-mongodb-java)
回答by Ajeet Khan
Hereis all the possible reason for this error are listed. In my case it was due to replicaset not initialised. Initialise replicaset using rs.initiate()
.
In my case I used the volume created from production data and used it in staging. Since the local
db was having old replicaset config, it was not able to become PRIMARY. I did the following thing to make it PRIMARY:
这里列出了此错误的所有可能原因。在我的情况下,这是由于副本集未初始化。使用rs.initiate()
. 就我而言,我使用了从生产数据创建的卷并将其用于暂存。由于local
数据库具有旧的副本集配置,因此无法成为 PRIMARY。我做了以下事情使其成为主要:
>use local
> db.dropDatabase();
{ "dropped" : "local", "ok" : 1 }
> rs.initiate()
>myrepl:PRMIARY
Now the client was able to connect and perform read/write operations.
现在客户端能够连接并执行读/写操作。