这是我第一次尝试使用MongoDB,所以如果你们有任何提示,我会很喜欢任何提示,但特别是我无法建立连接...当jar加载时会发生此错误。
这是我的堆栈跟踪:
[00:12:43 INFO]: Exception in monitor thread while connecting to server 127.0.0.1:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[GangWars-1.0-SNAPSHOT-shaded.jar:?]
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[GangWars-1.0-SNAPSHOT-shaded.jar:?]
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) [GangWars-1.0-SNAPSHOT-shaded.jar:?]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[?:1.8.0_91]
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[?:1.8.0_91]
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[?:1.8.0_91]
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[?:1.8.0_91]
at java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[?:1.8.0_91]
at java.net.PlainSocketImpl.connect(Unknown Source) ~[?:1.8.0_91]
at java.net.SocksSocketImpl.connect(Unknown Source) ~[?:1.8.0_91]
at java.net.Socket.connect(Unknown Source) ~[?:1.8.0_91]
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[GangWars-1.0-SNAPSHOT-shaded.jar:?]
at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[GangWars-1.0-SNAPSHOT-shaded.jar:?]
... 3 more
这是我的代码(当jar运行时立即发生):
private void loadGangs() {
MongoDB.getIDs().forEach(id -> GangUtils.addGang(new Gang(id)));
}
我的MongoDB类:
public final class MongoDB {
private static final String name = "data";
private static final MongoClient mongoClient = new MongoClient();
public static String getName() {
return name;
}
public static MongoDatabase getDatabase() {
return mongoClient.getDatabase(name);
}
public enum CollectionEnum {
GANG, DRUGS, COPS
}
public static MongoCollection<Document> getCollection(CollectionEnum collection) {
return getDatabase().getCollection(collection.name().toLowerCase());
}
public static List<Integer> getIDs() {
List<Document> gangs = getCollection(CollectionEnum.GANG).find().into(new ArrayList<>());
List<Integer> ids = new ArrayList<>();
for (Document gang : gangs) {
Object object = gang.get("_id");
if (!(object instanceof Document)) continue;
List<Document> idTags = (List<Document>) object;
for (Document id : idTags) {
ids.add(id.getInteger("_id"));
}
}
return ids;
}
public static int getNextID() {
int id = 0;
for (int i = 0; i >= getIDs().size(); i++) {
if (getIDs().get(i) != i) {
id = i;
break;
}
id++;
}
return id;
}
}
我做错了什么? (我试着在这里进行研究,但我找不到任何与Java相关的东西来帮助我使用MongoDB)
答案 0 :(得分:1)
您的MongoDB实例似乎未正确启动,
您可以通过直接运行命令mongo
进行诊断
或者,如果您使用的是Linux发行版,则可以使用nc -zv localhost 27017
。
如果您正在使用Windows,可以尝试运行telnet 127.0.0.1 27017
以查看端口是否正在打开,请注意默认情况下不会启用telent
命令,以启用您应该转到软件和服务中的控制面板,启用telnet客户端。作为开发人员,您应该始终启用它来诊断某些网络问题。
此外,还有一个ORM驱动程序我们发现它非常有用并且已经投入生产环境多年,它被包装了由官方供应商维护的默认MongoDB-java驱动程序 morphia
答案 1 :(得分:0)
我知道这篇文章是9个月,但这可以帮助其他人。
我遇到了同样的问题:问题是我遗憾地test
scope
为de.flapdoodle.embed.mongo
依赖关系,只是通过评论它是默认的(compile
)解决了问题
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<!-- <scope>test</scope> -->
</dependency>