How to create a CUPS service for mongoDB?

独自空忆成欢 提交于 2019-12-12 04:31:38

问题


I am currently using a local database. Using a terminal to access mongodb.

These are all the credentials I have. In my application properties when I provide the following data I am able to retrieve and store data.

spring.data.mongodb.database=myNewDB
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017

I am trying to use cloud foundry CUPS to connect mongodb with URI. But following the URI guide line I am missing username and password. Where can I find those values? All my data is currently stored locally and do not wish to use the MongoDB that is provided by Cloud Foundry because I would have to manually enter those data again.

DB-TYPE://USERNAME:PASSWORD@HOSTNAME:PORT/NAME
mongo://don't:know@localhost:27017/myNewDB

Attempt

In mongo shell I did the following to create an admin.

use admin
db.createUser(
   {
     user: "appAdmin",
     pwd: "password",
     roles:
       [
         { role: "readWrite", db: "myNewDB" },
         "clusterAdmin"
       ]
   }
)

After this I added the following into my application.properties

spring.data.mongodb.username=appAdmin
spring.data.mongodb.password=password

When I run my application I receive the following error:

com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='appAdmin', source='myNewDB', password=<hidden>, mechanismProperties={}}
    at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:61) ~[mongo-java-driver-3.2.2.jar:na]
    at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32) ~[mongo-java-driver-3.2.2.jar:na]
    at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:99) ~[mongo-java-driver-3.2.2.jar:na]
    at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:44) ~[mongo-java-driver-3.2.2.jar:na]
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongo-java-driver-3.2.2.jar:na]
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:128) ~[mongo-java-driver-3.2.2.jar:na]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_121]
Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server localhost:27017. The full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication failed." }
    at com.mongodb.connection.CommandHelper.createCommandFailureException(CommandHelper.java:170) ~[mongo-java-driver-3.2.2.jar:na]
    at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:123) ~[mongo-java-driver-3.2.2.jar:na]
    at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32) ~[mongo-java-driver-3.2.2.jar:na]
    at com.mongodb.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:95) ~[mongo-java-driver-3.2.2.jar:na]
    at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:45) ~[mongo-java-driver-3.2.2.jar:na]
    ... 6 common frames omitted

but when I remove username and password from my application.properties file. My application would run as intended.


回答1:


Username and password in your context means the username password for MongoDB. MongoDB authentication is optional. You can start mongod without authentication. but this is not recommended in Production systems. You can enable authentication for mongodb and create users in it to access. https://docs.mongodb.com/manual/tutorial/enable-authentication

You can create and bind CUPS service with your mongodb params to your application. Then you can read this service in an environment variable VCAP_SERVICES. System.getEnv("VCAP_SERVICES"); You can also write a custom connector to read the configurations if you are using Spring cloud. https://spring.io/blog/2014/08/05/extending-spring-cloud

Also remember you may need to create Security Groups in CF to open ports to connect cloud foundry to local network. https://docs.cloudfoundry.org/concepts/asg.html



来源:https://stackoverflow.com/questions/43878143/how-to-create-a-cups-service-for-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!