Why MongoDB connection gives ETIMEOUT error with ExpressJS?

Sometimes it works just fine & most of the time it’s unable to connect for this timeout error.
I am using WSL2 Ubuntu 20 with Windows 11 (latest).

My IPadress active in the network access (MongoCloud)

Error: querySrv ETIMEOUT _mongodb._tcp.test.y0gmmey.mongodb.net
    at QueryReqWrap.onresolve [as oncomplete] (node:internal/dns/promises:251:17) {
  errno: undefined,
  code: 'ETIMEOUT',
  syscall: 'querySrv',
  hostname: '_mongodb._tcp.test.y0gmmey.mongodb.net'

To solve this DNS error you might want to try to use faster DNS servers such Google’s and

1 Like

:memo: [Edited] (Avoid this reply, nothing important here)

This error raise because of I didn’t property setup my IP address at Mongodb Cloud Network access.

New error after setting DNS to & …

MongoServerSelectionError: connection <monitor> to closed
    at Timeout._onTimeout (/home/tahazzot/code_playground/express/node_modules/mongodb/lib/sdam/topology.js:284:38)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
      'ac-m7ha51d-shard-00-00.y0gmmey.mongodb.net:27017' => [ServerDescription],
      'ac-m7ha51d-shard-00-01.y0gmmey.mongodb.net:27017' => [ServerDescription],
      'ac-m7ha51d-shard-00-02.y0gmmey.mongodb.net:27017' => [ServerDescription]
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'atlas-110lxu-shard-0',
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  code: undefined,
  [Symbol(errorLabels)]: Set(0) {}

If you are using mongoose try upgrading or downgrading the version you are using.

Otherwise try upgrading the mongodb driver.

Go to your Atlas cluster and make sure you have a primary. It is very unlikely that you do not have one so the error ReplicaSetNoPrimary is most likely another error that is wrongly misinterpreted. A different driver version might give a better error message or even not err at all.

:white_check_mark: [SOLVED]


Shift to Google DNS and Works.

:warning: New Issues

(Off topic but causing a new issue, just to remind.)

Thanks @steevej

1 Like

aslo Im facing this same issue
PS C:\myProjects\sparrowtalk> npm run dev

sparrowtalk@1.0.0 dev
nodemon server.js

[nodemon] 3.0.2
[nodemon] to restart at any time, enter rs
[nodemon] watching path(s): .
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting node server.js
Error: queryTxt ETIMEOUT mydata.d0jjezh.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (node:internal/dns/promises:275:17) {
errno: undefined,
code: ‘ETIMEOUT’,
syscall: ‘queryTxt’,
hostname: ‘mydata.d0jjezh.mongodb.net
[nodemon] clean exit - waiting for changes before restart

Google DNS server changed, but still getting thsi same error