gpt4 book ai didi

java - MongoDB 拒绝使用 Java + Eclipse 连接

转载 作者:可可西里 更新时间:2023-11-01 10:01:02 30 4
gpt4 key购买 nike

当我运行以下简单代码时,我通常会收到连接被拒绝的错误...但 20 次中有 1 次它会随机工作。然后继续重复工作2-3分钟,然后再次拒绝连接。我无法检测到模式。我查看了其他连接被拒绝的线程,但它们使用的不同技术可能会使情况复杂化,也可能不会(不幸的是,并非每个线程都得到解决)。

我是 Mongo 的新手,正在遵循本指南:http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/ .我的目标是建立稳定的连接,以便我可以试验/学习数据库。非常感谢您对此事的任何帮助!请记住,我对这项技术完全陌生,还不知道如何使用它。

我正在使用 JDK 1.7.0_25 和 Eclipse。我已经将 mongo-driver-2.11.3.jar 添加到我的项目的构建路径中。以下是我的简单代码,直接来 self 列出的网站上的示例。

package database;

import java.util.Set;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.ServerAddress;

public class MongoPortal {

/*static final String domain = "localhost";
static final int port = 27107;
static final String database = "test";*/

public boolean insert(){

try {
MongoClient mongoClient = new MongoClient( "localhost" , 27107 );

DB db = mongoClient.getDB("test");

// Get and print all the collections
Set<String> colls = db.getCollectionNames();
for (String s : colls)
System.out.println(s);

mongoClient.close();
}
catch (Exception e){
e.printStackTrace();
return false;
}

return true;
}
}

我得到的错误:

Oct 13, 2013 9:12:50 AM com.mongodb.DBTCPConnector initDirectConnection
WARNING: Exception executing isMaster command on localhost/127.0.0.1:27107
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at com.mongodb.DBPort._open(DBPort.java:223)
at com.mongodb.DBPort.go(DBPort.java:125)
at com.mongodb.DBPort.go(DBPort.java:106)
at com.mongodb.DBPort.findOne(DBPort.java:162)
at com.mongodb.DBPort.runCommand(DBPort.java:170)
at com.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:547)
at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:526)
at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:236)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
at com.mongodb.DB.getCollectionNames(DB.java:400)
at database.MongoPortal.insert(MongoPortal.java:29)
at driver.Driver.main(Driver.java:22)

Oct 13, 2013 9:12:50 AM com.mongodb.DBPortPool gotError
WARNING: emptying DBPortPool to localhost/127.0.0.1:27107 b/c of error
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at com.mongodb.DBPort._open(DBPort.java:223)
at com.mongodb.DBPort.go(DBPort.java:125)
at com.mongodb.DBPort.call(DBPort.java:92)
at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:244)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
at com.mongodb.DB.getCollectionNames(DB.java:400)
at database.MongoPortal.insert(MongoPortal.java:29)
at driver.Driver.main(Driver.java:22)

com.mongodb.MongoException$Network: Read operation to server localhost/127.0.0.1:27107 failed on database test
at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:253)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
at com.mongodb.DB.getCollectionNames(DB.java:400)
at database.MongoPortal.insert(MongoPortal.java:29)
at driver.Driver.main(Driver.java:22)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at com.mongodb.DBPort._open(DBPort.java:223)
at com.mongodb.DBPort.go(DBPort.java:125)
at com.mongodb.DBPort.call(DBPort.java:92)
at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:244)
... 6 more

当我在 bash 中输入 mongo 时,我得到的显示是:

@debian:~$ mongo
MongoDB shell version: 2.4.6
connecting to: test
>

最佳答案

虽然您可以使用 mongo 命令连接,但可以肯定的是,mongodb 服务器在端口 27017 上监听(虽然没有参数,但它会尝试连接到那里)。这意味着在 Java 代码中你必须改变这一行:

MongoClient mongoClient = new MongoClient( "localhost" , 27107 );

到这一行:

MongoClient mongoClient = new MongoClient( "localhost" , 27017 );

而且我不确定如果没有服务器监听主机:端口配置,驱动程序返回连接被拒绝的这种行为是好的。至少从我的角度来看有点误导。

关于java - MongoDB 拒绝使用 Java + Eclipse 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19346329/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com