简体   繁体   中英

Connecting SpringBoot Java app to external mongodb

I'm having a lot of trouble trying to connect my basic local maven springboot java app to an external mongodb.

Even with just trying to use mongo console itself it took a long time to figure out the syntax. Example below i managed to finally connect with (I run via a batch script)

...\mongo.exe "mongodb://serv1.unix.abc:27018,serv2.unix.abc:27018,ser3.unix.abc:27018/test?replicaSet=asdfg01h" --authenticationMechanism=GSSAPI --authenticationDatabase=$external --username="user@THATDOMAIN.COM" --password="password" -ssl --sslCAFile=C:\mongo\ca.pem

(Using mongo v 4.2.8 enterprise)

On java side (using jdk11, maven 3.6.1, springboot 2.4.0) it was easy enough to connect to a local mongo instance. I just needed to add a basic uri in my application-local.yml:

spring.data.mongodb.uri: mongodb://localhost:27017/localtest

And just add below code:

@Autowired
private MongoClient mongoClient;
..
..
MongoDatabase myDatabase = mongoClient.getDatabase("localtest");

But I dont really have any idea how to connect to the same external database as that mongo console script I wrote above. Using the uri only doesnt seem to work. I think it is because of the complexity given there are a lot of variables to account for.

  • replicaset (3 hosts)
  • GSSAPI authentication mechanism
  • $external authenticationdatabase
  • provide both username and password (GSSAPI example codes I've seen dont mention password anywhere for some reason)
  • enable SSL
  • and point to a ssl CA pem file

I've seen various examples that make use of MongoClientOptions MongoCredential MongoClientSettings

But I keep struggling to get it to work, especially when some articles use deprecated features. Any help?

I'm having a lot of trouble trying to connect my basic local maven springboot java app to an external mongodb.

Even with just trying to use mongo console itself it took a long time to figure out the syntax. Example below i managed to finally connect with (I run via a batch script)

...\mongo.exe "mongodb://serv1.unix.abc:27018,serv2.unix.abc:27018,ser3.unix.abc:27018/test?replicaSet=asdfg01h" --authenticationMechanism=GSSAPI --authenticationDatabase=$external --username="user@THATDOMAIN.COM" --password="password" -ssl --sslCAFile=C:\mongo\ca.pem

(Using mongo v 4.2.8 enterprise)

On java side (using jdk11, maven 3.6.1, springboot 2.4.0) it was easy enough to connect to a local mongo instance. I just needed to add a basic uri in my application-local.yml:

spring.data.mongodb.uri: mongodb://localhost:27017/localtest

And just add below code:

@Autowired
private MongoClient mongoClient;
..
..
MongoDatabase myDatabase = mongoClient.getDatabase("localtest");

But I dont really have any idea how to connect to the same external database as that mongo console script I wrote above. Using the uri only doesnt seem to work. I think it is because of the complexity given there are a lot of variables to account for.

  • replicaset (3 hosts)
  • GSSAPI authentication mechanism
  • $external authenticationdatabase
  • provide both username and password (GSSAPI example codes I've seen dont mention password anywhere for some reason)
  • enable SSL
  • and point to a ssl CA pem file

I've seen various examples that make use of MongoClientOptions MongoCredential MongoClientSettings

But I keep struggling to get it to work, especially when some articles use deprecated features. Any help?

In case you are using spring data, this is how I connect:

spring.data.mongodb.database=database_name_here spring.data.mongodb.uri=mongodb://CN=some.name.com@some.domain.com:55017/?authMechanism=MONGODB-X509&tls=true&authSource=$external

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM