Connect to remote mongodb server with java
Asked Answered
M

5

5

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!

Morula answered 30/5, 2013 at 17:21 Comment(2)
Is the port open? Why did you try to connect to 172.234.52.24:8888? 8888 isn't the default port. docs.mongodb.org/manual/tutorial/…Plat
Does "mongo 172.234.52.24" work from your side? Does "mongo localhost" work if you ssh into the server?Mcclelland
M
7

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!

Morula answered 30/5, 2013 at 21:30 Comment(3)
Hey exxodus7! can you please share the details on how you solved the problem?Fork
@Fork open mongod configuration file, at /etc/mongod.conf, then comment out the bind_ip=127.0.0.1 linePillory
thanks @slonage its really working just need to restart the mongod service after edit the file.Aluminous
P
1

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();
    }
Pretender answered 13/11, 2015 at 6:1 Comment(0)
P
1

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;
}
Pretender answered 13/11, 2015 at 6:17 Comment(0)
P
0

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.

Porous answered 30/5, 2013 at 18:31 Comment(0)
P
0

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.

Paget answered 5/6, 2019 at 5:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.