gpt4 book ai didi

java - MongoDB 连接问题

转载 作者:太空宇宙 更新时间:2023-11-04 10:41:43 25 4
gpt4 key购买 nike

我正在尝试使用 java 代码连接到在远程 Linux 机器上运行的 mongodb 服务器,

public static void main(String[] args) {
Properties prop = new Properties();
try {
//load a properties file
prop.load(new FileInputStream("src/main/resources/mongodb.properties"));
String server = prop.getProperty("server");
String port = prop.getProperty("port");
int portnum = Integer.parseInt(port);

/**** Connect to MongoDB ****/
MongoClient mongo = new MongoClient(server,portnum);
System.out.println("Connected to database");

/**** Get database ****/
// if database doesn't exists, MongoDB will create it for you
DB db = mongo.getDB("test");

/**** Get collection / table from 'details' ****/
// if collection doesn't exists, MongoDB will create it for you
DBCollection table = db.getCollection("details");

/**** Insert ****/
// create a document to store key and value
BasicDBObject document = new BasicDBObject();
document.put("name", "test");
document.put("age", 24);
table.insert(document);

/**** Find and display ****/
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("name", "test");

DBCursor cursor = table.find(searchQuery);

while (cursor.hasNext()) {
System.out.println(cursor.next());
}


} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

我的本地 mongo 运行良好。但是在尝试连接 Linux 服务器时遇到问题。

这是错误信息,

Jan 18, 2016 12:23:02 AM com.mongodb.DBTCPConnector initDirectConnection
WARNING: Exception executing isMaster command on server:port
java.io.IOException: couldn't connect to [server port] bc:java.net.ConnectException: Connection refused: connect
at com.mongodb.DBPort._open(DBPort.java:214)
at com.mongodb.DBPort.go(DBPort.java:107)
at com.mongodb.DBPort.go(DBPort.java:88)
at com.mongodb.DBPort.findOne(DBPort.java:143)
at com.mongodb.DBPort.runCommand(DBPort.java:148)
at com.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:548)
at com.mongodb.Mongo.getMaxBsonObjectSize(Mongo.java:620)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:254)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:226)
at com.mongodb.DBCollection.insert(DBCollection.java:75)
at com.mongodb.DBCollection.insert(DBCollection.java:59)
at com.mongodb.DBCollection.insert(DBCollection.java:104)
at com.filecompare.util.MongoDBConnection.main(MongoDBConnection.java:42)

Jan 18, 2016 12:23:03 AM com.mongodb.DBTCPConnector initDirectConnection
WARNING: Exception executing isMaster command on sever port
java.io.IOException: couldn't connect to [server port] bc:java.net.ConnectException: Connection refused: connect
at com.mongodb.DBPort._open(DBPort.java:214)
at com.mongodb.DBPort.go(DBPort.java:107)
at com.mongodb.DBPort.go(DBPort.java:88)
at com.mongodb.DBPort.findOne(DBPort.java:143)
at com.mongodb.DBPort.runCommand(DBPort.java:148)
at com.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:548)
at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:527)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:174)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:155)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:270)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:226)
at com.mongodb.DBCollection.insert(DBCollection.java:75)
at com.mongodb.DBCollection.insert(DBCollection.java:59)
at com.mongodb.DBCollection.insert(DBCollection.java:104)
at com.filecompare.util.MongoDBConnection.main(MongoDBConnection.java:42)

Jan 18, 2016 12:23:04 AM com.mongodb.DBPortPool gotError
WARNING: emptying DBPortPool to server:port b/c of error
java.io.IOException: couldn't connect to [server:port] bc:java.net.ConnectException: Connection refused: connect
at com.mongodb.DBPort._open(DBPort.java:214)
at com.mongodb.DBPort.go(DBPort.java:107)
at com.mongodb.DBPort.go(DBPort.java:84)
at com.mongodb.DBPort.say(DBPort.java:79)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:181)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:155)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:270)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:226)
at com.mongodb.DBCollection.insert(DBCollection.java:75)
at com.mongodb.DBCollection.insert(DBCollection.java:59)
at com.mongodb.DBCollection.insert(DBCollection.java:104)
at com.filecompare.util.MongoDBConnection.main(MongoDBConnection.java:42)

Exception in thread "main" com.mongodb.MongoException$Network: can't say something
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:194)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:155)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:270)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:226)
at com.mongodb.DBCollection.insert(DBCollection.java:75)
at com.mongodb.DBCollection.insert(DBCollection.java:59)
at com.mongodb.DBCollection.insert(DBCollection.java:104)
at com.filecompare.util.MongoDBConnection.main(MongoDBConnection.java:42)
Caused by: java.io.IOException: couldn't connect to [server port] bc:java.net.ConnectException: Connection refused: connect
at com.mongodb.DBPort._open(DBPort.java:214)
at com.mongodb.DBPort.go(DBPort.java:107)
at com.mongodb.DBPort.go(DBPort.java:84)
at com.mongodb.DBPort.say(DBPort.java:79)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:181)
... 7 more

我在 linux 上连接 mongo 通常遵循的步骤是,

  1. 使用 Putty(windows) 或 ssh 命令(Mac) 登录服务器。
  2. 输入 mongo。
  3. 连接到 mongo,运行 mongo 命令以查看/插入数据。

但是,在上面的 java 代码中,我没有包含任何登录凭据。我必须这样做吗?如果是这样,请有人指导我。

最佳答案

您确定在 mongodb.properties 中为您的 serverport 属性变量提供了正确的值吗?

MongoDB 的默认端口是 27017

此外,检查 MongoDB 服务器上的 mongod.conf。此文件默认位于 /etc/mongod.conf 并查找以下属性:

bind_ip = 127.0.0.1

如果设置为 127.0.0.1localhost,那么您将无法从外部公开访问您的 MongoDB 服务器。

现在要解决这个问题,请更改 bind_ip 的值以包含您的公共(public) IP 地址,因此如果您的公共(public)地址是 46.12.212.34bind_ip 看起来像:

bind_ip = 127.0.0.1, 46.12.212.34

保存并退出文件并重新启动您的 MongoDB 数据库。您的 Java 应用程序现在应该可以运行了。

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

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