当我运行以下简单代码时,我通常会收到连接拒绝错误...但是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
>
答案 0 :(得分:3)
虽然你可以使用mongo命令连接,但确定mongodb服务器监听端口27017(没有参数,它尝试连接那里)。这意味着在java代码中你必须改变这一行:
MongoClient mongoClient = new MongoClient( "localhost" , 27107 );
到这一行:
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
如果没有服务器在主机上侦听,我不确定拒绝连接的驱动程序的这种行为:端口配置是好的。至少从我的角度来看有点误导。
答案 1 :(得分:1)
在我们的群集上新安装mongodb时出现同样的错误。 当我在服务器上运行java程序时,代码工作mongodb安装了 localhost 。
要在群集外运行代码,请拒绝连接错误。
问题是:
安装mongodb的端口仅限于localhost。 我们纠正并重新启动了服务,它完美无缺!!
答案 2 :(得分:0)
我通过向mongod提供--dbpath并将我的整个数据库迁移到那里来解决了这个错误。 现在每次启动服务器(mongod),我都会给--dbpath。 早些时候我没有使用--dbpath ..