4

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?

Sean Letendre
  • 2,623
  • 3
  • 14
  • 32
  • 1
    Are there any characters in your password which might be confusing things, for example a double-quote character or a backslash? – Vince Bowdren Jul 23 '18 at 08:07
  • What version of shell are you using ? Are you running shell on unix environment ? – s7vr Jul 23 '18 at 14:04
  • MongoDB shell version: 3.2.11. Running on Debian 9 – Sean Letendre Jul 23 '18 at 21:57
  • The `connecting to: 127.0.0.1:27017/hunter2` portion suggests that your mongo client is trying to connect to a database named 'hunter2'. This indicates that the shell is interpreting 'hunter2' as the database option, e.g. `mongo --port 1234 -u myuser -p mypass --authenticationDatabase admin hunter2`. Vince's suggestion of special characters in a username or password seems a likely candidate. You may also want to check your mongodb.log file for any auth failures. – Adam Harrison Jul 24 '18 at 00:18
  • [This](https://jira.mongodb.org/browse/SERVER-32421) seems related to your issue. Remove the space between the flag and password as suggested in the jira comments. – s7vr Jul 26 '18 at 02:41
  • Good question! +1 for it. Checkout my answer below. – Harshal Yeole Jul 29 '18 at 10:15

5 Answers5

0

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!

N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
0

This will solve your problem:

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

Ref: command line authentication of mongo fails

Savan Gadhiya
  • 305
  • 1
  • 6
0

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

Nic Cottrell
  • 9,401
  • 7
  • 53
  • 76
0

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!

Harshal Yeole
  • 4,812
  • 1
  • 21
  • 43
0

It's a bug in 3.2.11.

https://jira.mongodb.org/browse/SERVER-32421

Thank you for the report. After some testing, I'm able to reproduce this issue and am investigating the cause. However, as a workaround it appears that if you do not leave a space between the -p flag and the password, it logs in as expected

So try -u UserName -pPassword

Robbie
  • 17,605
  • 4
  • 35
  • 72