I am currently developing a Java application connected to a remote MongoDB databse.
I have implemented the authentication methods fallowing the mongo guides:
MongoCredential credential = MongoCredential.createScramSha1Credential(username, credentialDatabase, password.toCharArray());
MongoClient client = new MongoClient(new ServerAddress(hostname, port), Arrays.asList(credential));
mongoDatabase = client.getDatabase(database);
The app connect properly to the database but there is a thing i can't understand.It connects well to the remote server,but I don't know why it try to connect to localhost:27017.
2016-03-07 16:13:29.662 INFO 12507 --- [*.*.*:25015] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:29}] to *.*.*.*:25015
2016-03-07 16:13:29.687 INFO 12507 --- [*.*.*:25015] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=*.*.*.*:25015, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 3]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, roundTripTimeNanos=24485426}
2016-03-07 16:13:30.062 INFO 12507 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2016-03-07 16:13:30.220 INFO 12507 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
So, how can I tell it I don't want to connect to localhost ?
Thanks
If you have created a user and are having trouble authenticating, try the following: Check that you are using the correct username and password for your database user, and that you are connecting to the correct database deployment. Check that you are specifying the correct authSource database in your connection string.
To connect your application with your local MongoDB database, you need to connect MongoDB on localhost 27017 port. This is the default port the MongoDB database server listens to the commands. Working on a local database is part of the development of any web, desktop, or mobile application.
To connect to your local MongoDB, you set Hostname to localhost and Port to 27017 . These values are the default for all local MongoDB connections (unless you changed them). Press connect, and you should see the databases in your local MongoDB.
The localhost exception allows you to enable access control and then create the first user or role in the system. After you enable access control, connect to the localhost interface and create the first user in the admin database. If you create a user first, the user must have privileges to create other users.
You can exclude Mongo auto connect/(localhost:27017) by adding below annotation on spring boot Application.java
.
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class})
public class Application {
// ...
}
I'm not sure if this will help.
If you are using SpringBoot 1.4 and you will not have a bean for MongoClient in the context auto configuration will create MongoClient using default configuration.
@Configuration
---->@ConditionalOnClass(MongoClient.class)<----
@EnableConfigurationProperties(MongoProperties.class)
@ConditionalOnMissingBean(type = "org.springframework.data.mongodb.MongoDbFactory")
public class MongoAutoConfiguration {
...
@Bean
---->@ConditionalOnMissingBean<----
public MongoClient mongo() throws UnknownHostException {
this.mongo = this.properties.createMongoClient(this.options, this.environment);
return this.mongo;
}
...
So you have 3 options:
spring.data.mongodb.host=
spring.data.mongodb.port=
spring.data.mongodb.database=
spring.data.mongodb.username=
spring.data.mongodb.password=
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With