问题
Having issues connecting to my server using the MEAN stack. I had no problems connecting until today and have not made code changes since so I am baffled on why all a sudden I am not able to connect.
Connection:
mongoose.connect("mongodb+srv://theller5567:" + process.env.MONGO_ATLAS_PW + "@cluster0-efzkv.mongodb.net/node-angular", { useNewUrlParser: true })
.then(() => {
console.log("Connected to database!");
})
.catch((error) => {
console.log("Connection failed!", error);
});
Response:
[nodemon] starting `node server.js`
Connection failed! { MongoNetworkError: failed to connect to server
[cluster0-shard-00-01-efzkv.mongodb.net:27017] on first connect
[MongoNetworkError: getaddrinfo ENOTFOUND cluster0-shard-00-01-
efzkv.mongodb.net cluster0-shard-00-01-efzkv.mongodb.net:27017]
at Pool.<anonymous> (/Users/Travis/Desktop/Github_Repos/OMNI-INC/Omni-
pl/node_modules/mongodb-core/lib/topologies/server.js:564:11)
at Pool.emit (events.js:182:13)
at Connection.<anonymous> (/Users/Travis/Desktop/Github_Repos/OMNI-INC/Omni-
pl/node_modules/mongodb-core/lib/connection/pool.js:317:12)
at Object.onceWrapper (events.js:273:13)
at Connection.emit (events.js:182:13)
at TLSSocket.<anonymous> (/Users/Travis/Desktop/Github_Repos/OMNI-INC/Omni-
pl/node_modules/mongodb-core/lib/connection/connection.js:246:50)
at Object.onceWrapper (events.js:273:13)
at TLSSocket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
Response from connecting using MongoShell in terminal:
connecting to: mongodb+srv://cluster0-efzkv.mongodb.net/test
2018-09-21T13:22:28.817-0400 I NETWORK [js] Starting new replica set monitor
for Cluster0-shard-0/cluster0-shard-00-02-efzkv.mongodb.net.:27017,cluster0-
shard-00-00-efzkv.mongodb.net.:27017,cluster0-shard-00-01-
efzkv.mongodb.net.:27017
2018-09-21T13:23:22.276-0400 W NETWORK [js] Unable to reach primary for set
Cluster0-shard-0
2018-09-21T13:23:22.276-0400 I NETWORK [js] Cannot reach any nodes for set
Cluster0-shard-0. Please check network connectivity and the status of the
set. This has happened for 1 checks in a row.
2018-09-21T13:23:22.280-0400 E QUERY [js] Error: connect failed to replica
set Cluster0-shard-0/cluster0-shard-00-02-efzkv.mongodb.net.:27017,cluster0-
shard-00-00-efzkv.mongodb.net.:27017,cluster0-shard-00-01-
efzkv.mongodb.net.:27017 :
connect@src/mongo/shell/mongo.js:257:13
@(connect):1:6
exception: connect failed
I have mongod running in a separate terminal like always. Does anyone have any ideas on what might be the issue and how to fix based on the error response?
Any help would be great, thank you!
回答1:
there are a few things you could try which helped me out in the same situation.
- Log into mongoDB Atlas and check if Atlas is working. If it's working go to the Security > IP-Whitelist and delete your current IP, then add a new one with your current IP. Maybe your IP changed because of your provider. Also check if your mongoDB user is still valid, you can check it right next to the IP Whitelist tab.
- Try to uninstall and reinstall your mongoose package. Before reinstalling clean your npm cache.
- Change your URI-String type to connect your application. Change it from a "SRV connection string" to a "Standard connection string". In the Dashboard inside your cluster click on CONNECT > Connect your application > Standard connection string. Copy the string and replace the SRV-String in your application. This helped me out most of the time when I had issues connecting to Atlas.
- Check that the internet connection you're using isn't blocking traffic/access to mongoDB atlas.
来源:https://stackoverflow.com/questions/52447418/unable-to-connect-to-mongodb-atlas-cluster-using-mean-stack