MongoError : Connect failed - Bitnami AWS instance - node.js

I am trying to connect to my AWS Bitnami MEAN instance. The code has been uploaded on the server. But on connecting to it, I am getting the following error :
I have been following the steps given at : https://scotch.io/tutorials/deploying-a-mean-app-to-amazon-ec2-part-1 to connect to the instance.
In the second aprt of it, where they are making changes for mongoDB, I did all those, but since then, I am getting this error.

Your connection is being refused, it seems like an authentication error. You must provide the password in your application, i.e. if you are using Mongoose in order to connect:
var Mongoose = require('mongoose');
var db = Mongoose.createConnection('mongodb://*USER:PASSWORD*#localhost/*DATABASE*');
You must use your correct mongodb socket and your correct password and database name.
Best regards,
Silvio Fernández

Related

Can't authenticate to local MongoDB database

I recently started working on a project with Express and since I'm using Node.js as backend I chose MongoDB as my database. It's my first time working with Mongo but I can't authenticate with Express, it works fine from terminal. I followed the guide from MogoDB blog here. I tried using their Atlas service where I had no problem authenticating. I'm using MongoDB driver. Here's how my connection URI looks like:
const uri = `mongodb://${username}:${password}#127.0.0.1/cloud?retryWrites=true&w=majority`;
I tried changing mongodb:// to mongodb+srv:// but that resulted in invalid connection string error.
You need to specify the authentication database, usually admin:
const uri = `mongodb://${username}:${password}#127.0.0.1/cloud?authSource=admin&retryWrites=true&w=majority`;
If you don't specify it then in your case MonogDB defaults the authentication database to cloud - which is most likely wrong.
When you are accessing mongodb on the web you can click on connect and on connect your application. You will show the uri to copy paste starting with
mongodb+srv://USERNAME:PASSWORD#CLUSTER/DATABASE
You forgot to specify the CLUSTER. Currently is your local Database

Bitnami MEAN stack can't connect to local mongoDb

I am trying to connect to my local mongodb with my node express app hosted on litghtsail MEAN bitnami.
The connection string im using with mongoosse client look like this: mongodb://app:sang#127.0.0.1:27017/prisedesang
Im able to connect whit that user remotely with a SSH tunnel. Im able to connect with that same user localy using mongo cli.
However when I use the app I get ERR_CONNECTION_REFUSED when I try to connect to the db hosted on the same server... Any suggestion?
Can you try using MongooseJS? You can connect your application with MongoDB using MongooseJS, an object modeling driver for Node.js. It is already installed in the MEAN stack so you only have to add the following lines to your app.js file:
var Mongoose = require('mongoose');
var db = Mongoose.createConnection('mongodb://USER:PASSWORD#localhost/DATABASE');

Cannot read/write on a MongoDB Atlas database using Mongoose

