问题
I am using spring boot and created a web application using a mongodb database. Locally I use command prompt "mongod" and "mongo" where I can query the data that I have inputted in the UI.
My current application is using MongoDb running on localhost with default port 27017. My web application reflect the data that is stored in the database. When I push this application to cloud foundry, and bind the MONGODB service, it uses another database. Where and how can I view/access all the data that are being inputted. On the local machine I am able to use db.collection.find() and it queries all of my data.
Problem
Once I push my application to cloud foundry, All my data that was stored locally is not linked with it. I am able to store values into PCF MongoDB. But I do not know how to view the data that I have in it. Is there a command or a method to view all the data that I have inputted into PCF MongoDB?
Attempt
Looking at my VCAP_SERVICES I was able to see my database name, username, and password. But they look like they are encrypted, it has letters numbers and hypens all mixed. Example below how VCAP looks like(replica values, same format)
"database": "9faf201a-39b1-4lse-49242f404g11"
"host": "10.100.100.333"
"password": "2jnkj4nk22kk5lk6kj4n4k6nkj6001"
"username": "401849301k-8g3f-5c3j-k28-583920308592f04"
I tried using the code below in a CLI
mongo someurl.mongodomain.com:45475/database_name -u username -p password
So for databasename, username, and password I simply copied and pasted the encrypted looking username and password
mongo myurl.com:1337/9faf201a-39b1-4lse-49242f404g11 -u 401849301k-8g3f-5c3j-k28-583920308592f04" -p 2jnkj4nk22kk5lk6kj4n4k6nkj6001
and I get a connection failed. Maybe I have to input the correct user name and password. Where can I set a username and password? I am using spring boot and it was automatically handled for me. So that, I never created a username or password.
回答1:
Here are five suggestions (thanks to Daniel.Mikusa for the last one):
Push a web client for mongodb and bind it to your database. There seems to be a cloudfoundry wrapper for mongo express: https://github.com/komushi/cf-mongo-express .
You may be able to connect to the remote mongodb with the mongo client program in a similar way as you connect to your local mongodb. You can find the credentials (username, password, db-name) in the environment of your app:
cf env <your-app-name>
Access to the mongodb instance may, however, be blocked from machines outside of your CloudFoundry installation. In this case you may want to try the next option.
Push a docker container that has the mongo client installed to cloudfoundry. Ssh into the docker container, and use the mongo client from there. Pushing docker containers to CF is not enabled in all cloudfoundry installations.
Finally you could expose your domain objects via REST-Controllers. Possibly using spring data rest: http://projects.spring.io/spring-data-rest/.
Use
cf ssh
and an ssh tunnel. Bind the mongodb instance to an app, runcf env
to get the host, port and credentials (or make a service key). Then runcf ssh -N -L <localport>:<service-fqdn-or-ip>:<remote-port> app-name
(the app you connect to doesn't strictly matter, it's just the one we are tunneling through). Now connect a client tolocalhost:<localport>
and use the credentials you got from cf env.
来源:https://stackoverflow.com/questions/41349499/how-to-access-mongodb-values-data-after-binding-to-cloud-foundry