Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to remote mongodb server with java

I'm trying to connect to a remote mongodb instance, but it keeps throwing an error.

Java code:

Mongo mongo = new Mongo("172.234.52.24");
DB db = mongo.getDB("myDB");
collection = db.getCollection("myCollection");

But I keep getting the following exception:

java.io.IOException: couldn't connect to [/172.234.52.24:27017] bc:java.net.ConnectException: Connection refused

Is there something else I have to do? Set username/password when I try to access the database or change some permissions on the mongo side? Its just the normal mongo install on a ubuntu server, no added configuration or permissions.

ADDITIONAL INFORMATION: mongo 172.234.52.24:8888 doesn't work either, says exception: connect failed. I can ping the other host, and know mongo is running on it.

Any ideas? Thanks!

like image 724
exxodus7 Avatar asked May 30 '13 17:05

exxodus7


People also ask

Can Java connect to MongoDB?

Before you start using MongoDB in your Java programs, you need to make sure that you have MongoDB CLIENT and Java set up on the machine. You can check Java tutorial for Java installation on your machine. Now, let us check how to set up MongoDB CLIENT. You need to download the jar mongodb-driver-3.11.


2 Answers

I figured it out... y'all had great suggestions but the problem was more fundamental.

In my mongo configuration file on the remote server, there was a bind_ip variable set to the local ip. Once I commented this out, everything worked properly.

Thank you all very much though!

like image 177
exxodus7 Avatar answered Sep 29 '22 06:09

exxodus7


Make sure you have added correct maven dependency in your pom.xml 1. spring-data-mongodb (1.5.2.RELEASE) 2. mongo-java-driver (2.13.0)

Just update your credential in following java code and it will work for you. "$external" in the below code represents that you are trying to connect database which is on Linux machine at remote location.

Below code is working in standalone Java program.

String database = "TestDev";
    String username = "[email protected]";
    String pass = "XXXXX";
    char[] password = pass.toCharArray();

    try {

        List<ServerAddress> serverAddresses = new ArrayList<ServerAddress>();
        ServerAddress address = new ServerAddress("hostname", portnumber);
        serverAddresses.add(address);
        List<MongoCredential> credentials = new ArrayList<MongoCredential>();
        MongoCredential credential = MongoCredential.createPlainCredential(username, "$external", password);
        credentials.add(credential);
        MongoClient mongoClient1 = new MongoClient(serverAddresses, credentials);
        DB db = mongoClient1.getDB(database);
        System.out.println(db.getCollectionNames());


        System.out.println("Done");
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
like image 35
Krishna Kumar Chourasiya Avatar answered Sep 29 '22 07:09

Krishna Kumar Chourasiya