Connect to remote mongodb server using java

I am trying to connect to a remote mongodb instance, but it continues to throw 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 anything else I need to do? What is your username / password when trying to access the database or change some permissions on the mongo side? Its just the usual installation of mongo on an ubuntu server, without adding configuration or permissions.

FURTHER INFORMATION: mongo 172.234.52.24:8888 does not work either, the exception says: connect failed. I can ping another host, and I know that mango works on it.

Any ideas? Thanks!

+5
source share
5 answers

I realized this ... you had great suggestions, but the problem was more fundamental.

In my mongo configuration file on the remote server, the bind_ip variable was set for the local ip. As soon as I commented on this, everything worked correctly.

Thank you all, though!

+7
source

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

Just update your credentials in the following Java code and it will work for you. "$ external" in the code below means that you are trying to mount a database that is located on a Linux machine in a remote location.

The below code works in a standalone Java program.

 String database = "TestDev"; String username = " user@test.COM "; 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(); } 
+1
source

Connect to a remote MongoDB database using a Java web application. Below code will definitely help you.

Before using the code below, add a properties file that has credentials, all other necessary data. Read this properties file in spring -config.xml. You can use the code below to read the properties 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; } 
+1
source

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 data (MongoVue or MongoExplorer). Also stick to port 27017.

Edit: I am using MongoDriver to connect Java.

0
source

In case the recommended answer did not work; try setting bindIp: 0.0.0.0 in the mongod.conf file, which is located in /etc/mongod.conf

0
source

Source: https://habr.com/ru/post/1483634/


All Articles