Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to connect to mongodb Atlas #319

Open
playerx opened this issue Dec 18, 2021 · 7 comments
Open

Unable to connect to mongodb Atlas #319

playerx opened this issue Dec 18, 2021 · 7 comments
Labels
atlas Everything About MongoDB Atlas

Comments

@playerx
Copy link

playerx commented Dec 18, 2021

Hey, it works with local db but fails with mongodb atlas connection string, I'm using the syntax like this:

await client.connect(
    "mongodb+srv://user:password@name.mongodb.net/db?authMechanism=SCRAM-SHA-1"
);

// also tried this

await client.connect(
    "mongodb+srv://user:password@name.mongodb.net/db?retryWrites=true&w=majority&authMechanism=SCRAM-SHA-1"
);

// and this

await client.connect(
    "mongodb+srv://user:password@name.mongodb.net/db?retryWrites=true&w=majority"
);

It always fails here: https://deno.land/x/mongo@v0.29.0/src/client.ts#L41
with error:

{
"ok":0,
"errmsg":"Authentication failed.",
"code":18,
"codeName":"AuthenticationFailed",
"$clusterTime":{"clusterTime":{"$timestamp":"7043035952079437828"},"signature":{"hash":"z8hkCMby/OYpTnJXHNS7+rZfBvI=","keyId":{"high":1625418439,"low":1,"unsigned":false}}},
"operationTime":{"$timestamp":"7043035952079437828"}
}

Can you share if it's a known issue or do I need to provide some additional params?

@erfanium
Copy link
Member

erfanium commented Dec 19, 2021

https://github.com/erfanium/deno-deploy-mongo
this demo uses MongoDB Atlas for its database with no problem. do you use any non-default options when you creating a database?

This issue needs a reproduction steps for me

@playerx
Copy link
Author

playerx commented Dec 21, 2021

I've a simple script like this:

import { MongoClient } from "https://deno.land/x/mongo@v0.29.0/mod.ts";

const client = new MongoClient();

const db = await client.connect(
  "mongodb+srv://USER:PASSWORD@jok-prod.RANDOM.mongodb.net/jok?authMechanism=SCRAM-SHA-1"
);

const names = await db.listCollectionNames();

console.log(names);

and it fails, I've tried the same script on different mongo atlas clusters and had the same error.

Can you share the structure of the connection string you are using? Maybe I miss something there

@playerx
Copy link
Author

playerx commented Dec 21, 2021

Ah it seems the database name in the connection string isn't supported for now. It started working once I've updated connection string like this:

 "mongodb+srv://USER:PASSWORD@jok-prod.RANDOM.mongodb.net/?authMechanism=SCRAM-SHA-1"

@erfanium
Copy link
Member

are you sure? maybe you should use authSource property

@playerx
Copy link
Author

playerx commented Dec 21, 2021

I've tried calling parse function for the connectionString I was using and here is the result:

Input:

 "mongodb+srv://USER:PASSWORD@jok-prod.RANDOM.mongodb.net/jok?authMechanism=SCRAM-SHA-1"

Output:

{
  servers: [
    { host: "jok-prod-shard-00-01.RAND.mongodb.net.", port: 27017 },
    { host: "jok-prod-shard-00-02.RAND.mongodb.net.", port: 27017 },
    { host: "jok-prod-shard-00-00.RAND.mongodb.net.", port: 27017 }
  ],
  authSource: "admin",
  replicaSet: "atlas-132kf5-shard-0",
  db: "jok",
  credential: {
    username: "USER",
    password: "PASSWORD",
    db: "jok",
    mechanism: "SCRAM-SHA-1"
  },
  compression: [],
  tls: true,
  retryWrites: true
}

As you can see it changed credentials database as well and that's the issue here I think

@erfanium
Copy link
Member

erfanium commented Dec 21, 2021

i think that's an expected behavior, just use something like:
mongodb+srv://USER:PASSWORD@jok-prod.RANDOM.mongodb.net/jok?authMechanism=SCRAM-SHA-1&authSource=admin

@playerx
Copy link
Author

playerx commented Dec 21, 2021

The reason why I think it's an issue is that the connection string I used works properly with nodejs mongo (official) driver and many users will use the same connections string and had the same issue.

I think credential.db should not be changed unless there will be provided authSource explicitly.

@lucsoft lucsoft added the atlas Everything About MongoDB Atlas label Apr 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
atlas Everything About MongoDB Atlas
Projects
None yet
Development

No branches or pull requests

3 participants