从命令行创建 MongoDB 用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22682891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a MongoDB user from commandline
提问by Golo Roden
I have set up a MongoDB database with an admin
user that has only administrative rights, not read or write access to databases.
我已经设置了一个 MongoDB 数据库,admin
用户只具有管理权限,没有对数据库的读或写访问权限。
What I now would want to do is:
我现在想做的是:
- Add a new database,
- and add a new user to that database.
- 添加一个新的数据库,
- 并向该数据库添加一个新用户。
And: I need to do this from the command line. So I tried:
并且:我需要从命令行执行此操作。所以我试过:
$ mongo admin -u admin -p admin --eval "db.addUser('dummyuser', 'dummysecret')"
(Please note that as I am still running MongoDB 2.0, I am using the old format of db.addUser
.)
(请注意,由于我仍在运行 MongoDB 2.0,我使用的是旧格式db.addUser
。)
This would make perfect sense if I could also tell it which database this user should be for. But now I am struggling. How do I specify the database for this command? If I were in the interactive shell, I could just run
如果我还可以告诉它这个用户应该使用哪个数据库,这将是非常有意义的。但现在我正在挣扎。如何为此命令指定数据库?如果我在交互式 shell 中,我就可以运行
> use dummydb
but how do I do this from the command line? My first try was to concatenate both commands with a ;
, but this didn't work:
但是如何从命令行执行此操作?我的第一次尝试是将两个命令与 a 连接;
,但这不起作用:
$ mongo admin -u admin -p admin --eval "use dummydb;db.addUser('dummyuser', 'dummysecret')"
just gives me syntax error.
只是给我语法错误。
How do I get this right?
我该如何做对?
回答by Stennie
The use db
syntax is only supported in an interactive shell session.
该use db
语法仅在交互式 shell 会话中受支持。
If you want to change databases from an admin script, you can use db.getSiblingDB('dbname')
.
如果要从管理脚本更改数据库,可以使用db.getSiblingDB('dbname')
.
So your equivalent command line using --eval
would be:
所以你使用的等效命令行--eval
是:
# MongoDB 2.6+: use createUser()
$ mongo admin -u admin -p admin --eval "db.getSiblingDB('dummydb').createUser({user: 'dummyuser', pwd: 'dummysecret', roles: ['readWrite']})"
# MongoDB 2.4: use addUser()
$ mongo admin -u admin -p admin --eval "db.getSiblingDB('dummydb').addUser('dummyuser', 'dummysecret')"
There is a section in the MongoDB manual covering Differences between interactive and scripted mongo
. This includes equivalents for other interactive shell helpers such as show dbs
and show collections
.
MongoDB 手册中有一节介绍了 Interactive 和 scripted 之间的差异mongo
。这包括其他交互式 shell 助手的等效项,例如show dbs
和show collections
。
回答by Golo Roden
Solved it by putting the commands
通过放置命令解决它
use dummydb
db.addUser('dummyuser', 'dummysecret')
into a .js
file, and then ran MongoDB by calling:
进入一个.js
文件,然后通过调用运行 MongoDB:
$ mongo admin -u admin -p admin < setupMongoDB.js
That's it :-)
就是这样 :-)
回答by Hamit YILDIRIM
Above in my case didn't worked
以上在我的情况下没有用
use dumyDb
db.createUser({user: "admin",pwd: "secrectP@wd",roles: [ { role: "readWrite", db: "reporting" } ],mechanisms: [ "SCRAM-SHA-256" ] }