Java mongo 无法连接到 [localhost/127.0.0.1:27017]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19857067/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:45:54  来源:igfitidea点击:

mongo couldn't connect to [localhost/127.0.0.1:27017]

javamongodb

提问by asaf app

I'm getting the following error:

我收到以下错误:

??? 08, 2013 12:05:46 PM com.mongodb.DBTCPConnector initDirectConnection
WARNING: Exception executing isMaster command on localhost/127.0.0.1:27017
java.io.IOException: couldn't connect to [localhost/127.0.0.1:27017] 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.innerCall(DBTCPConnector.java:277)
    at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:257)
    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:310)
    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:295)
    at com.mongodb.DB.getCollectionNames(DB.java:412)
    at Main.Main.main(Main.java:26)

my code is simple (my first time with mongo):

我的代码很简单(我第一次使用 mongo):

        MongoClient Client = new MongoClient( "localhost" , 27017 );

        DB db = Client.getDB("qw");
        DBCollection coll[] = new DBCollection[4];
        Set<String> colls = db.getCollectionNames();
        for(String s: colls)
            System.out.println(s);

what is the problem?

问题是什么?

采纳答案by Konstantin Yovkov

Possibly you haven't started the Mongo server.

可能你还没有启动 Mongo 服务器。

Open a shell and type

打开外壳并键入

mongod

On the file system, you can start it from $MONGO_INSTALL_PATH/bin/mongod.

在文件系统上,您可以从$MONGO_INSTALL_PATH/bin/mongod.

Don't close the shell and then try to run your code again.

不要关闭外壳,然后再次尝试运行您的代码。

More info:

更多信息:

回答by Aditya

You haven't started your MongoDB Server.

您尚未启动 MongoDB 服务器。

First start your mongodb server and then run your code.

首先启动您的 mongodb 服务器,然后运行您的代码。

Or

或者

You can also create MongoDB service which will always be running in the background, so from next time onwards you don't have to start the MongoDB server.

您还可以创建将始终在后台运行的 MongoDB 服务,因此从下次开始您不必启动 MongoDB 服务器。

Here are the steps to create MongoDB service:

以下是创建 MongoDB 服务的步骤:

  1. Create a folder named ‘log' parallel to ‘data' folder, inside mongodb folder.
  2. Copy the ‘mongo.config' file parallel to log folder inside ‘mongodb' folder.

    Here is the content for your mongo.config file:

    stores data here

    dbpath = your_drive\mongodb-win32-x86_64-2.2.3\data\db

    all output goes here

    logpath = your_drive\mongodb-win32-x86_64-2.2.3\log\mongo.log

    log read and write operations

    diaglog = 3

  3. Create a file MongoServer.bat,

    Here is the content for MongoServer.bat

    your_drive\mongodb-win32-x86_64-2.2.3\bin\mongod.exe --config "your_drive\mongodb-win32-x86_64-2.2.3\mongo.config"

  4. On your command prompt, go to your mongodb\bin directory and then write this command, mongod --config your_Drive\mongodb-win32-x86_64-2.2.3\mongo.config –install

    This will create the service named as ‘Mongo DB'.

  5. To start the service, type on command prompt net start MongoDB.

  1. 在 mongodb 文件夹中创建一个名为 'log' 的文件夹,与 'data' 文件夹平行。
  2. 将 'mongo.config' 文件平行复制到 'mongodb' 文件夹内的 log 文件夹。

    以下是 mongo.config 文件的内容:

    在这里存储数据

    dbpath = your_drive\mongodb-win32-x86_64-2.2.3\data\db

    所有输出都在这里

    日志路径= your_drive\mongodb-win32-x86_64-2.2.3\log\mongo.log

    记录读写操作

    对话 = 3

  3. 创建文件 MongoServer.bat,

    这是 MongoServer.bat 的内容

    your_drive\mongodb-win32-x86_64-2.2.3\bin\mongod.exe --config " your_drive\mongodb-win32-x86_64-2.2.3\mongo.config"

  4. 在您的命令提示符下,转到您的 mongodb\bin 目录,然后编写此命令, mongod --config your_Drive\mongodb-win32-x86_64-2.2.3\mongo.config --install

    这将创建名为“Mongo DB”的服务。

  5. 要启动该服务,请在命令提示符 net start MongoDB 上键入。

Hence, your service is created. Now you can perform your task.

因此,您的服务已创建。现在您可以执行您的任务了。

For further information, you can visit website.

欲了解更多信息,您可以访问网站

回答by avian

Try restarting your mongo. I had the same problem, and restarting solved it for me.

尝试重新启动您的 mongo。我遇到了同样的问题,重新启动为我解决了它。

回答by Leo Dagum

Probably not your problem, but I've seen this Exception raised when mongodb is refusing the connection because it hit a limit on the number of open connections. Check your mongodb log file for a statement like:

可能不是你的问题,但我已经看到当 mongodb 拒绝连接时引发了这个异常,因为它达到了打开连接数的限制。检查您的 mongodb 日志文件中是否有如下语句:

"connection refused because too many open connections".

“连接被拒绝,因为打开的连接太多”。

This can mean you're leaving connections open, or you need to raise the limit on number of open files in the db server.

这可能意味着您将连接保持打开状态,或者您需要提高数据库服务器中打开文件数的限制。

回答by Mani

Seems to have the same problem when using the below code to the connection

使用下面的代码连接时似乎有同样的问题

MongoClient mongo = new MongoClient("localhost", 27017);

MongoClient mongo = new MongoClient("localhost", 27017);

But worked out when changed to

但是当改为

MongoClient mongo = new MongoClient("127.0.0.1", 27017);