I have a new sandbox cluster on MongoDB Atlas that I am connecting to with mongoose on a Node.js server. This is the code I'm using to connect:
const mongoDbUrl =
`mongodb+srv://${username}:${password}#mydb-sandbox-12345.mongodb.net/testdb`
mongoose.connect(mongoDbUrl)
const connection = mongoose.connection
connection.on('connected', () => {
console.log('Connected to mongodb')
})
In the Atlas dashboard I have a readWriteAnyDatabase user that I am authenticating with. Authentication works as expected. I am able to connect to the database and no error is thrown on the connection. I can confirm this by removing a character in the password - authentication fails and I'm unable to connect.
The problem is when I try to insert documents.
const UserModel = require('./models/User')
UserModel.create({
name: 'Hello Atlas'
})
I get the following error:
MongoError: not authorized on admin to execute command {
insert: "users",
documents: [
[{
name Hello Atlas
} {
_id ObjectIdHex("5aa17933d72d25730a340611")
} {
__v 0
}]
],
ordered: false
}
As far as I know the user I'm authenticating with should have permission to read and write on the database I'm connecting to. The other part I don't understand is that the error shows that it's trying to write to admin even though my url is connecting to testdb.
Not sure if you have seen this post, but it could be because you are on a free cluster? Hope this helps.
UPDATE
I looked into the problem further and reproduced it on my own. I got the same error. However, I noticed that at one point Atlas provided me with a choice of connection strings. I went back to that page and chose I am using driver 3.4 or earlier.
The connection string looks like this:
const mongoDbUrl = `mongodb://${username}:${password}#cluster0-shard-00-00-1wntz.mongodb.net:27017,cluster0-shard-00-01-1wntz.mongodb.net:27017,cluster0-shard-00-02-1wntz.mongodb.net:27017/testdb?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin`;
It worked with that connection string.
It looks like the free version of MongoDB Atlas launches with v3.4
If you are using free cluster. change 'admin' to 'test' in the path:
mongodb+srv://username:password#cluster0-yauj8.mongodb.net/test?retryWrites=true&w=majority
This worked for me
Going off #rithesh-mys 's answer. Replacing it with "test" is specifying the db. So you have to change it to the db name that you would use.
I had the same problem. I was trying a lot of connection strings, but the one for olders mongo shell (3.4 or earlier) worked for me.
mongodb://my_username:my_password#cluster0-shard-00-00.osobw.mongodb.net:27017,cluster0-shard-00-01.osobw.mongodb.net:27017,cluster0-shard-00-02.osobw.mongodb.net:27017/my_database?ssl=true&replicaSet=atlas-xw3rfy-shard-0&authSource=admin
I think that newer versions of connection strings don't work with mongoose, at least with free clusters.
Make sure the user you have created has the write and read privileges.

Is mongoose.Types.ObjectId() an absolutely Node.js client-side operation?

I'm using the Mongoose next to the Node.js. The question is:
Does the following command for creation of the ObjectID value make a call to the server?
mongoose.Types.ObjectId()
I've checked my local MongoDB server log, and it doesn't show anything like a call to the single local MongoDB node/server for requesting a new ObjectID value. However, I'm not sure if the default server log is about all the operations(trivial and essential ops), or not!
NOTE: trivial ops here means the non-manipulative data operations!
A very simple script suggests that it does not make a call to the server:
Installing mongoose by running npm install mongoose
Then make a 2-liner index.js:
var mongoose = require('mongoose');
console.log(new mongoose.Types.ObjectId);
Because we haven't even connected yet.

Auth failed, code 18 when connecting to MongoLab database

I'm trying to connect to a MongoLab database but keep getting the following error on connection:
{ [MongoError: auth failed] name: 'MongoError', ok: 0, errmsg: 'auth failed', code: 18 }
The code I'm using to connect is:
var mongoose = require("mongoose");
mongoose.connect("mongodb://username:password#ds061474.mongolab.com:61474/apitest");
mongoose.connection.on('error', function (err) {
console.log(err);
});
When I connect using the shell, I have no problems whatsoever. What am I doing wrong?
I have encountered similar problem when connecting the mongo db using mongoose. After exploring a while I found mongoLab is using SCRAM-SHA-1 authentication.
Refer to the question below I tried to upgrade my mongoose to V4.1.11, and then it works for me
Authentication in mongoose using SCRAM-SHA-1
I faced the same issue while I try to import data from the locale to server.
Those 2 parameters can be important, it worked after I put them:
--authenticationMechanism 'MONGODB-CR'
--authenticationDatabase "admin"
Be careful about the auth mechanism, can be a different one. Check this part of documentation: https://docs.mongodb.com/manual/reference/program/mongoimport/#cmdoption-mongoimport-authenticationmechanism
Had this error myself, turns out I did two things incorrectly (thanks Idos):
Used the mongolab.com username instead of the database one.
Tried to connect to a mongo 3.4 database using a 2.6 shell provided through Ubuntu's repositories. mongo --version to check.
Follow the instructions from this MongoDB page to add their keys and repositories to your APT sources in order to upgrade and keep your MongoDB installation updated going forward.
i had a similar error in that case. put authSourse=admin and ssl=true to your connection
e.g
mongodb://username:password#ds061474.mongolab.com:61474/apitest?authSourse=admin&ssl=true

Resources