Fail to connect Mongoose to Atlas - node.js

I'm always connecting to the "admin" DB, which is a fixed bug.
Using Mongoose 5.0.6 MongoDb 3.6 and trying to connect to Atlas.
My question, what driver Mongoose 5.0.6 depend on?
How can I find out when Mongoose will have that fix?
On a different direction, is there a way to connect with MongoDB then use this connection with
Mongoose?
Cheers

Basically you should try connecting with your url link, and specify the DB name on the mongoose connect method
so if your cluster link is:
mongodb+srv://userName:Passwrod#clustor.mongodb.net/
and your DB name is:
testDB
then you should call the mongoose.connect method as follows:
mongoose.connect('mongodb+srv://userName:Passwrod#cluster.mongodb.net/', {dbName: 'testDB'});

Connection is established when you use this connection string for (MongoShell 3.6+) :
var connDB = "mongodb+srv://<username>:<password>#cluster-fax0w.mongodb.net/test"
However, you will not be able to read/write data without entering the DBName in the mongoose.connect().
mongoose.connect(uri, { dbName: <your DB name> })
.then( () => {
console.log('Connection to the Atlas Cluster is successful!')
})
.catch( (err) => console.error(err));

Just a side note if you ever get an error connecting to the MongoDB Atlas make sure:
You've whitlisted our IP address / all (0.0.0.0/0)
Your credentials are right
Firewall / anti-virus are disabled
And if all fails
Make sure your DNS is a public one. Or maybe change it to Google's public DNS.
Throwing a link for the same:
https://www.youtube.com/watch?v=F9u5ULrRt3Y
Goodluck!

Related

How do i resolve this Mongo Server Selection Error

Am quiet new to backend and database. I have a solution currently using mongodb. It was working fine till yesterday when i starting having the connect ETIMEDOUT 13.37.254.237:27017 error. Nothing was changed in the URI path or tampered with. It just started and i have not been able to sort it out.
is there any help available please?
I have created another cluster and its working well. But my initial cluster that has datas which are live from clients is not connecting still.
My connection code
I have used these connections code but it has not worked. It was connecting fine all through yesterday but today without tampering with the code, couldn't connect to my mongodb
mongoose.connect(process.env.MONGO_URI,{ useNewUrlParser: true, useUnifiedTopology: true });
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URL);
console.log(`MongoDB Connected: ${conn.connection.host}`);
} catch (error) {
console.log(error);
process.exit(1);
}
};
my mongoose connection and the timedout error
Whenever you connect to Mongodb using IPs that keep updating from your system causes this kind of issues.
also this can be due to your network connection. So i will advice you to:
To allow connection from any IP address(but must ensure your URI is not made known to the public to avoid attack/ access from unwanted users.)
2.Check your network status(data)
3. Run the mongo URI on your atlas

MongoDB connection error: MongoTimeoutError: Server selection timed out after 30000 ms

