MongoDB连接问题

时间:2016-01-18 05:50:29

标签: java linux mongodb

我尝试使用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命令查看/插入数据。
  4. 但是,在上面的java代码中,我没有包含任何登录凭据。我必须这样做吗?如果是这样,有人可以指导我。

1 个答案:

答案 0 :(得分:1)

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

MongoDB的默认端口是27017

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

bind_ip = 127.0.0.1

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

现在要解决此问题,请更改bind_ip的值以包含您的公共IP地址,因此,如果您的公开地址为46.12.212.34,则bind_ip将如下所示:

bind_ip = 127.0.0.1, 46.12.212.34

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