2

当我运行以下简单代码时,我通常会收到连接被拒绝错误...但是 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 添加到我的项目的构建路径中。以下是我的简单代码,直接来自我列出的网站上的示例。

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
> 
4

3 回答 3

3

虽然您可以使用 mongo 命令进行连接,但可以确定 mongodb 服务器在端口 27017 上侦听(虽然没有参数,但它会尝试在那里连接)。这意味着您必须在 java 代码中更改这一行:

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

到这一行:

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

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

于 2013-10-13T15:41:04.857 回答
1

在我们的集群上新安装 mongodb 时出现同样的错误。当我在安装了localhost的服务器上运行 java 程序时,代码有效。

要在集群外运行代码,出现连接被拒绝错误。

问题是:

安装 mongodb 的端口仅限于 localhost。我们更正并重新启动了服务,它运行完美!

于 2015-09-01T15:55:18.570 回答
0

我通过向 mongod 提供 --dbpath 并将我的整个数据库迁移到那里解决了这个错误。现在每次启动服务器(mongod)时,我都会提供 --dbpath。早些时候我没有使用--dbpath ..

于 2014-05-29T11:37:37.583 回答