I am trying to create a fullstack app reading the following tutorial:
https://medium.com/javascript-in-plain-english/full-stack-mongodb-react-node-js-express-js-in-one-simple-app-6cc8ed6de274
I followed all steps and then tried to run:
node server.js
But I got the following error:
MongoDB connection error: MongoTimeoutError: Server selection timed
out after 30000 ms
at Timeout._onTimeout (C:\RND\fullstack_app\backend\node_modules\mongodb\lib\core\sdam\server_selection.js:308:9)
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7) { name: 'MongoTimeoutError', reason: Error: connect ETIMEDOUT
99.80.11.208:27017
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14) {
name: 'MongoNetworkError',
[Symbol(mongoErrorContextSymbol)]: {} }, [Symbol(mongoErrorContextSymbol)]: {} } (node:42892)
UnhandledPromiseRejectionWarning: MongoTimeoutError: Server selection
timed out after 30000 ms
at Timeout._onTimeout (C:\RND\fullstack_app\backend\node_modules\mongodb\lib\core\sdam\server_selection.js:308:9)
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7)
My code at server.js is as follows:
const mongoose = require('mongoose');
const router = express.Router();
// this is our MongoDB database
const dbRoute =
'mongodb+srv://user:<password>#cluster0-3zrv8.mongodb.net/test?retryWrites=true&w=majority';
mongoose.Promise = global.Promise;
// connects our back end code with the database
mongoose.connect(dbRoute,
{ useNewUrlParser: true,
useUnifiedTopology: true
});
let db = mongoose.connection;
db.once('open', () => console.log('connected to the database'));
Any suggestions?
just go to mongodb atlas admin panel.Go in security tab>Network Access> Then whitelist your IP by adding it
See this image to locate the particular menu
Note:Check your IP on google then add it
I wasted whole day on this because the whitelist suggestion was not my issue (I'm running in docker compose with --bind_ip option set properly and I can see the Connection accepted in the mongo logs each time my client tried to connection).
It turns out, I simply needed to add this to the end of my connection string in the code:
?directConnection=true
connection string:
mongodb://myUserId:myPassword#mongodb/myDatabase?directConnection=true
I hope mongodb documents this better because I only stumbled across it from looking at the mongosh logs when I connected using that client.
Sometimes this error occurs due to the IP Address given access to in your database.
Mind you, your application can be working well then come up with such an error.
Anytime this happens, most times your IP address has changed, which is not on the whitelist of addresses allowed. (IP addresses can be dynamic and are subject to change)
All you have to do is to update the whitelist with your current IP addresses
Sometimes it will also happen when your MongoDB services are turned OFF.
Here are the steps to Turn ON the MongoDB Services:
Press window key + R to open Run window.
Then type services.msc to open services window.
Then select MongoDB server, right-click on it, finally click on the start.
I got the same error. These are the steps I followed for resolve it
Log into your Mongo Atlas account
Go to the security tab -> Network Access -> Then whitelist your IP
Then go to the security tab -> Database Access -> and delete your current user.
Create a new user by providing new username and password.
Provide that newly created username and password in the .env file or mongoose.connect method
After these steps it worked as usual. hope this will help you guys.
Ensure that the "<" and ">" are removed when you replace the user and password fields. This worked for me
In my case, this issue happened after NodeJS version upgrade from Node 14 to Node 17 and the mongoose was not connecting to MongoDB in local.
Solution - In connection string, change localhost to 127.0.0.1.
Ref - https://github.com/Automattic/mongoose/issues/10917#issuecomment-957671662
Are you using any Antivirus, Firewall, VPN or on the restricted network (e.g. work/commercial Wi-Fi/LAN connection)? Then try to turn it off/reconnect to a different network. Some IPs/connections might be blocked by the administrator of a network that you're using or simply antivirus might have firewall policies. Even though if you have 0.0.0.0 in your IP Address at the MongoDB Atlas.
I am able to solve the issue. Everything was fine, but the firewall was blocking access to port 27017. connectivity can be tested at http://portquiz.net:27017/ or using telnet to the endpoint which can be retrieved from clusters->Metrics.
Thanks, everybody for the suggestions
Whitelist your connection IP address.
Atlas only allows client connections to the cluster from entries in the project’s whitelist. The project whitelist is distinct from the API whitelist, which restricts API access to specific IP or CIDR addresses.
NOTE
You can skip this step if Atlas indicates in the Setup Connection Security step that you have already configured a whitelist entry in your cluster. To manage the IP whitelist, see Add Entries to the Whitelist.
If the whitelist is empty, Atlas prompts you to add an IP address to the project’s whitelist. You can either:
Click Add Your Current IP Address to whitelist your current IP address.
Click Add a Different IP Address to add a single IP address or a CIDR-notated range of addresses.
For Atlas clusters deployed on Amazon Web Services (AWS) and using VPC Peering, you can add a Security Group associated with the peer VPC.
You can provide an optional description for the newly added IP address or CIDR range. Click Add IP Address to add the address to the whitelist.
You can remove { useUnifiedTopology: true } flag and reinstall mongoose dependecy!
it worked for me.
To anyone still struggling through this issue. I resolved this issue by setting all the parameters like username, password and dbName in mongoose options itself.
Earlier the code was like this. (When I was getting the error).
import mongoose from "mongoose";
mongoose.Promise = require("bluebird");
let dbName = process.env.DB_NAME;
const dbAddress = process.env.DB_HOST;
const dbPort = process.env.DB_PORT;
if(!dbName || !dbAddress || !dbPort){
throw new Error("Mongo error unable to configuredatabase");
}
let options = {
useNewUrlParser: true,
useUnifiedTopology: true,
user:process.env.DB_USER,
pass: process.env.DB_PASS
};
mongoose.connect(`mongodb://${dbAddress}:${dbPort}/${dbName}`, options).catch(err => {
if (err.message.indexOf("ECONNREFUSED") !== -1) {
console.error("Error: The server was not able to reach MongoDB. Maybe it's not running?");
process.exit(1);
} else {
throw err;
}
});
Note that url in mongo connect. mongodb://${dbAddress}:${dbPort}/${dbName}.
New code without error.
import mongoose from "mongoose";
mongoose.Promise = require("bluebird");
let dbName = process.env.DB_NAME;
const dbAddress = process.env.DB_HOST;
const dbPort = process.env.DB_PORT;
// console.log("process.env", process.env)
if(!dbName || !dbAddress || !dbPort){
throw new Error("Mongo error unable to configuredatabase");
}
let options = {
useNewUrlParser: true,
useUnifiedTopology: true,
user:process.env.DB_USER,
pass: process.env.DB_PASS,
dbName: dbName
};
mongoose.connect(`mongodb://${dbAddress}:${dbPort}`, options).catch(err => {
if (err.message.indexOf("ECONNREFUSED") !== -1) {
console.error("Error: The server was not able to reach MongoDB. Maybe it's not running?");
process.exit(1);
} else {
throw err;
}
});
Note the options and the url.
This is my local env. Not production env.
Hope It'll be helpful.
try to specify the node driver, version 2,2,12 in cluster --> connect --> connect your application. new string must be with mongodb://. use this string to connect. do not forget to enter a password
Most of the answers present in this thread should fix you're issue, I tried all the answers and whitelisted all the IP's but still I was not able to connect to the database.
Interestingly, my system was connected to a VPN while I was trying to connect to the mongo atlas. So, I just disconnected the VPN and was able to connect to the db. This was just an simple issue due to which I was not able to connect to the database.
i was has this problem, to me the solution was check if my ip be configured correctly and before confirm in this screen
enter image description here
Please Check your Password, Username OR IP configuration. Mostly "Server selection timed out after 30000 ms at Timeout._onTimeout" comes whenever your above things are not matched with your server configuration.
Try to make new User in Database Access with the default Authentication Method which is "SCRAM". Then make a new Cluster for that. It worked for me! My
server.js file
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 5000;
app.use(cors());
app.use(express.json());
//const uri = process.env.ATLAS_URI;
mongoose.createConnection("mongodb+srv://u1:u1#cluster0-u3zl8.mongodb.net/test", { userNewParser: true, useCreateIndex: true, useUnifiedTopology: true}
);
const connection = mongoose.connection;
connection.once('once', () => {
console.log(`MongoDB databse connection established successfully`);
})
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});
What is your mongoose version? because there is an issue with a certain mongoose version. Please visit this link for more details "https://github.com/Automattic/mongoose/issues/8180". Anyway, I was facing the same issue but after using an older version (5.5.2), it worked for me. Please give a try and let me know what happened.
It might basically be because of an npm update.
I just updated to npm v 6.13.7. and I began to get that error.
Following instructions from #Matheus, I commented the line "{ useUnifiedTopology: true }" and I was able to get through it.
I updated mongoose and everything works fine.
I had the same issue. I could connect from MongoDB Compass or my Node app using the connection strings Atlas gave me. I resolved it by adding my IP Address into the Whitelist in Atlas.
For anyone who may face this problem with mongoose v6+ and nodejs and white listing your ip has not solved it or changing from localhost to 127.0.0.1.
Solution
Set the following options for the mongoose client connection options:
mongoose.connect(url, {
maxIdleTimeMS: 80000,
serverSelectionTimeoutMS: 80000,
socketTimeoutMS: 0,
connectTimeoutMS: 0
}
These options stop the mongoose client from timining out the connection before it selects a primary from the DB cluster.
Please remember to set any other options relevant to your setup like if you are running a cluster, make sure to set the replicaSet option or if you have authentication enabled, set the authSource option or the database you are connecting to dbName.
Some of these options can be set in the connection string.
The full list of options can be found here: https://mongodb.github.io/node-mongodb-native/4.2/interfaces/MongoClientOptions.html#authSource
This must be a temporary network issue. Please try to connect after some time and hopefully it should be fine. but make sure you white list your ip address.
If your config is alright and still the error shows up, it maybe because of too much data in the db (Like for me, I am using the free tier for testing my app where I upload posts and the posts are images with some description), so I just deleted the db and it worked fine after that. Hope this helps someone.
I have solved this problem by using the following way, use a callback.
const uri = '';
mongoose
.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true }, () => {
console.log("we are connected");
})
.catch((err) => console.log(err));
Newer versions of Mongoose and MongoDB require your application port higher than 5000.
If your app running on port 3000 for example, you'll have this error message.
I just changed my PORT from 3000 to 5556 and I'm fine 🎉

