Sunday, July 29, 2018

Password flag doesn't work to log into mongo

Leave a Comment

I created a mongo daemon. Then, I did:

$ mongo --port 27017  > use admin > db.createUser({            user: "AzureDiamond",             pwd: "hunter2",           roles: [{                role: "readWrite",                  db: "test_db1"           }]    }) >^D 

Then I tried to log into Mongo with the new account (exactly as in section 7 of Mongo's utorial):

$ mongo --port 27017 -u "AzureDiamond" -p "hunter2" --authenticationDatabase "admin" 

This is the weird part. It still prompted me for my password, and then appended that to the database path that I connected to:

Enter password: connecting to: 127.0.0.1:27017/hunter2 > 

What did I do wrong? How can I connect to Mongo while supplying the password in the command line, but not having my password displayed on the screen?

4 Answers

Answers 1

I think that it is just a typo that you've pointed out in your question, Can you please retry the command as shown below:

You just missed a d in your username!

$ mongo --port 27017 -u "AzureDiamond" -p "hunter2" --authenticationDatabase "admin" 

If this doesn't work, can you please try logging in to Mongo database as per the documentation provided here.

Hope this helps!

Answers 2

This will solve your problem:

mongo admin -u {username} -p '{password}' 

Ref: command line authentication of mongo fails

Answers 3

Did you try using the connection string URI?

e.g. mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

So in your case, mongo mongodb://AzureDiamond:hunter2@localhost:27017/test_db1?authSource=admin

Answers 4

Easiest and Most Secure way:


You don't need to specify the port, you can do it easily using:

mongo admin -u ""AzureDiamond"" -p 

Now it will prompt you for the password, enter your password and Voila!! You are logged in!

This is the most secure way, as your password is not visible to others as well.

Hope this works for you! Let me know if this doesn't work!

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment