mongo icon indicating copy to clipboard operation
mongo copied to clipboard

Connection error

Open garrigaj opened this issue 4 years ago • 14 comments

I am getting this error when I try to connect: error: Uncaught (in promise) Error: MongoError: "Connection failed: Invalid hostname: 'cluster0.gpkxo.mongodb.net?retryWrites=true&w=majority'" throw new MongoError(Connection failed: ${e.message || e}); ^ at MongoClient.connect (https://deno.land/x/[email protected]/src/client.ts:93:15) at async MongoDBConnector._makeConnection (https://deno.land/x/[email protected]/lib/connectors/mongodb-connector.ts:44:7) at async MongoDBConnector.query (https://deno.land/x/[email protected]/lib/connectors/mongodb-connector.ts:65:5) at async Database.query (https://deno.land/x/[email protected]/lib/database.ts:240:21) at async Function.createTable (https://deno.land/x/[email protected]/lib/model.ts:172:5) at async Database.sync (https://deno.land/x/[email protected]/lib/database.ts:210:7)

I used the code example from the home page: db: 'todo-app', tls: true, servers: [ { host: 'cluster0.gpkxo.mongodb.net?retryWrites=true&w=majority', port: 27017, }, ], credential: { username: '<my_username>', password: '<my_password>', db: 'todo-app', mechanism: 'SCRAM-SHA-1', },

I also verified that the password was available by resetting and I made sure I had whitelisted my IP address.

garrigaj avatar Jun 06 '21 20:06 garrigaj

Try to connect using URI

manyuanrong avatar Jun 07 '21 06:06 manyuanrong

I did and got the same error message.

garrigaj avatar Jun 07 '21 15:06 garrigaj

There is a problem with the domain name resolution. Are you sure your domain name is correct? Can you connect through other client tools?

manyuanrong avatar Jun 08 '21 02:06 manyuanrong

I connect with Mongoose on Node.js using the full URI without issues. When I use either the full URI or the more detailed method here, I can't connect.

garrigaj avatar Jun 08 '21 03:06 garrigaj

host should only contains cluster0.gpkxo.mongodb.net

manyuanrong avatar Jun 08 '21 03:06 manyuanrong

Tried it. It still didn’t work. I also tried with and without the Mongo protocol and still could not connect.

garrigaj avatar Jun 08 '21 03:06 garrigaj

I don't know what you tried and whether you constructed the parameters correctly. Just believe that you did the right thing. By the way, can your domain name be resolved successfully through ping or route? (give me the result of your attempt)

manyuanrong avatar Jun 08 '21 03:06 manyuanrong

I can connect with Mongoose, MongoDB Compass, and Node.js MongoDB driver with the URI. I cannot connect with this library.

garrigaj avatar Jun 08 '21 03:06 garrigaj

Perhaps your database is on an internal network. You can give me the results of your investigation. Like this, image

manyuanrong avatar Jun 08 '21 03:06 manyuanrong

That is the URI MongoDB Atlas gives me. Everything, except this driver, that I use to connect to it works: "mongo+srv://garrigaj:[email protected]/db_name?retryWrites=true&w=majority"

garrigaj avatar Jun 08 '21 16:06 garrigaj

What made it possible for me to connect via URI is adding the "authMechanism" param exactly like shown below. I also added it to the docs here a few weeks ago.

await client.connect(
  "mongodb+srv://<username>:<password>@<db_cluster_url>/<db_name>?authMechanism=SCRAM-SHA-1",
);

RNR1 avatar Jun 20 '21 07:06 RNR1

Hi, i'm having a similar issue.

Error: MongoError: "Connection failed: failed to lookup address information: No address associated with hostname"

I tried both methods on the docs but result in the same issue. I can connect to the db with node so i know the hostname is right.

acaala avatar Jul 11 '21 12:07 acaala

Hello 🙂 If you use Mongo Atlas you should add your IP public address as available in Network Access section

GlennGuegan avatar Jul 29 '21 20:07 GlennGuegan

What made it possible for me to connect via URI is adding the "authMechanism" param exactly like shown below. I also added it to the docs here a few weeks ago.

await client.connect(
  "mongodb+srv://<username>:<password>@<db_cluster_url>/<db_name>?authMechanism=SCRAM-SHA-1",
);

Thanks! This worked perfectly 💜

toridoriv avatar Oct 21 '22 03:10 toridoriv

What made it possible for me to connect via URI is adding the "authMechanism" param exactly like shown below. I also added it to the docs here a few weeks ago.

await client.connect(
  "mongodb+srv://<username>:<password>@<db_cluster_url>/<db_name>?authMechanism=SCRAM-SHA-1",
);

Just for the record. I was also facing this issue and adding the authMechanism solved the problem.

marcelovicentegc avatar Oct 20 '23 16:10 marcelovicentegc