Fellow Community :
I’m desperately trying to connect through Mongoose in a node.js 20 server running on port 5001 in a Docker container to my database located on the same ubuntu 23 VPS (Virtual Private Server) as the running container, however failing to do so. Note: Interestingly, I am able to connect to the database from Mongo compass or my node server on my computer…
I have tried:
- Formulating the mongo URI host with : localhost:27017, 127.0.0.1:27017, 0.0.0.0:27017 and {server’s ip}:27017 ;
- Allowing on the firewall (
ufw
) all of the above configurations to access port 27017 ; - Allowing in the bindIp list of
/etc/mongod.conf
all of the above configurations including the universal “0.0.0.0” config to allow all IPs ; - Restarting the mongod service multiple times after modifying the
mongod.conf
and also after resetting the node server, the systemctl mongod service was always enabled and fully operational before trying to do anything with the node server ; - Configuring the mongoose connection through the connection string or mongoose’s connection options ;
- Verified that the env variables are being correctly passed through into the container ;
Results
All of my above results ultimately ended with the following error log:
MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at _handleConnectionErrors (/usr/src/api/node_modules/mongoose/lib/connection.js:875:11)
at NativeConnection.openUri (/usr/src/api/node_modules/mongoose/lib/connection.js:826:11) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) { '127.0.0.1:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}
Node server
For additional context on my mongoose connection configuration, here is the the code powering the index.ts page:
import express from "express";
import mongoose from "mongoose";
import yachtsRoutes from "./routes/yachtsRoutes";
import dotenv from "dotenv";
import cors from "cors";
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3000;
const uri = `mongodb://${process.env.URI}/?directConnection=true` || "";
const db = process.env.DB || "";
const user = process.env.USER || "";
const pass = process.env.PASS || "";
mongoose
.connect(uri, {
appName: "mongosh+2.2.4",
serverSelectionTimeoutMS: 2000,
dbName: db,
user: user,
pass: pass,
})
.then(() => console.log("MongoDB connected"))
.catch((e) => console.log(e));
app.use(express.json());
app.use(
cors({
origin: process.env.WEB_URL,
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
allowedHeaders: "Content-Type, Authorization",
credentials: true,
}),
);
app.use("/yachts", yachtsRoutes);
app.listen(PORT, () => {
console.log(`Server is at port ${PORT}.`);
});
Conspiracy or harsh reality?
So my ultimate conclusion of this repetitive failure, is that my cloud provider OVH Cloud may be blocking the usage of databases internally, since they’re providing the possibility to order databases, but that just sounds like a foolish conspiracy, although I’m seriously starting to consider this…
I would appreciate, any kind of contribution to my issue, I really ran out of ideas on any possible solutions and tried all I found online.