0

I am trying to create a MERN app and containerize it with docker and manage the containers using kubernetes (minikube).

When I try to run the nodejs container it logs the following error

Server v8 up and running on port 5000 !
MongoTimeoutError: Server selection timed out after 10000 ms
at Timeout._onTimeout (/home/app/node_modules/mongodb-core/lib/sdam/topology.js:773:16)
at listOnTimeout (node:internal/timers:559:17)
at processTimers (node:internal/timers:502:7) {
[Symbol(mongoErrorContextSymbol)]: {}
}

The code at server.js is as follows:

const express = require("express");
const mongoose = require("mongoose");

const app = express();

// the connection string when using username and password in mongodb-server
// mongodb://${process.env.MONGO_DB_USERNAME}:${process.env.MONGO_DB_PASSWORD}@mongodb-service:27017/exam?authSource=${process.env.MONGO_DB_USERNAME}
mongoose
  .connect("mongodb://mongodb-service/exam", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() => console.log(`MongoDB successfully connected`))
  .catch((err) => console.log(err));

The logs in mongodb container are as follows:

{"t":{"$date":"2022-12-16T14:28:58.316+00:00"},"s":"I",  "c":"COMMAND",  "id":51803,   "ctx":"LogicalSessionCacheRefresh","msg":"Slow query","attr":{"type":"command","ns":"config.system.sessions","command":{"createIndexes":"system.sessions","v":2,"indexes":[{"key":{"lastUse":1},"name":"lsidTTLIndex","expireAfterSeconds":1800}],"ignoreUnknownIndexOptions":false,"writeConcern":{},"$db":"config"},"numYields":0,"reslen":114,"locks":{"ParallelBatchWriterMode":{"acquireCount":{"r":5}},"FeatureCompatibilityVersion":{"acquireCount":{"r":5,"w":1}},"ReplicationStateTransition":{"acquireCount":{"w":5}},"Global":{"acquireCount":{"r":5,"w":1}},"Database":{"acquireCount":{"r":4,"w":1}},"Collection":{"acquireCount":{"r":5,"w":1}},"Mutex":{"acquireCount":{"r":8}}},"storage":{},"protocol":"op_msg","durationMillis":529}}

If I should provide more details please tell me

Thanks in advance

Alix
  • 78
  • 8
  • 1
    It is rather unlikely that `authSource` is equal to the username. See https://stackoverflow.com/questions/63754742/authentication-failure-while-trying-to-save-to-mongodb/63755470#63755470 – Wernfried Domscheit Dec 16 '22 at 15:57
  • 1
    That log entry doesn't appear to be related to a connection attempt, is there any indication in the server log that a connection was established? – Joe Dec 16 '22 at 18:18
  • No there was no indication to an established connection – Alix Dec 16 '22 at 19:20
  • 1
    @WernfriedDomscheit that authsource was the problem I forgot about it. thank you so much I spent a whole day because of it I didn't pay attention after moving to configuring on k8s – Alix Dec 16 '22 at 19:29

0 Answers0