'Mongoose.connect is not connecting to local db
I'm building a simple MERN blogging app with a local MongoDB. Everything was working fine until yesterday. I literally changed nothing at all and now I can't connect to my local DB with my Express app while a connection via MongoDB Compass is working fine. Also if I connect to MongoDB Atlas it's also working fine in my Express app.
Tried restarting brew services and updating mongodb-community and mongoose but nothing seems to work.
Code to connect:
const mongoose = require("mongoose");
mongoose
.connect(process.env.DATABASE_LOCAL, {}) // DATABASE_CLOUD = mongodb://localhost:27017/seo-blog
.then(() => console.log("Database connected!"));
Error I get from the server:
Server is running on port: 8000
/Users/tallestguy/Documents/Projects/seo-blog/backend/node_modules/mongoose/lib/connection.js:796
const serverSelectionError = new ServerSelectionError();
^
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at NativeConnection.Connection.openUri (/Users/tallestguy/Documents/Projects/seo-blog/backend/node_modules/mongoose/lib/connection.js:796:32)
at /Users/tallestguy/Documents/Projects/seo-blog/backend/node_modules/mongoose/lib/index.js:329:10
at /Users/tallestguy/Documents/Projects/seo-blog/backend/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (/Users/tallestguy/Documents/Projects/seo-blog/backend/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (/Users/tallestguy/Documents/Projects/seo-blog/backend/node_modules/mongoose/lib/index.js:1150:10)
at Mongoose.connect (/Users/tallestguy/Documents/Projects/seo-blog/backend/node_modules/mongoose/lib/index.js:328:20)
at Object.<anonymous> (/Users/tallestguy/Documents/Projects/seo-blog/backend/server.js:22:4)
at Module._compile (node:internal/modules/cjs/loader:1095:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) {
'localhost:27017' => ServerDescription {
_hostAddress: HostAddress { isIPv6: false, host: 'localhost', port: 27017 },
address: 'localhost:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 410689241,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED ::1:27017
at connectionFailureError (/Users/tallestguy/Documents/Projects/seo-blog/backend/node_modules/mongodb/lib/cmap/connect.js:293:20)
at Socket.<anonymous> (/Users/tallestguy/Documents/Projects/seo-blog/backend/node_modules/mongodb/lib/cmap/connect.js:267:22)
at Object.onceWrapper (node:events:510:26)
at Socket.emit (node:events:390:28)
at emitErrorNT (node:internal/streams/destroy:164:8)
at emitErrorCloseNT (node:internal/streams/destroy:129:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
}
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
}
}
Node.js v17.0.1
[nodemon] app crashed - waiting for file changes before starting...```
Solution 1:[1]
change "localhost" for 127.0.0.1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jym |