MongoDB 使用 Java + Eclipse 拒绝连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19346329/
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
MongoDB Refusing Connection using Java + Eclipse
提问by donsiuch
When I run the following simple code, I typically get a connection refused error... but 1 time out of 20 it will work randomly. Then continue working repeatedly for 2-3 minutes, then refuse connections again. 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).
当我运行以下简单代码时,我通常会收到连接被拒绝的错误……但是 20 次中有 1 次它会随机工作。然后继续重复工作 2-3 分钟,然后再次拒绝连接。我无法检测到模式。我查看了其他连接被拒绝的线程,但它们使用了不同的技术,这些技术可能会或可能不会使情况复杂化(不幸的是,并不是每个线程都得到解决)。
I am completely new to Mongo and am following this guide: 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.
我对 Mongo 完全陌生,正在遵循本指南:http: //docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/。我的目标是拥有稳定的连接,以便我可以试验/学习数据库。非常感谢您对此事的任何帮助!请记住,我对这项技术完全陌生,还不知道如何解决它。
I am using JDK 1.7.0_25 and Eclipse. I have added mongo-driver-2.11.3.jar to my project's build path already. The following is my simple code, directly from the example on the site I listed.
我正在使用 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;
}
}
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
>
采纳答案by attish
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). That means in the java code you have to change this line:
虽然您可以使用 mongo 命令进行连接,但 mongodb 服务器肯定会侦听端口 27017(而在没有参数的情况下,它会尝试连接到该端口)。这意味着在 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.
而且我不确定如果没有服务器侦听主机,则拒绝返回连接的驱动程序的这种行为:端口配置是好的。至少从我的角度来看有点误导。
回答by Vivek Sethi
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. Earlier I wasn't using a --dbpath..
我通过给 mongod 一个 --dbpath 并将我的整个数据库迁移到那里解决了这个错误..现在每次我启动服务器(mongod)时,我都会给--dbpath。早些时候我没有使用 --dbpath ..
回答by yoga
Got the same error when mongodb was newly installed on our cluster. Code worked when i run the java program on the server mongodb was installed with localhost.
在我们的集群上新安装 mongodb 时遇到了同样的错误。当我在 mongodb 安装了localhost的服务器上运行 java 程序时,代码有效。
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. We corrected and restarted the service and it worked perfect !!
安装 mongodb 的端口仅限于 localhost。我们更正并重新启动了服务,它运行完美!!