当前位置: 动力学知识库 > 问答 > 编程问答 >

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

问题描述:

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):

 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?

网友答案:

Possibly you haven't started the Mongo server.

Open a shell and type

mongod

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

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

More info:

  • Manage the mongod process
网友答案:

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

网友答案:

You haven't started your MongoDB Server.

First start your mongodb server and then run your code.

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.

Here are the steps to create MongoDB service:

  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.

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

For further information, you can visit website.

网友答案:

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:

"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.

网友答案:

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

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

But worked out when changed to

MongoClient mongo = new MongoClient("127.0.0.1", 27017);
分享给朋友:
您可能感兴趣的文章:
随机阅读: