1

I run MongoDB docker container on my local computer and when I try to connect to MongoDB container without specifying database name, it works fine.
With following command, I managed to connect to MongoDB.

$ mongo "mongodb://username@127.0.0.1:27017"

But when I try to connect with database name, it will return following error.

$ mongo "mongodb://username@127.0.0.1:27017/dbname"

MongoDB shell version v4.2.0
Enter password: 
connecting to: mongodb://127.0.0.1:27017/dbname?compressors=disabled&gssapiServiceName=mongodb
2021-02-25T00:26:09.511+0900 E  QUERY    [js] Error: Authentication failed. :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2021-02-25T00:26:09.515+0900 F  -        [main] exception: connect failed
2021-02-25T00:26:09.515+0900 E  -        [main] exiting with code 1

I thought it might be because database doesn't exist yet, so I tried with database which already exist but same result.

And this is my local environment.
MacOS version 11.2.1
MongoDB shell version v4.2.0
mongo official docker image version 4.0.4

prasad_
  • 12,755
  • 2
  • 24
  • 36

1 Answers1

0

Add your password right after the username string, if you dont have a password leave the username out of the connectionstring.

With password

mongodb://username:password@localhost:27017/dbname

Without password

mongodb://localhost:27017/dbname

floormind
  • 1,868
  • 5
  • 31
  • 85