MongoDB 3.2 - 如何从命令行传递密码?

时间:2017-12-19 15:16:41

标签: mongodb

我有一个似乎微不足道的问题。但它不是,至少不是我。

我希望能够登录到受密码保护的MongoDB(v3.2)实例,而无需手动输入密码(我想在控制台的连接字符串中传递它)。它似乎应该是这样的:

hostnamename:port/database -u someUser -p 'password'

但它没有按预期工作:(所以让我们在三种情况下获取战利品。

(1)连接密码提示 - 当我输入密码时,它按预期工作,我可以登录。这是控制台输出和日志:

# hostnamename:27017/admin -u root -p
MongoDB shell version: 3.2.11
Enter password:
connecting to: hostname:27017/admin
>

I NETWORK  [initandlisten] connection accepted from ip:port #232 (11 connections now open)
I ACCESS   [conn232] Successfully authenticated as principal root on admin
I NETWORK  [conn232] end connection ip:port (10 connections now open)

(2)尝试连接已经通过的密码。我仍然被要求输入密码。如果我不在控制台中输入它就会一直等待。

# hostnamename:27017/admin -u root -p 'password'
MongoDB shell version: 3.2.11
Enter password:

(no logs are generated until password is typed)

(3)尝试连接已经通过的密码。我仍然被要求输入密码。我输入它...并且发生了一些奇怪的事情。连接崩溃与'密码'值(然后在连接字符串中传递),但日志说用户已连接。

# hostnamename:27017/admin -u root -p 'something'
MongoDB shell version: 3.2.11
Enter password:
connecting to: hostname:27017/admin
E -        [main] file [something] doesn't exist
failed to load: something


I NETWORK  [initandlisten] connection accepted from ip:port #233 (11 connections now open)
I ACCESS   [conn233] Successfully authenticated as principal root on admin
I NETWORK  [conn233] end connection ip:port (10 connections now open)

有什么想法吗?提前谢谢!

4 个答案:

答案 0 :(得分:1)

我向Mongo的家伙报告了这个问题:https://jira.mongodb.org/browse/SERVER-32421,它被证实是一个错误。

同时,解决方法是不在-p标志和密码之间留出空格:

mongo hostname:port/database -u username -pPassword

答案 1 :(得分:0)

您是否尝试过:[username:password @] host1 [:port1] ??

例如root:password@hostnamename:27017

official documentation connection string

答案 2 :(得分:0)

如果您输入带引号的密码,请输入无引号。

https://docs.mongodb.com/v3.4/reference/program/mongo/#options

答案 3 :(得分:0)

您还可以在目录中使用连接信息创建.mongorc.js文件。然后,您可以使用您的选项键入mongo。这是一个例子:

set /?