https://spring.io/guides/gs/accessing-data-mongodb/ - The example does not show connecting to Mongo DB usage.
default spring data mongo db is connect your localhost if you not declare any connfiguration properties in applicaiton.properties file
for example
launch mongodb server with mongod command and after launch your application and make any CRUD operation see log in your command prompt
if your application need special server uri or any configuration properties
add application.properties file this properties and configure
for example
spring.data.mongodb.uri=mongodb://user:secret#mongo1.example.com:12345,mongo2.example.com:23456/test
this configure your uri with colleciton names
and also declare host and port with this
spring.data.mongodb.host=mongoserver
spring.data.mongodb.port=27017
and also more properties knowledges(for example: username and password settings) here: https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
connecting features here :
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-connecting-to-mongodb
Related
I am build REST api using play framework 2.8 , I am able to start the application and call some service. Facing issue when trying to connecting with postgres database. Logs are below -
Error opening connection for database: {}org.postgresql.util.PSQLException: FATAL: no pg_hba.conf entry for host "3.87.112.32", user "abc", database "xyz", SSL off
Tried looking solution for this and foudn have to add sslmode=require in query param while connection to database.
When log the url sslmode is already mentioned.
jdbc:postgresql://abc.amazonaws.com:5432/xyz?password=1234&sslmode=require&user=xyz
I am reading this property from heroku env variable JDBC_DATABASE_URL for obtaining database connection.
This issue can be fixed by adding addition query parameters apart from sslmode=require which are ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory after adding this the new URL become jdbc:postgresql://abc.amazonaws.com:5432/xyz?password=1234&sslmode=require&user=xyzssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory
I am fairly new to Eclipse Ditto and have just started using it for my project.
I am trying to connect Cloud hosted mongodb instance to ditto.
Following the documentation I know that I need to add some variables and pass them to docker-compose. The problem is that I do not know what should be the values of these variables as there are no examples.
Are all these variables necessary or will just the URI work?
This is my current .env file config
MONGO_DB_URI=mongodb+srv://username:pass#IP
MONGO_DB_READ_PREFERENCE=primary
MONGO_DB_WRITE_CONCERN=majority
The command I am using to start ditto is
docker-compose --env-file .env up
I have removed mongodb service from docker-compose.yml
Nice to hear that you started using Ditto in your project.
You need to set the following env variables to connect to your Cloud hosted MongoDB.
MONGO_DB_URI: Connection string to MongoDB
For more detailes see: https://docs.mongodb.com/manual/reference/connection-string/
If you have a ReplicaSet your MongoDB URI should look like this: mongodb://[username:password#]mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl
I assume you also need to enable SSL to connect to your MongoDB.
To do so set this env var.
MONGO_DB_SSL_ENABLED: true
If you want to use a specific Ditto version you can set the following env var
DITTO_VERSION= e.g. 2.1.0-M3
If you use .env as file name you can start Ditto with:
docker-compose up
The other options for pool size, read preference and write concern aren't necessary as there are default values in place.
Our company recently changed from Basic auth to LDAP authentication and regular apps are now using authSource=$external&authMechanism=PLAIN on the URL.
This works fine on a normal app but I can't figure out how to set these using Spring Data. Surely there is a way to do this, right?
As you can see below you can't set something like authSource/authMechanism so you have to change to use URI. Also note when using URI you cannot use host/port and username/password. Those all need to go in the URI.
Something like
export spring_data_mongodb_database=db
export spring_data_mongodb_uri = mongodb://username:password#host:port?authSource=$external&authMechanism=PLAIN
Here all the properties that you can currently configure for mongodb in spring boot.
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
spring.data.mongodb.authentication-database= # Authentication database name.
spring.data.mongodb.database=test # Database name.
spring.data.mongodb.field-naming-strategy= # Fully qualified name of the FieldNamingStrategy to use.
spring.data.mongodb.grid-fs-database= # GridFS database name.
spring.data.mongodb.host=localhost # Mongo server host.
spring.data.mongodb.password= # Login password of the mongo server.
spring.data.mongodb.port=27017 # Mongo server port.
spring.data.mongodb.repositories.enabled=true # Enable Mongo repositories.
spring.data.mongodb.uri=mongodb://localhost/test # Mongo database URI. When set, host and port are ignored.
spring.data.mongodb.username= # Login user of the mongo server.
If you are using Spring-Boot mongodb api with LDAP then the uri has to be the following in application.properties files in the resources folder:
spring.data.mongodb.uri="mongodb://username:password#host:port/?authSource=$external&authMechanism=PLAIN"
spring.data.mongodb.database="databasThatYouWantToConnectTo"
No need for other parameters like spring.data.mongodb.username, spring.data.mongodb.password, etc., because everhything is mentioned in the spring.data.mongodb.uri
the substring authSource=$external&authMechanism=PLAIN in spring.data.mongodb.uri says Mongo that the authentication and authorization is by LDAP.
You can check the image below which is taken from the Connection String URI format document form MongoDB
If you are using the Mongo Java Driver provided by Mongo Db then you have to do the connection like this:
import com.mongodb.MonogoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoDatabase;
public class MongoConnect{
public static void main(String args[]){
MongoClientURI connString = new MongoClientURI("mongodb://username:password#host:port/?authSource=$external&authMechanism=PLAIN");
MongoClient mongoClient = new MongoClient(connString);
MongoDatabase database = mongoClient.getDatabase("databasThatYouWantToConnectTo");
System.out.println(database.getName()+" "+ mongoClient.getCredentialList());
}
}
I cannot find anywhere to set the default connection URL for the mongo CLI. I want to be able to specify a particular username/password/hostname/database, which can all be specified as a URL. However, I want to just be able to type mongo instead of mongo "mongodb://…" in the shell.
I also don’t want the password to show up in /proc/«PID»/cmdline, but having it in a dotfile in my home directory is perfectly fine with me.
It doesn’t appear that ~/.mongorc.js allows specifying the default connection string. But I would expect such an option to be available because the mysql CLI supports ~/.my.cnf which allows you to specify username/password/hostname/database. So, where is this for mongodb?
EDIT: The solution has to work even if the mongodb at localhost is fully secured (no insecure local test database should be required—I thought this went without saying).
You can run mongo shell commands in ~/.mongorc.js. So you can just run the connect method in your ~/.mongorc.js:
db = connect("mongodb://username:password#host:27017/myDatabase")
When you run mongo it will first connect to localhost, then connect to the database passed to the connect method.
A MongoDB server must be running on localhost, otherwise the mongo shell will error and exit before trying to connect to the other DB.
Run mongo --nodb to get around needing to connect on localhost before connecting to whatever is defined in ~/.mongorc.js.
To directly launch mongo against a particular hostname/database/username/password without prompting for a password, without passing the password on the CLI, and without requiring an insecure database at mongodb://localhost/test, you can use this technique. Write a script file which does the connection for you and then launch mongo with the appropriate arguments. For example:
~/.mongo-do-auth.js
// https://docs.mongodb.com/manual/reference/program/mongo/#cmdoption-nodb
// https://docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/#mongo-shell-new-connections
//
// Run this with:
//
// mongo --nodb --shell ~/.mongo-do-auth.js
db = connect('localhost/admin', 'root', 'asdf123');
And then, simply launch it from the CLI to get an interactive session:
sam ~ # mongo --nodb --shell ~/.mongo-do-auth.js
MongoDB shell version: 2.6.12
type "help" for help
connecting to: localhost/admin
> quit()
Or, to run a script noninteractively, simply remove --shell and append your script. Consider a script:
s2.js:
print('connected to database ' + db);
To run:
sam ~ # mongo --nodb ~/.mongo-do-auth.js ~/s2.js
MongoDB shell version: 2.6.12
loading file: /root/.mongo-do-auth.js
connecting to: localhost/admin
loading file: /root/s2.js
connected to database admin
However, this requires a lot of work and invoking mongo with arguments. It appears that this is a limitation of mongo because no one has suggested any alternative which works as smoothly as the mysql CLI client’s ~/.my.cnf. This means that all shell scripts which directly invoke mongo will need to allow the the user to pass through --nodb and a path to a connection script instead of being able to rely on implicit per-user configuration.
I am working with deployd which uses mongoDB as a db on Ubuntu OS.
All databases of mongoDB are accessible from within mongodb shell but my question is when I create a new project with deployd I can store/read/modify records but this new database is not accessible from mongodb shell, how do i do that?.
Database files for this new project are stored within the application directory not in mongoDB default data directory.
Also what's the difference of mongoDB as a database within application and MongoDB as a global database for all applications.
Currently all collections are accessible using dpd object like
dpd.albums.get(inputParams, function(resultSet, err){ //handle result});
While I would like to access these collections using something like
db.albums.find(); //within mongod shell
It seems there are a few misconceptions here about the deploying with "deployd".
Firstly "deployd" depends on an existing mongoDB installation on it's deployed host or is otherwise configured to use a host on another server. So where "data files" are kept are generally a point of condifuration. All that "deployd" tries to do is start the "mongod" instance as it's own instance is started should the configuration be local.
No matter what you do, all of the options are available in configuration. There is a good sample in the documentation here:
var server = deployd({
port: process.env.PORT || 5000,
env: 'staging',
db: {
host: 'my.production.mongo.host',
port: 27105,
name: 'my-db',
credentials: {
username: 'username',
password: 'password'
}
}
});
As you can see the host and port settings are specified in the configuration of the application itself, so where you are not using the default "mongoDB" host and port then add them to the command line options, as in:
mongo --host my.production.mongo.host --port 27105
So even on the same localhost, note that the instance does not use the default 27017 port but a different one that you would need to specify when connecting.
Also see the mongo shell command documentation as well as the deployd documentation.