Flask-User with MongoDB Atlas - mongodb

I'm trying to follow the Flask-User documentation with MongoDB.
However, when I try to connect to my MongoDB Atlas, it's always writing to a "test" database, instead of the one I specify.
I have identified that it is because of the Flask-MongoEngine that is being used, as it mentions that
By default, Flask-MongoEngine assumes that the mongod instance is
running on localhost on port 27017, and you wish to connect to the
database named test.
However, even after inputting my URI details, I still keep writing to the test database.
Uri style connections are also supported, just supply the uri as the
hostin the ‘MONGODB_SETTINGS’ dictionary with app.config. Note that
database name from uri has priority over name.
app.config['MONGODB_SETTINGS'] = { 'db': 'project1', 'host': 'mongodb://localhost/database_name' }
My code
My configuration settings (albeit in another syntax) are as below
app.config['MONGODB_DB'] = '<redcated-database-name>'
app.config['MONGODB_HOST'] = 'mongodb+srv://<redcated-user>:<redcated-pw>#gapcluster-np4bu.mongodb.net/test?retryWrites=true&w=majority'
app.config['MONGODB_PORT'] = 27017
app.config["MONGODB_CONNECTION"] = False
Has anyone successfully used Flask-User with MongoDB Atlas before?

Related

Mongodump fails with "a direct connection cant be made if multiple hosts are specified"

I have a dev & staging database which i connect to regularly (using mongoose.connect(<uri>)).
I would like to mongodump them. Im using the following syntax:
mongodump --uri=<uri>
This works fine for dev and I was able to dump the dev database using this command. The staging database however produces the following error:
Failed: can't create session: error configuring the connector: a direct connection cannot be made if multiple hosts are specified
Does anyone know or have any insight on what the issue could be? I haven't found any docs or posts about this error.
Historically, when given a URI with one host in it (or otherwise a single host to connect to), some MongoDB drivers performed a direct connection to that host only and some drivers inspected the type of the server and, if it was a replica set node, connected to replica set (which may have caused none of the queries or commands to be sent to the host which was originally given in the URI).
In the MongoDB 4.4-compatible drivers the behavior was unified, and directConnection URI option added to force either behavior (true to connect to the specified host, false to perform discovery; if not set, the driver would do whatever it did historically).
When direct connection is used, you can only specify one host. Your URI contains or resolves to more than one.
For the gritty details, see https://github.com/mongodb/specifications/pull/739.

Connect to Database directly via Mongo Compass

Via shell, I can directly connect to mongo database with this string
mongo --ssl host1,host2:port/MyDataBase...
And I land directly on the MyDataBase.
Is there a similar way to do it in Compass? I get connected to whole server and I can see all the other databases. I just want to connect to MyDataBase.
I am using the lattest version of Compass, so it may differ from your current version.
It is important the you are in the network of the server, or use a VPN connection, otherwise, it does not work.
Step 1
Step 2
Please,let me know if that works!

AWS Postgres DB "does not exist" when connecting with PG

I can't seem to connect to my DB instance in AWS. I'm using the pg package and following the examples from the website is not working.
A search for "aws postgres database does not exist" really isn't returning anything helpful. Going through the open/closed issues on the PG github isnt helpful either.
Running $nc <RDS endpoint> <port number> returns a success message so it's definitely there. Every value placed in the Client config is copy/pasted from my DB instance.
I'm starting to wonder if the databases have a different name than what it shows in the "Instances" section of RDS on AWS?
const client = new Client({
host : '<<RDS ENDPOINT>>',
database : '<<RDS NAME>>', // maybe this isnt the real name?
user : '<<username>>',
password : '<<password>>',
port : <<port>>
});
client.connect()
.then(data => {
console.log('connected');
})
.catch(err => {
console.log(err);
})
I ran into this issue as well. It looks like the DB Instance Name and the actual DB name are two different things and even when you add a name when you create your DB, it defaults to 'postgres'. When I put in the name of my DB it gave me the same error. However, when I just put in 'postgres' it worked fine. Try that and see if it works for you.
The initial configuration of RDS instances is quite messy, since the parameter "database name" is only the name of the instance, not the proper name of the database. If you want AWS to create a database at the moment you create the db instance, you have to select "Additional configuration" and explicitly add a parameter called "Initial database name". Check the screenshot I attach here.
Try adding postgres as dbname. It worked for me!
After connecting with postgres as db name, you can type \l to list all database on that PSQL cluster, that will return a bunch of default dbs and also the one you created (the name) so you can connect to it
I ran into the same issue after creating a DB instance on AWS RDS. I wanted to test the connection of my database using PostBird, and I used my actual DB instance name but it could not work.
But I used "postgres in field of DB_name and it worked. That means that my default username was posgres and db_name was also "posgres.
I hope it will help you too.
Try this if the above answer does not work.
Remove the:5439/lab ending so that the Host value ends with: .com

Connecting to MongoDB using Spring Repository

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

Mongo db database not accessible from shell, data storage

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.