Mongoose unable to connect to db on same server

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật