denodrivers / mongo

🍃 MongoDB driver for Deno 🦕
https://deno.land/x/mongo
MIT License
509 stars 95 forks source link

connect to MongoDB Atlas Error: MongoError: "Connection failed: failed to lookup address information: nodename nor servname provided, or not known" #203

Closed RNR1 closed 3 years ago

RNR1 commented 3 years ago

I am getting an error when trying to login to a MongoDB Atlas DB:

Error: MongoError: "Connection failed: failed to lookup address information: nodename nor servname provided, or not known"
    at MongoClient.connect (https://deno.land/x/mongo@v0.23.1/src/client.ts:26:13)
    at async connect (file:///Users/ronbraha/code/projects/road-trip/backend/config/db_client.ts:8:8)

specifications:

macOS Big Sur Version 11.2.3
deno 1.10.2 (release, x86_64-apple-darwin)
v8 9.1.269.27
typescript 4.2.2
mongo@v0.23.1

My code:

import { MongoClient } from 'mongo';

let db: ReturnType<MongoClient['database']>;

export async function connect() {
    try {
        db = await new MongoClient().connect({
            db: 'roadTrip',
            tls: true,
            servers: [
                {
                    host: 'cluster0.vfygo.mongodb.net',
                    port: 27017
                }
            ],
            credential: {
                username: 'Ron',
                password: 'Aj8FOtWMhrZMa8Ai',
                mechanism: 'SCRAM-SHA-1'
            }
        });
    } catch (err) {
        console.error(err);
    }
}

export function getDb() {
    return db;
}

To clarify, this is the URL I got from the atlas cluster connect:

mongodb+srv://Ron:<password>@cluster0.vfygo.mongodb.net/roadTrip?retryWrites=true&w=majority

I really appreciate any help you can provide.

manyuanrong commented 3 years ago

It seems that there is a problem with your server address, you can directly use uri to connect

image

RNR1 commented 3 years ago

Thanks for your comment! I tried using the URI as well, I was not familiar about passing the authMechanism in the query params, but still, I get the following error:

MongoError: "Connection failed: MongoError: \"Auth mechanism not implemented in Deno driver: SCRAM_SHA_1\""

If I omit this param, I get the following message

MongoError: "Connection failed: MongoError: {\"ok\":0,\"errmsg\":\"auth mechanism SCRAM-SHA-256 is not supported\",\"code\":8000,\"codeName\":\"AtlasError\"}"
manyuanrong commented 3 years ago

Should be SCRAM-SHA-1, not SCRAM_SHA_1

RNR1 commented 3 years ago

Thanks! it worked. I will close this issue, but I would recommend referencing this param on the examples because right now, the connect string option hints that it's more compatible with local instances and connecting to an Atlas cluster is a common use case.