Skip to content

MongooseServerSelectionError: getaddrinfo EAI_AGAIN mongodb #565

Description

Good day sir.
Please assist me with this issue when connecting mongo docker to the mongoose to start the server. I'm new to docker and I'm trying to connect mongo docker to mongoose but apparently, things are not working so well. Your assistance would be highly appreciated.

Error:

MongooseServerSelectionError: getaddrinfo EAI_AGAIN mongodb
at NativeConnection.Connection.openUri (/home/app/node_modules/mongoose/lib/connection.js:824:32)
at /home/app/node_modules/mongoose/lib/index.js:380:10
at /home/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5
at new Promise ()
at promiseOrCallback (/home/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)
at Mongoose._promiseOrCallback (/home/app/node_modules/mongoose/lib/index.js:1225:10)
at Mongoose.connect (/home/app/node_modules/mongoose/lib/index.js:379:20)
at connectDB (/home/app/connections/database.js:4:18)
at start (/home/app/server.js:31:9)
at Object. (/home/app/server.js:39:1) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) { 'mongodb:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
},
code: undefined
}

mongo.yaml

version: "3"
services:
mongodb:
image: mongo
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
mongo-express:
image: mongo-express
depends_on:
- mongodb
restart: always
ports:
- 8080:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=password
- ME_CONFIG_MONGODB_SERVER=mongodb
- ME_CONFIG_MONGODB_HOSTNAME=localhost

  server.js
  
  const express = require("express");

const app = express();
const bodyParser = require("body-parser");
const path = require("path");

// Routes and controllers
const router = require("./routes/route");
const controller = require("./controllers/controller");
const connectDB = require("./connections/database");
const User = require("./models/User");

app.use(express.json());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true,
limit: "100000mb",
parameterLimit: 10000000
}));

app.use("/public", express.static(path.join(__dirname, "assets")));
app.set("views", __dirname + "/views");
app.set("view engine", "ejs");
app.get("/", async (req, res) => {
res.render("index");
});

app.use("/api/v1/test", router);
const PORT = 3000;
const start = async () => {
try {
await connectDB("mongodb://admin:password@mongodb");
app.listen(PORT, () => console.log(Server is listening to port ${PORT}));
} catch(error) {
console.log(error);
return error;
}
}

start();

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionUsability question, not directly related to an error with the image

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions