Mongoose Could not connect to servers in My MongoDB Atlas cluster - node.js

I have just created an Atlus mongo db account which is based on Paris and added IP Access, now I'm trying to access it from the Mumbai server. but getting below error.
MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://www.mongodb.com/docs/atlas/security-whitelist/
at NativeConnection.Connection.openUri (/home/sbfasto/backendfortsmith.sbfasto.com/node_modules/mongoose/lib/connection.js:825:32)
at /home/sbfasto/backendfortsmith.sbfasto.com/node_modules/mongoose/lib/index.js:411:10
at /home/sbfasto/backendfortsmith.sbfasto.com/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5
at new Promise (<anonymous>)
at promiseOrCallback (/home/sbfasto/backendfortsmith.sbfasto.com/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)
at Mongoose._promiseOrCallback (/home/sbfasto/backendfortsmith.sbfasto.com/node_modules/mongoose/lib/index.js:1285:10)
at Mongoose.connect (/home/sbfasto/backendfortsmith.sbfasto.com/node_modules/mongoose/lib/index.js:410:20)
at Object.<anonymous> (/home/sbfasto/backendfortsmith.sbfasto.com/app.js:15:10)
at Module._compile (node:internal/modules/cjs/loader:1165:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1219:10) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map(3) {
'ac-nbyh9mn-shard-00-00.6yz0ajv.mongodb.net:27017' => [ServerDescription],
'ac-nbyh9mn-shard-00-01.6yz0ajv.mongodb.net:27017' => [ServerDescription],
'ac-nbyh9mn-shard-00-02.6yz0ajv.mongodb.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'atlas-a81bt4-shard-0',
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}
after this I added IP access for any ip still getting same error. can anyone tell me the reason?
mongoose.connect("mongodb+srv://popcorn:<password>#fortsmith.6yz0ajv.mongodb.net/?retryWrites=true&w=majority", {
useNewUrlParser: true,
useUnifiedTopology: true }).then(() => console.log('mongo connected')).catch(err => console.log(err));

Change the "<password>" in the url to your cluster password

Related

Unable to connect aws documentdb from node.js using mongoose

Here is my node.js code i am trying to connect aws document db using mongoose but getting
below error.
const URL = "mongodb://school:SJDJDJDJDD#dev-docdb-2022-09-02-05-55-49.cqsssmioaafcdjkaooj9.ap-south-1.docdb.amazonaws.com:27017/school?ssl=true&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false";
// Connect to MongoDB.
const connect = mongoose.connect(
URL, {
ssl: true,
sslValidate: true,
sslCA: `./rds-combined-ca-bundle.pem`,
useNewUrlParser: true,
useUnifiedTopology: true,
},
{}
);
errors i am getting
ubuntu#ip-172-31-38-108:~/school_api$ npm start
> graphql-crud#1.0.0 start
> node server.js
🚀 Server ready at http://localhost:4000/graphql
MongooseServerSelectionError: unable to get local issuer certificate
at Connection.openUri (/home/ubuntu/school_api/node_modules/mongoose/lib/connection.js:847:32)
at /home/ubuntu/school_api/node_modules/mongoose/lib/index.js:351:10
at /home/ubuntu/school_api/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (/home/ubuntu/school_api/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (/home/ubuntu/school_api/node_modules/mongoose/lib/index.js:1149:10)
at Mongoose.connect (/home/ubuntu/school_api/node_modules/mongoose/lib/index.js:350:20)
at Object.<anonymous> (/home/ubuntu/school_api/server.js:18:26)
at Module._compile (node:internal/modules/cjs/loader:1119:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)
at Module.load (node:internal/modules/cjs/loader:997:32)
at Module._load (node:internal/modules/cjs/loader:838:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:18:47 {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map(1) {
'dev-docdb-2022-09-02-05-55-49.cqioaafcooj9.ap-south-1.docdb.amazonaws.com:27017' => [ServerDescription]
},
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
}
}
I have my cerificate available also inside my code. please take a look.
I am able to connect to mongodb shell from the server. So there must be something wrong
in the code.
const URL = `mongodb://school:SJDJDJDJDD#dev-docdb-2022-09-02-05-55-49.cqsssmioaafcdjkaooj9.ap-south-1.docdb.amazonaws.com:27017/school?replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false`
const addedToUrl = `&tls=true&tlsCAFile=rds-combined-ca-bundle.pem`;
const connect = mongoose.connect(
`${URL}${addedToUrl}`, {
useNewUrlParser: true,
useUnifiedTopology: true,
},
{}
);
You can place these parameters in the connection string.

After migrating from Monggose 5.x to 6.x facing connectivity issues

I am migrating the mongoose from 5.x to 6.x, using createConnection method and updated the options as suggested in mongoose official site.
Trying to connect through the SSL certificate validation and X.509 authentication.
Getting the below error:
Connected SuccessFully 2
API is running securely on the PORT: 3123
Error connecting to Mongo Database.
MongooseServerSelectionError: unable to get local issuer certificate
at NativeConnection.Connection.openUri (/opt/connect/service/node_modules/mongoose/lib/connection.js:824:32)
at Mongoose.createConnection (/opt/connect/service/node_modules/mongoose/lib/index.js:326:10)
at TestSetupConnection.setupConnection (/opt/connect/service/classtestmongoose.js:29:41)
at /opt/connect/service/app.js:30:60
at propagateAslWrapper (/opt/connect/service/node_modules/async-listener/index.js:504:23)
at /opt/connect/service/node_modules/async-listener/index.js:541:70
at /opt/connect/service/node_modules/async-listener/glue.js:188:31
at processTicksAndRejections (internal/process/task_queues.js:93:5) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map(3) {
'pl-1-us-east-1.5ylwo.mongodb.net:1025' => [ServerDescription],
'pl-1-us-east-1.5ylwo.mongodb.net:1026' => [ServerDescription],
'pl-1-us-east-1.5ylwo.mongodb.net:1024' => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'atlas-24xeac-shard-3',
logicalSessionTimeoutMinutes: undefined
},
code: undefined
}

