问题
I am using A2 shared hosting server to host my node express application. I am giving the following URI to connect to mongoose.
const uri = mongodb+srv://<username>:<password>@cluster0-gwff9.gcp.mongodb.net/customer-data?retryWrites=true&w=majority
and the mongoose connect function is:
mongoose.connect(uri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true}).then(() => {
console.log("Successfully connected to the database"); }).catch(err => {
console.log('Could not connect to the database. Exiting now...', err);
process.exit();});
I have whitelisted the public as well as private IP address of the host. Then also when I run an npm start
it gives the following error :
Could not connect to the database. Exiting now... { 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 (/home/cleanlab/apps/prelaunch/node_modules/mongoose/lib/connection.js:826:32)
at Mongoose.connect (/home/cleanlab/apps/prelaunch/node_modules/mongoose/lib/index.js:335:15)
at Object.<anonymous> (/home/cleanlab/apps/prelaunch/server/model/mongodb.js:5:10)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Module.require (internal/modules/cjs/loader.js:650:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (/home/cleanlab/apps/prelaunch/server/server.js:22:1)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
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-02-gwff9.gcp.mongodb.net:27017' => [ServerDescription],
'cluster0-shard-00-01-gwff9.gcp.mongodb.net:27017' => [ServerDescription],
'cluster0-shard-00-00-gwff9.gcp.mongodb.net:27017' => [ServerDescription] },
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null } }
I have also whitelisted the public IP of my laptop. When I run the same code locally, it connects to database. Its just not working on the shared server and gives above error. How to correct this ?
回答1:
So I was able to make it work. Actually my shared hosting server has blocked the port 27017 which is used for mongodb. Nothing was wrong with the code or configuration. They enabled the port and it worked.
回答2:
I had the same problem like you . But after using Google's DNS server (8.8.8.8 , 8.8.4.4 ) ,and whitelist my ip address i managed to connect to the MongoDB Atlas. Hope that's solve your problem
来源:https://stackoverflow.com/questions/62522565/not-able-to-connect-to-mongodb-atlas-database