Unable to connect with Mongodb Atlas using Mongoose [duplicate] - node.js

When I try to connect to Mongo instance using this connection string
mongodb://root:password#localhost:27017/
everything works, however, when I try to specify the database name in the connection string
i.e
mongodb://root:password#localhost:27017/storefont
I get the following error MongoDB Connection Error: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoError: Authentication failed.

Specify the authentication database like this:
mongodb://root:password#localhost:27017/storefont?authSource=admin
If you specify a database then this database is also taken for authentication by default. The MongoDB documentation is not 100% clear in that topic.
See also Authentication failure while trying to save to mongodb

The answer provided by Wernfield is correct. I am providing a small tweak along with an example to connect to a replica set:
When using the authSource query param, please ensure that the connection string is wrapped in single quotes OR just use --authenticationDatabase argument instead,
mongo mongodb://ip1:27017,ip2:27017,ip3:27017/my-db --authenticationDatabase admin -u myUsername -p myPassword

Related

How to create the connection string for a database generated with pgAdmin4

I developed my app with SQLAlchemy testing it on an SQLite database.
Now I would like to testing more using PostgreSQL but in a localhost once again.
For this reason, I tried to create a local Postgre database with pdAdmin4 (my laptop has Windows 7 so I installed the v4 version of the database manager).
Now I am trying to generate the connection string for this Postgre database but I cannot understand how to do it.
This is what I created in pgAdmin4:
while the details of the server are these:
The connection string I am trying to use is this:
selected_engine = create_engine('postgresql+psycopg2://postgres:123#localhost/test_server/test_database')
The password 123 is the password I set at the first access to pgdAmin4.
However, I got this error:
OperationalError: (psycopg2.OperationalError) connection to server at "localhost" (::1), port 5432 failed: FATAL: database "test_server/test_database" does not exist
I am sure I am wrong and I cannot figure out what is the correct URL for my database.

Mongoose connection error when using db name in connection string

When I try to connect to Mongo instance using this connection string
mongodb://root:password#localhost:27017/
everything works, however, when I try to specify the database name in the connection string
i.e
mongodb://root:password#localhost:27017/storefont
I get the following error MongoDB Connection Error: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoError: Authentication failed.
Specify the authentication database like this:
mongodb://root:password#localhost:27017/storefont?authSource=admin
If you specify a database then this database is also taken for authentication by default. The MongoDB documentation is not 100% clear in that topic.
See also Authentication failure while trying to save to mongodb
The answer provided by Wernfield is correct. I am providing a small tweak along with an example to connect to a replica set:
When using the authSource query param, please ensure that the connection string is wrapped in single quotes OR just use --authenticationDatabase argument instead,
mongo mongodb://ip1:27017,ip2:27017,ip3:27017/my-db --authenticationDatabase admin -u myUsername -p myPassword

Connection sasl conversation error(authentication error) while using mongoexport

I am trying to export data from a mongodb cluster to my computer, using my URI connection string, but am getting the error: could not connect to server: connection() : auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-S HA-1": (AtlasError) bad auth Authentication failed
This is the command I am using:
mongoexport --uri="mongodb+srv://yash_verma:<******>#jspsych-eymdu.mongodb.net/test?retryWrites=true&w=majority" --collection=entries --out=entries.csv
Could anyone tell me what it is that I am doing wrong? I am sure I am using the correct password.
I am also fairly new to programming and have tried to look online for a solution, but haven't found one yet.
Any help would be greatly appreciated.
Thanks,
Yash.
Your connection string looks fine, but make sure to remove the angle brackets (<>) around <password>, like so:
mongoexport --uri="mongodb+srv://yash_verma:******#jspsych-eymdu.mongodb.net/test?retryWrites=true&w=majority" --collection=entries --out=entries.csv
…where ****** is the database password (not the account password!) of the database user yash_verma.
Duplicate answer

How to get standard URI connection string (without srv) on mongodb website with current update?

I have made a cluster on mongodb website, whitelisted IP adress, added the user and connected my application, but it is not giving me 'Standard URI connection' option (maybe their website got updated) instead it is giving me one with srv. So when I used it in mongoose.connect() function it is giving me error:"Invalid Uri,Must begin with mongodb://"...
I have been working to connect to mongodb for so long but failing every time
You have created cluster on MongoDB Atlas? Right.
If using mongo application:
change nodejs version to 2.12 or later from connect your application screen.
If using mongo shell:
On the "Connect to Cluster0" page, select I have mongo shell installed, then select mongo shell version "3.4 or earlier". You will get the desired format.

Cannot fetch from or insert data to mongodb using mongoose

I wrote a node web app and created a mongoDb database on my local system. I was using the following code to connect to local mongodb from node js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/db_name'); //local
And everything was working fine on my local machine. So I went on and created an mlab account and created a database. But when I tried to run the code by changing the connection string, connections are still established I believe. But the find and save requests are not invoking the callbacks, even no errors shows up. All requests are getting timed out.
var mongoose = require('mongoose');
mongoose.connect("mongodb://user:pass#ds036789.mlab.com:36789/db_name"); //mlab
Another thing I noticed is that I cannot ping ds036789.mlab.com. But TCP connections are succeeding when I tried the nc command
nc -w 3 -v ds036789.mlab.com 36789
I even tried deploying to azure. Which doesn't work either. Any help is much appreciated. Thanks.
EDIT:
Not being able to ping was due to the fact that I used azure hosting. It is expected. And I also found out that I get this error while trying to connect :
connection error: { [MongoError: auth failed] name: 'MongoError', ok: 0, errmsg: 'auth failed', code: 18 }
Credentials are correct though.
From the error mesasge it seems like you are using invalid auth details
This is most likely happen when you do not create username and password for individual database i.e, db_name in you case.
Check mLabs account and create username and password for db_name database and update your connection string.
According to the error information, as #Astro said, it seems to be caused by using invalid auth user/password which be created for database.
Did you create a new user for connecting the database, not account user for mlab? Such as the figures below shown.
Fig 1. A database user is required for connecting
Fig 2. Users list for the database
Hope it helps.
I figured out the issue, it wasn't an issue with the credentials. It was an issue with the mongoose version. The mongoose version I used didn't support the authentication. I had to remove the package and reinstall the latest version. with
node install mongoose#latest
Hope it helps someone. And thanks for the answers :)

Resources