gpt4 book ai didi

java - MongoDB 和 Java 连接错误

转载 作者:行者123 更新时间:2023-11-29 09:31:14 25 4
gpt4 key购买 nike

我正在尝试在运行 mongod 的计算机之外的另一台计算机上使用 java/mongo 驱动程序运行 java 程序。我只修改了 java/mongo 教程代码以包含一个 ip 地址。

package mongotest;
import com.mongodb.*;

public class Main {
static DBCursor cur;
static DBCollection coll;
public static void main(String[] args) {
Mongo m;
try{
m = new Mongo("192.168.0.102"); // <---- This does not connect. It will eventually time out
DB db = m.getDB("playerdb");
coll = db.getCollection("players");

cur = coll.find();
//while (cur.hasNext())
// coll.remove(cur.next());

coll.ensureIndex(new BasicDBObject("playerID", 1).append("unique", true));

boolean unique = true;

cur = coll.find();
printResults(cur, "Find All Records");


boolean canCreate;
canCreate = createAccount("Josh", "1", cur, coll);
canCreate = createAccount("Jason", "1", cur, coll);
canCreate = createAccount("Ryan", "1", cur, coll);
canCreate = createAccount("Michael", "1", cur, coll);
canCreate = createAccount("John", "1", cur, coll);
canCreate = createAccount("Susan", "1", cur, coll);


cur = coll.find();
printResults(cur, "Find All Records After Insert");



}//try
catch(Exception e){
System.out.println(e);
}//catch
}

(注意:这最终会超时退出)

但是当我在运行数据库的计算机上运行相同的代码时,一切正常。

如何在不同网络上的两台计算机之间建立连接以进行通信?

最佳答案

首先需要保证网络路由:

你能从计算机 a ping 计算机 b 吗?

你能从第二台电脑远程登录到第一台电脑的mongo端口吗?

如果不是,则您遇到的是网络问题而不是编程问题。在这种情况下,您可能需要在 serverfault 或 superuser 上问这个问题

关于java - MongoDB 和 Java 连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5036312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com