13

I am trying the Mongo Atlas Cloud. I create a cluster and i am trying a connection with the mongo shell: (same problem with mongo drivers)

mongo mongodb://***-cluster-shard-00-00-***.mongodb.net:27017,***-cluster-shard-00-01-***.mongodb.net:27017,***-cluster-shard-00-02-***.mongodb.net:27017/any_database?replicaSet=****-Cluster-shard-0 --ssl --username ***** --password *****

this is the connection string in the documentation. And this is the error:

MongoDB shell version: 3.2.7
connecting to: mongodb://***-cluster-shard-00-00-***.mongodb.net:27017,***-cluster-shard-00-01-***.mongodb.net:27017,***-cluster-shard-00-02-***.mongodb.net:27017/any_database?replicaSet=***-Cluster-shard-0
2016-07-07T01:31:17.535-0300 I NETWORK  [thread1] Starting new replica set monitor for ***-Cluster-shard-0/***-cluster-shard-00-00-***.mongodb.net:27017,***-cluster-shard-00-01-***.mongodb.net:27017,***-cluster-shard-00-02-***.mongodb.net:27017
2016-07-07T01:31:17.535-0300 I NETWORK  [ReplicaSetMonitorWatcher] starting
2016-07-07T01:31:20.084-0300 E QUERY    [thread1] Error: Authentication failed. :
DB.prototype._authOrThrow@src/mongo/shell/db.js:1441:20
@(auth):6:1
@(auth):1:2

exception: login failed

I can connect to the database only when i use admin database "/admin?" in the connection string.

THE PROBLEM:

I need to connect to a custom database with the console or mongo drivers.

PD: i protect my data with "***"

4 Answers 4

23

You authenticate with the admin database. From there you can switch the database.

When connecting using Mongo drivers, your connection string needs add the auth source:

/any_database?authSource=admin&replicaSet=xyz
2
  • thank you, mongo atlas support gives me same answer. Jul 13, 2016 at 18:58
  • 1
    I've visited many SO questions on this topic and this was the key. Just add the query string ?authSource=admin to your connection string Jan 13, 2021 at 21:54
9

Adding to Nath's answer, use "--authenticationDatabase admin" if your connecting via the mongo shell and use "authSource=admin" for drivers.

Tested with [MongoDB shell version: 3.2.8]

0
3

"authSource=admin" Is a requirement for All Atlas connections.

0

I also faced the same issue. "admin" user would be created at the time of db creation. If you forgot the password for the default admin user, then it is difficult to connect with the database and access the collections.

You could create one more database user under "Security->Database Access" section and select "Atlas admin" as a role.

If your password contains any special character, then this would be encrypted internally. Hence, use this link (https://www.urlencoder.org/) to encrypt your password (plain text) and use the encrypted string to establish a connection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.