Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.92k stars 3.84k forks source link

MongoError: server selection failed #12941

Closed OguzBey closed 1 year ago

OguzBey commented 1 year ago

Prerequisites

Mongoose version

5.13.15

Node.js version

16.5.0

MongoDB server version

4.4.0

Typescript version (if applicable)

No response

Description

I don't understand where this error occurs. Express app, nodejs cluster, Mongodb cluster

MongoError: server selection failed
at Mongos.selectServer (/my-project/api/node_modules/mongodb/lib/core/topologies/mongos.js:1190:14)
at Mongos.selectServer (/my-project/api/node_modules/mongodb/lib/topologies/topology_base.js:342:32)
at executeWithServerSelection (/my-project/api/node_modules/mongodb/lib/operations/execute_operation.js:131:12)
at /my-project/api/node_modules/mongodb/lib/operations/execute_operation.js:70:9
at maybePromise (/my-project/api/node_modules/mongodb/lib/utils.js:692:3)
at executeOperation (/my-project/api/node_modules/mongodb/lib/operations/execute_operation.js:34:10)
at Cursor._initializeCursor (/my-project/api/node_modules/mongodb/lib/core/cursor.js:541:7)
at Cursor._initializeCursor (/my-project/api/node_modules/mongodb/lib/cursor.js:190:11)
at nextFunction (/my-project/api/node_modules/mongodb/lib/core/cursor.js:744:10)
at Cursor._next (/my-project/api/node_modules/mongodb/lib/core/cursor.js:209:5)
at /my-project/api/node_modules/mongodb/lib/cursor.js:253:14
at maybePromise (/my-project/api/node_modules/mongodb/lib/utils.js:692:3)
at Cursor.next (/my-project/api/node_modules/mongodb/lib/cursor.js:238:12)
at FindOneOperation.execute (/my-project/api/node_modules/mongodb/lib/operations/find_one.js:29:14)
at /my-project/api/node_modules/mongodb/lib/operations/execute_operation.js:72:19
at maybePromise (/my-project/api/node_modules/mongodb/lib/utils.js:692:3)

reconnection hook was not triggered either.

Steps to Reproduce

function connectDB() {
  mongoose.connect(connectionURI, connectionOpts).then(
    () => {
      console.log('DB Connected !');
    },
    (err) => {
      console.error('DB CONNECTION ERROR:', err);
    }
  );
}

mongoose.connection.on('error', (err) => {
  console.error(err);
  mongoose.disconnect();
});

mongoose.connection.on('disconnected', () => {
  console.log('Mongoose Connection: Disconnected. Reconnection starts in 5 secs...');
  setTimeout(() => {
    connectDB();
  }, 5000);
});

Expected Behavior

Does this error occur while query is running or does it happen by itself? Can someone explain?

IslandRhythms commented 1 year ago

Are you trying to connect locally, to a server? What happens when you log the connectionURI?

OguzBey commented 1 year ago

Are you trying to connect locally, to a server? What happens when you log the connectionURI?

Are you trying to connect locally, to a server? What happens when you log the connectionURI?

I do not trying to reconnect locally, just such errors appear on the screen, I do not understand how it came out yet. App running on production. If I could catch the error I would print some variables to the screen.

IslandRhythms commented 1 year ago

@vkarpov15 I saw that there was an issue similar to this that you handled so I'll defer this to you if I'm unable to resolve.

IslandRhythms commented 1 year ago

https://github.com/Automattic/mongoose/issues/9167

IslandRhythms commented 1 year ago

No logs in the connection error event are firing? Is this your first time attempting to connect to production or have you connected before?

OguzBey commented 1 year ago

No logs in the connection error event are firing? Is this your first time attempting to connect to production or have you connected before?

Yes, no connection error firing. If it happen, first error display then calling disconnect event and 'Mongoose Connection: Disconnected. Reconnection starts in 5 secs...' text prints. I don't think the connection is broken either.

After deploying in the production environment, mongoose must be updated. Maybe I can solve this problem by downgrading the version. I don't think I've seen such an error before.

package.json version -> "mongoose": "^5.11.8" package-lock 5.13.15

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

github-actions[bot] commented 1 year ago

This issue was closed because it has been inactive for 19 days since being marked as stale.