exxodus7
exxodus7

Reputation: 562

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!

Upvotes: 5

Views: 14493

Answers (5)

NiharGht
NiharGht

Reputation: 161

In case the recommended answer didnt work; try setting the bindIp: 0.0.0.0 in mongod.conf file located in /etc/mongod.conf

Edit: Apparently, late post with no other answer solving my issue and one line answer is insufficient.

Upvotes: 0

Krishna Kumar Chourasiya
Krishna Kumar Chourasiya

Reputation: 2136

Connect to remote MongoDB database using Java web application. Below code surely help you.

Before to use below code please add property file having credentials all other required details in it. Read that property file in spring-config.xml. You can use below code to read the property file -

<context:property-placeholder location='classpath:/config/configTest.properties'/>

@Configuration public class MongoConfiguration extends AbstractMongoConfiguration{

@Value("${mongodb.dbname}")
private String  dbName;

@Value("${mongodb.host}")
private String  host;

@Value("${mongodb.port}")
private Integer port;

@Value("${mongodb.username}")
private String  userName;

@Value("${mongodb.password}")
private String  password;

@Value("${mongodb.authenticationdatabase}")
private String  authenticationDatabase;

@Override
protected String getDatabaseName()  {
    return this.dbName;
}

@Override
public MongoClient mongo() throws Exception {
    List<ServerAddress> serverAddresses = new ArrayList<ServerAddress>();
    ServerAddress address = new ServerAddress(host, port);
    serverAddresses.add(address);
    List<MongoCredential> credentials = new ArrayList<MongoCredential>();
    MongoCredential credential = MongoCredential.createPlainCredential(userName, authenticationDatabase, password.toCharArray());
    credentials.add(credential);
    return new MongoClient(serverAddresses, credentials);
}

@Override
@Bean
public SimpleMongoDbFactory mongoDbFactory() throws Exception {
    return new SimpleMongoDbFactory(mongo(), getDatabaseName());
}

@Override
@Bean
public MongoTemplate mongoTemplate() throws Exception {

    final MongoTemplate mongoTemplate = new MongoTemplate(mongo(), getDatabaseName());
    mongoTemplate.setWriteConcern(WriteConcern.SAFE);
    return mongoTemplate;
}

Upvotes: 1

Krishna Kumar Chourasiya
Krishna Kumar Chourasiya

Reputation: 2136

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();
    }

Upvotes: 1

Mike Gostintsev
Mike Gostintsev

Reputation: 88

The following works for me:

private static final String DB_NAME = "yourDbName";

MongoClient mongo = new MongoClient();
DB db = mongo.getDB(DB_NAME);
collection = db.getCollection("myCollection");

The db name is used by the driver; the connection string (172.234.52.24:27017) is used by the client when viewing the data (MongoVue or MongoExplorer). Also, stick to port 27017.

Edit: I'm using MongoDriver for java to connect.

Upvotes: 0

exxodus7
exxodus7

Reputation: 562

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!

Upvotes: 7

Related Questions