FG
🗄️ DatabasesMongoDB

No retries are made after "failed to connect on first connect"

Freshabout 7 years ago
Mar 14, 20260 views
Confidence Score76%
76%

Problem

By default, mongoose throws an Error if the first connect fails, which crashes node. So to reproduce this bug, you will need the following code in your app, to catch the error and prevent the crash: db.on('error', console.error.bind(console, 'connection error:')); Now we can reproduce this bug as follows: 1. Shut down your MongoDB 2. Start up your node app that uses mongoose 3. Your app will log: `[MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]]` 4. Start up your MongoDB again 5. Observe that mongoose does not now connect to the working MongoDB. The only way to get reconnected is to restart your app, or to use a manual workaround. Expected behaviour: Since `autoreconnect` defaults to true, I would expect mongoose to establish a connection soon after the MongoDB is accessible again. Note: If the first connect succeeds, but the connection to MongoDB is lost during runtime, then autoreconnect works fine, as expected. The problem is the inconsistency if MongoDB is not available when the app starts up. (If this is the desired behaviour, and developers are recommended to handle this situation by not catching the error, and letting node crash, then I can accept that, but it is worth making it clear.) node v4.4.1, mongoose@4.9.4, mongodb@2.2.19, mongodb-core@2.1.4

Error Output

Error if the first connect fails, which crashes node.

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
High Confidence Fix
75% confidence100% success rate7 verificationsLast verified Mar 14, 2026

Solution: No retries are made after "failed to connect on first connect"

Low Risk

For anyone wanting auto-reconnection when first connect fails, this is how I handle it: [code block] For mongoose < 4.11 use `db.open()` instead of `db.openUri()` For mongoose 4.11.7 this technique does not work. For mongoose 4.13.4 it is working again! ---- Edit 2019/09/02: There is also a shorter solution using `promiseRetry` here.

75

Trust Score

7 verifications

100% success
  1. 1

    For anyone wanting auto-reconnection when first connect fails, this is how I han

    For anyone wanting auto-reconnection when first connect fails, this is how I handle it:

  2. 2

    For mongoose < 4.11 use `db.open()` instead of `db.openUri()`

    For mongoose 4.11.7 this technique does not work. For mongoose 4.13.4 it is working again!

  3. 3

    Edit 2019/09/02: There is also a shorter solution using `promiseRetry` here.

    Edit 2019/09/02: There is also a shorter solution using `promiseRetry` here.

Validation

Resolved in Automattic/mongoose GitHub issue #5169. Community reactions: 30 upvotes.

Verification Summary

Worked: 7
Partial: 2
Last verified Mar 14, 2026

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

mongoosemongodbodmdocs