简体   繁体   English

使用Java + Eclipse的MongoDB拒绝连接

[英]MongoDB Refusing Connection using Java + Eclipse

When I run the following simple code, I typically get a connection refused error... but 1 time out of 20 it will work randomly. 当我运行以下简单代码时,通常会出现连接被拒绝的错误……但是20次中有1次会随机工作。 Then continue working repeatedly for 2-3 minutes, then refuse connections again. 然后继续重复工作2-3分钟,然后再次拒绝连接。 I can't detect a pattern. 我无法检测到模式。 I have looked at the other connection refused threads but they are using differing technologies that may or may not be complicating the situation (and unfortunately not every thread is even resolved). 我看过其他拒绝连接的线程,但是它们使用的是不同的技术,可能会使情况复杂化,也可能不会使情况复杂化(不幸的是,并不是每个线程都得到解决)。

I am completely new to Mongo and am following this guide: http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/ . 我对Mongo完全陌生,并且正在遵循此指南: http : //docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/ My goal is to have a stable connection so I can experiment/learn the DB. 我的目标是建立稳定的连接,以便我可以试验/学习数据库。 Any help with this matter is greatly appreciated! 非常感谢对此事的任何帮助! Keep in mind I'm completely new to this technology and don't know my way around it yet. 请记住,我对这项技术是完全陌生的,还不知道如何解决。

I am using JDK 1.7.0_25 and Eclipse. 我正在使用JDK 1.7.0_25和Eclipse。 I have added mongo-driver-2.11.3.jar to my project's build path already. 我已经在项目的构建路径中添加了mongo-driver-2.11.3.jar。 The following is my simple code, directly from the example on the site I listed. 以下是我的简单代码,直接来自我列出的网站上的示例。

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;
    }
}

And the error I get: 我得到的错误是:

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

When I type mongo in bash, the display I get is: 当我在bash中键入mongo时,显示的内容为:

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

while you can connect with the mongo command it is definite that the mongodb server listen on the port 27017 (while without parameters it tries to connect there). 虽然可以使用mongo命令进行连接,但可以肯定的是mongodb服务器在端口27017上进行侦听(而没有参数时,它将尝试在该端口上进行连接)。 That means in the java code you have to change this line: 这意味着在Java代码中,您必须更改以下行:

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

To this line: 到这行:

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

And i am not sure this behavior of the driver that give back connection refused if there is no server listening on a host:port config is something which is good. 而且我不确定如果没有服务器在主机上侦听,则驱动程序返回连接的行为会被拒绝:端口配置是否良好。 At least a bit misleading from my point of view. 从我的角度来看,至少有些误导。

Got the same error when mongodb was newly installed on our cluster. 当mongodb新安装在我们的集群上时,出现了相同的错误。 Code worked when i run the java program on the server mongodb was installed with localhost . 当我在服务器上运行Java程序的代码工作正常时,mongodb已安装localhost

To run the code outside the cluster , got the connection refused error. 要在集群外部运行代码,出现连接拒绝错误。

The issue was : 问题是:

Port on which mongodb was installed was limited to localhost. 安装mongodb的端口仅限于localhost。 We corrected and restarted the service and it worked perfect !! 我们更正并重新启动了该服务,它正常运行!

I solved this error by giving a --dbpath to mongod and migrating my entire DB there.. Now everytime I start the server(mongod), I give --dbpath. 我通过给mongod提供--dbpath并将整个数据库迁移到那里来解决了该错误。现在,每次启动服务器(mongod)时,我都提供--dbpath。 Earlier I wasn't using a --dbpath.. 之前我没有使用--dbpath。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM