10

When I try to connect to Mongo instance using this connection string

mongodb://root:password@localhost:27017/

everything works, however, when I try to specify the database name in the connection string

i.e mongodb://root:password@localhost:27017/storefont

I get the following error MongoDB Connection Error: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoError: Authentication failed.

2 Answers 2

24

Specify the authentication database like this:

mongodb://root:password@localhost:27017/storefont?authSource=admin

If you specify a database then this database is also taken for authentication by default. The MongoDB documentation is not 100% clear in that topic.

See also Authentication failure while trying to save to mongodb

Sign up to request clarification or add additional context in comments.

1 Comment

I was missing "?authSource=admin", been googling for hours. :D sent an up arrow.
0

The answer provided by Wernfield is correct. I am providing a small tweak along with an example to connect to a replica set:

When using the authSource query param, please ensure that the connection string is wrapped in single quotes OR just use --authenticationDatabase argument instead,

mongo mongodb://ip1:27017,ip2:27017,ip3:27017/my-db --authenticationDatabase admin -u myUsername -p myPassword

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.