typeorm: MongoDB MongoServerSelectionError: getaddrinfo ENOTFOUND

Issue Description

When I try to make a normal mongodb connection with typerom@0.2.29 version I get this error:

MongoDB MongoServerSelectionError: getaddrinfo ENOTFOUND [Mongodb Server IP]

but when I try to make a normal mongodb connection with typerom@0.2.25 version it works.

Expected Behavior

Must connect to mongodb server

Actual Behavior

throws this error:

MongoDB MongoServerSelectionError: getaddrinfo ENOTFOUND [Mongodb Server IP]

Steps to Reproduce

  1. install typeorm@0.2.29
  2. connect to a mongodb server with default options
typeorm.createConnection({
            type: "mongodb",
            url: CONFIG.MONGODB_URI,
            synchronize: true,
            logger: "debug",
            useUnifiedTopology: true,
            useNewUrlParser: true,
            autoLoadEntities: true,
        })

My Environment

Dependency Version
Operating System Windows10
Node.js version v15.0.1
Typescript version v3.7.4
TypeORM version v0.2.29

Additional Context

Relevant Database Driver(s)

  • mysql
  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don’t know how to start. I would need guidance.
  • No, I don’t have the time, although I believe I could do it if I had the time…
  • No, I don’t have the time and I wouldn’t even know how to start.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 14
  • Comments: 22 (3 by maintainers)

Commits related to this issue

Most upvoted comments

Fix was merged and will be in v0.2.30 later this week.

Same here, works with v0.2.28 but not with v0.2.29, connect url is mongodb+srv://<hidden>:<hidden>@cluster0.fbbdh.mongodb.net/<hidden>?retryWrites=true&w=majority"

@pleerock, I am waiting for the update. I’m trying to use Next-Auth.js. When the update will be released?

Any news about it? Tried to use typeorm with mongodb atlas and it doesn’t work in any way

Same here.

I’m also using MongoDB Atlas. For weeks i’ve been using the same configuration, but today i had to recreate my node_modules, and it stopped working.

[Nest] 33020 - 05/11/2020 03:03:18 [TypeOrmModule] Unable to connect to the database. Retrying (6)... +3005ms MongoNetworkError: failed to connect to server [cluster0-unwqs.mongodb.net:27017] on first connect [Error: getaddrinfo ENOTFOUND cluster0-unwqs.mongodb.net at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26) { name: 'MongoNetworkError', [Symbol(mongoErrorContextSymbol)]: {} }] at Pool.<anonymous> (C:\projetos\rolla-dev\node_modules\mongodb\lib\core\topologies\server.js:438:11) at Pool.emit (events.js:314:20) at Pool.EventEmitter.emit (domain.js:486:12) at C:\projetos\rolla-dev\node_modules\mongodb\lib\core\connection\pool.js:561:14 at C:\projetos\rolla-dev\node_modules\mongodb\lib\core\connection\pool.js:994:11 at C:\projetos\rolla-dev\node_modules\mongodb\lib\core\connection\connect.js:31:7 at callback (C:\projetos\rolla-dev\node_modules\mongodb\lib\core\connection\connect.js:264:5) at Socket.<anonymous> (C:\projetos\rolla-dev\node_modules\mongodb\lib\core\connection\connect.js:294:7) at Object.onceWrapper (events.js:421:26) at Socket.emit (events.js:314:20)

EDIT:

This is my version:

"typeorm": "^0.2.29",

I was using 0.2.24 but it does not work either.

👋 I encountered this issue today while using next-auth (which uses typeorm). Here are my versions:

$ npm ls typeorm mongodb
├── mongodb@3.6.3 
└─┬ next-auth@3.1.0
  └── typeorm@0.2.29 

I was able to replicate directly with typeorm as @barbarbar338 suggested, using the following script and a free mongodb atlas instance.

const {createConnection} = require('typeorm');

const main = async() => {
  const connection = await createConnection({
    type: "mongodb",
    host: "SOMETHING.mongodb.net",
    port: 27017,
    username: "USER",
    password: "PASSWORD",
    database: "test"
  });

  console.log(connection);
}

main();

Errors:

(node:38953) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.

(node:38953) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [SOMETHING.mongodb.net:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND SOMETHING.mongodb.net SOMETHING.mongodb.net:27017]
    at Pool.<anonymous> (/Users/sammatthews/mug/typeorm-debug-example/node_modules/mongodb/lib/core/topologies/server.js:438:11)
    at Pool.emit (events.js:198:13)
    at createConnection (/Users/sammatthews/mug/typeorm-debug-example/node_modules/mongodb/lib/core/connection/pool.js:562:14)
    at connect (/Users/sammatthews/mug/typeorm-debug-example/node_modules/mongodb/lib/core/connection/pool.js:995:11)
    at makeConnection (/Users/sammatthews/mug/typeorm-debug-example/node_modules/mongodb/lib/core/connection/connect.js:32:7)
    at callback (/Users/sammatthews/mug/typeorm-debug-example/node_modules/mongodb/lib/core/connection/connect.js:280:5)
    at Socket.err (/Users/sammatthews/mug/typeorm-debug-example/node_modules/mongodb/lib/core/connection/connect.js:310:7)
    at Object.onceWrapper (events.js:286:20)
    at Socket.emit (events.js:198:13)
    at emitErrorNT (internal/streams/destroy.js:91:8)

However, I’m unable to successfully run the script above with typeorm@0.2.28. I’m able to successfully connect to the same Mongo Atlas instance without typeorm and the following example script:

const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://user:pass@something.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
client.connect(async() => {
  const collection = client.db("test").collection("testtest");
  await collection.insertOne({"waka":"flocka"}) // works!
  client.close();
});

Additionally, I am able to connect to a local version of mongodb, so there’s a chance this could be an issue with Mongo Atlas and not typeorm.

@FcoMarcosMAbreu in the meantime, if you still have the issue, you can drop a npm i typeorm@0.2.28 in your project, it will override the typeorm’s version next-auth has installed. Just did it yesterday as I had the issue and now it works fine ! Good luck!

This worked for me

{
      name: 'default',
      type: TYPEORM_CONNECTION as string,
      url: TYPEORM_URL, // mongodb+srv://<db.username>:<db.password>@cluster0.***.mongodb.net/<db.name>?retryWrites=true&w=majority
      useNewUrlParser: Boolean(TYPEORM_USE_NEW_URL_PARSER),
      synchronize: Boolean(TYPEORM_SYNCHRONIZE),
      logging: Boolean(TYPEORM_LOGGING),
      cache: {
        type: REDIS_TYPE as string,
        duration: Number(REDIS_DURATION),
        options: {
          host: REDIS_HOST as string,
          port: Number(REDIS_PORT),
        },
      },
      cli: {
        migrationsDir: TYPEORM_MIGRATIONS_DIR as string,
        entitiesDir: TYPEORM_ENTITIES_DIR as string,
        subscribersDir: TYPEORM_SUBSCRIBERS_DIR as string,
      },
      entities: ["src/entity/*.*"],
      migrations: [TYPEORM_MIGRATIONS as string],
      subscribers: [TYPEORM_SUBSCRIBERS as string],
    }

I’ve got the same error on 0.2.29

I tried to trace the error by cloning TypeORM to my local FS, compiling and linking it in my repository… But then it works as normal. Was expecting the error to originate from #6925

Steps I took (more-or-less):

git clone git@github.com:typeorm/typeorm.git
cd typeorm
npm install
git checkout 0.2.29
yarn link
cd ../my-project
yarn link typeorm
yarn start

EDIT: 0.2.28 works fine