16

i am trying to connect to my mongo database with following connection string

var Mongo_url = 'mongodb://MyUSer:tech@localhost:27017/chatme?authSource=admin';  

I am getting error as

 assertionError: null == { [MongoError: Authentication failed.]
 name: 'MongoError',
 message: 'Authentication failed.',
 ok: 0,
 code: 18,

Could anyone please clear this "authSource=admin" thing to me.

gaurav malik
  • 356
  • 1
  • 2
  • 16

2 Answers2

18

This is the name of the database that has the collection with the user credentials.

https://docs.mongodb.com/manual/reference/connection-string/#connections-connection-options

This is where the usernames and passwords are set up.

Thilo
  • 241,635
  • 91
  • 474
  • 626
  • i have my users in system.users collection. So according to you 'authSource=admin' should be replace with authSource=users. right?? – gaurav malik Nov 15 '16 at 11:33
  • No, it's the name of the database, not the collection. And it defaults to the database you are trying to connect to. Does it work without the parameter? – Thilo Nov 15 '16 at 11:41
  • yes it works without parameter too. I got it now. Thanks – gaurav malik Nov 15 '16 at 11:50
1

I spent a few hours today trying to solve this problem and unfortunately, this SO post didn't appear in my searches. That was too bad because it's the post with the best answer to the problem.

I hope to help the next person by adding the error message I got.

I'm setting up the connection inside a Docker container that is spun up via a docker-compose yaml file. The user name and password are passed into docker-compose from a .env file.

Connection URI that works

mongodb://aUser:aPassword@mevn-app-mongo:27017/myDb?authSource=admin

Error:

SASL SCRAM-SHA-1 authentication failed for aUser on myDb from client 172.25.0.3:37352 ; UserNotFound: Could not find user aUser@myDb
Bryan
  • 973
  • 9
  • 15
  • 1
    I got this error when I was using NON-URL-ENCODED Connection SRV String – Hammad Apr 14 '20 at 11:43
  • In my case, I was trying to login to a specific database as "root", but authentication was failing. Adding "authSource=admin" to the end of the connection string fixed it, thanks! – Big Sam Mar 05 '21 at 00:45