How to make connection with 2 DBs while using mongo official driver for node js

Am trying to make the connection from node js to Mongo DB.
while using mongo's official node js driver Official
I am able to make a connection with DB at one at a time.
but am looking for a way to get connected with 2 DBs at a time.
for a fallback issue.
Approach one:
const MONGO_CLIENT = require('mongodb').MongoClient;
const MONGO_URL = mongodb://USER:PASS#HOST:PORT/DBNAME
MONGO_CLIENT.connect(MONGO_URL, (err: any, db: any) => {
if (err) {
res.status(503).send(err);
}
console.log ('Connected do the things');
}
I got success by this way.
but am looking for a fallback way.
while setting two Hosts in URL I got errors
const MONGO_URL = mongodb://USER:PASS#PRIMARY_HOST:PRIMARY_HOST_PORT,SECONDARY_HOST:SECONDARY_HOST_PORT/DBNAME
am getting following errors
{
"name":"MongoError",
"message":"seed list contains no mongos proxies, replicaset connections requires the parameter replicaSet to be supplied in the URI or options object, mongodb://server:port/db?replicaSet=name"
}
Could anyone please explain to me what am doing wrong. and how should I connect with multiple DBS?
NOTE: Primary and secondary DBs are same and always synched.

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.

Can't connect to mongo lab on arduino yun

I can't connect to a database with mongodb or mongoose on nodejs on my arduino yun.
Unable to connect to the mongoDB server. Error: { [MongoError: Authentication failed.]
name: 'MongoError',
message: 'Authentication failed.',
ok: 0,
code: 18,
errmsg: 'Authentication failed.' }
I get that error. The code works fine on my desktop computer.
//lets require/import the mongodb native drivers.
var mongodb = require('mongodb');
//We need to work with "MongoClient" interface in order to connect to a mongodb server.
var MongoClient = mongodb.MongoClient;
// Connection URL. This is where your mongodb server is running.
var url = 'mongodb://*******:********#address:23118/arduino';
// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
//HURRAY!! We are connected. :)
console.log('Connection established to', url);
// do some work here with the database.
//Close connection
db.close();
}
});
I am trying to connect to a mongo lab database. I have not tried connecting to any other database to troubleshoot. This is because I cannot find a free trial version where I do not need a credit card.
EDIT:
I got an Object Rocket instance and it works perfectly on my arduino yun. However it is very expensive; $30 a month (currently on the trial). And I don't need speed or lots of data. So if anyone could figure out why it doesnt work on mlab that would be great.
I've experienced some authentication issues in the past and I've addressed them following the next steps:
1) Identify version numbers of both mongo server and mongoose
2) Identify what authentication mechanism does your server use and make sure that mongoose is trying to authenticate with the one your server is expecting. If you're using an old version, some authentication mechanism might not be supported, so you'd need to upgrade.
3) Identify where the user has been created. Check out Authentication Database and make sure that your client is authenticating using such database. It can be different than the database you store your data. Indeed, it uses to be admin. If so, take a look to the authSource option you can add to your connection string. More info here.
In addition to all of the above, I'd recommend you to use the the mongo client to verify your credentials are right and you can connect to the database.

Resources