问题描述
我当前正在使用本地数据库。使用终端访问mongodb。
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
我正在尝试使用Cloud Foundry CUPS将mongodb与URI连接。
但是遵循URI指南,我缺少用户名和密码。我在哪里可以找到这些价值?我所有的数据当前都存储在本地,并且不希望使用Cloud Foundry提供的MongoDB,因为我不得不再次手动输入这些数据。
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
尝试
在mongo shell中,我执行了以下操作创建管理员。
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"
]
}
)
在此之后,我将以下内容添加到我的application.properties
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
但是当我从application.properties文件中删除用户名和密码时。我的应用程序将按预期运行。
but when I remove username and password from my application.properties file. My application would run as intended.
推荐答案
您上下文中的用户名和密码表示MongoDB的用户名密码。
MongoDB身份验证是可选的。您无需身份验证即可启动mongod。但这在生产系统中不建议使用。
您可以为mongodb启用身份验证,并在其中创建要访问的用户。
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
您可以创建CUPS服务并将其mongodb参数绑定到应用程序。然后,您可以在环境变量VCAP_SERVICES中读取此服务。
System.getEnv( VCAP_SERVICES);
如果您使用的是Spring Cloud,还可以编写一个自定义连接器以读取配置。
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
还请记住,您可能需要在CF中创建安全组以打开端口以将Cloud Foundry连接到本地网络。
https://docs.cloudfoundry.org/concepts/asg.html
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
这篇关于如何为mongoDB创建CUPS服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!