Get error "getaddrinfo EAI_AGAIN" when logout from Atlas

Hello! :wave:

I’m use Mongo Atlas for connect my Nextjs website to db and show data in json (mysite/api/comments), but a connection is successful only when I’m authorized in site https://cloud.mongodb.com/, if I’m logout from a Atlas website, my Nextjs show this error and can’t get data from Mongo :exploding_head:

Error

 ○ Compiling /api/comments ...
 ✓ Compiled /api/comments in 978ms (1775 modules)
 
 ⨯ MongoServerSelectionError: getaddrinfo EAI_AGAIN ac-u4oyhzv-shard-00-00.9iuxagc.mongodb.net
    at EventTarget.<anonymous> (/workspaces/beta/node_modules/.pnpm/mongodb@6.3.0/node_modules/mongodb/lib/sdam/topology.js:276:34)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:814:20)
    at EventTarget.dispatchEvent (node:internal/event_target:749:26)
    at abortSignal (node:internal/abort_controller:371:10)
    at TimeoutController.abort (node:internal/abort_controller:393:5)
    at Timeout.<anonymous> (/workspaces/beta/node_modules/.pnpm/mongodb@6.3.0/node_modules/mongodb/lib/utils.js:1011:92)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
      'ac-u4oyhzv-shard-00-00.9iuxagc.mongodb.net:27017' => [ServerDescription],
      'ac-u4oyhzv-shard-00-01.9iuxagc.mongodb.net:27017' => [ServerDescription],
      'ac-u4oyhzv-shard-00-02.9iuxagc.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'atlas-pzy369-shard-0',
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined,
  page: '/api/comments',
  [Symbol(errorLabels)]: Set(0) {},
  [cause]: MongoNetworkError: getaddrinfo EAI_AGAIN ac-u4oyhzv-shard-00-00.9iuxagc.mongodb.net
      at connectionFailureError (/workspaces/beta/node_modules/.pnpm/mongodb@6.3.0/node_modules/mongodb/lib/cmap/connect.js:379:20)
      at TLSSocket.<anonymous> (/workspaces/beta/node_modules/.pnpm/mongodb@6.3.0/node_modules/mongodb/lib/cmap/connect.js:285:22)
      at Object.onceWrapper (node:events:629:26)
      at TLSSocket.emit (node:events:514:28)
      at emitErrorNT (node:internal/streams/destroy:151:8)
      at emitErrorCloseNT (node:internal/streams/destroy:116:3)
      at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
    [Symbol(errorLabels)]: Set(1) { 'ResetPool' },
    [cause]: Error: getaddrinfo EAI_AGAIN ac-u4oyhzv-shard-00-00.9iuxagc.mongodb.net
        at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:118:26) {
      errno: -3001,
      code: 'EAI_AGAIN',
      syscall: 'getaddrinfo',
      hostname: 'ac-u4oyhzv-shard-00-00.9iuxagc.mongodb.net'
    }
  }
}

 ○ Compiling /_error ...
 ✓ Compiled /_error in 2.3s (3756 modules)

This is my .env

MONGODB_URI=mongodb+srv://alex:password@clusterus.9iuxagc.mongodb.net/?retryWrites=true&w=majority
MONGODB_DB=App

In Atlas access setting

Please help me, how I can fix it? :pray:

Hi @Anton_Antipov,

Welcome to the MongoDB Community :sparkles:

Looking at the error stacks it seems to be a DNS lookup timed-out error, which means it might be a network connectivity error or proxy-related error. Try switching your DNS to 8.8.8.8 and 8.8.4.4 (Google DNS) and see if that works.

Best regards,
Kushagra