NodeJS MongoDB ReplicaSetNoPrimary

I was attempting to connect to a MongoDB cluster using the following from the nodeJS MongoDB package
const client = require('mongodb').MongoClient
const conn = await client.connect(
process.env.MONGO_DSN,
{
useUnifiedTopology: true,
ssl: process.env.MONGO_SSL, // true
},
)
The MONGO_DSN variable is as follows:
mongodb://{USER}:{PASSWORD}#{HOST1}:27017,{HOST2}:27017,{HOST3}:27017/{DB_NAME}?ssl=true&replicaSet={REPLICASET}&authSource=admin&retryWrites=true&w=majority
However, the connection times out with the error
MongoServerSelectionError: connection <monitor> to {IP_ADDRESS}:27017 closed
at Timeout._onTimeout (/node_modules/mongodb/lib/core/sdam/topology.js:438:30)
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7) {
name: 'MongoServerSelectionError',
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map {
'{HOST1}:27017' => [ServerDescription],
'{HOST2}:27017' => [ServerDescription],
'{HOST3}:27017' => [ServerDescription]
},
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
}
}
After a little playing around, I found out that I can connect successfully if I remove the ssl config option from the driver constructor and leave it to infer it from the DSN query string:
const client = require('mongodb').MongoClient
const conn = await client.connect(
process.env.MONGO_DSN,
{
useUnifiedTopology: true,
},
)
Why would this config option (not sure if others effect this too) effect the ability to connect?
Is it because if there are config options specified in the constructor that appear in the DSN, then it completely ignores the DSN query string, which would mean some config options would then be missing (primary replica)?
To clarify, there are no issues with the hosts in the DSN, no issues with connecting to the database normally, only when the config is set in the constructor.

MongoDB Atlas cluster connection error while running my app

I've connected my app with Mongo DB database but when running my app I'm getting this error below,also I confirmed that my username and password are correct, I've seen some questions here like this before but nothing helped me, any help or guidance?
[nodemon] starting `node app.js`
MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
at NativeConnection.Connection.openUri (C:\Users\user\Desktop\my port\node_modules\mongoose\lib\connection.js:800:32)
at Mongoose.connect (C:\Users\user\Desktop\my port\node_modules\mongoose\lib\index.js:339:15)
at Object.<anonymous> (C:\Users\user\Desktop\my port\app.js:7:10)
at Module._compile (internal/modules/cjs/loader.js:1251:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1272:10)
at Module.load (internal/modules/cjs/loader.js:1100:32)
at Function.Module._load (internal/modules/cjs/loader.js:962:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47 {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map(3) {
'codie-shard-00-00.lcuzy.mongodb.net:27017' => [ServerDescription],
'codie-shard-00-01.lcuzy.mongodb.net:27017' => [ServerDescription],
'codie-shard-00-02.lcuzy.mongodb.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
}
}
Seems you have not whitelisted your IP address on MongoDB Atlas cluster,
follow below and whitelist
https://docs.atlas.mongodb.com/tutorial/whitelist-connection-ip-address/

Problem connecting mongodb with mongoose, after whitelisted my IP address

I can't connect to my MongoDb database. I checked several times, my IP address is correctly whithelisted in the network access. And I have the following message :
message:
'Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that
you\'re trying to access the database from an IP that isn\'t whitelisted. Make sure your current IP address is on your Atlas cluster\'s IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/',
reason:
TopologyDescription {
type: 'ReplicaSetNoPrimary',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers:
Map {
'cluster0-shard-00-01-216w6.mongodb.net:27017' => [ServerDescription],
'cluster0-shard-00-02-216w6.mongodb.net:27017' => [ServerDescription],
'cluster0-shard-00-00-216w6.mongodb.net:27017' => [ServerDescription] },
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null } }
^C
I had the same issue and solved it by:
Go to "Network Access".
On the IP Address registered go to "EDIT" Actions.
If your IP Address was changed, you will see an option to allow access to the current IP Address, click on that option in order to update the IP.

Resources