English 中文(简体)
我怎样才能用脚本来创建蒙戈巴的用户?
原标题:How can I use a script to create users in mongodb?

我正在建立蒙戈布 我们用木偶来控制服务器的配置

我已经安排了大多数我需要的东西, 但我需要在蒙戈数据库里创建用户。

我知道如何用蒙戈贝壳来做这件事, 我知道,我可以用 使用喷射针 / a.js 使用命令来做这件事。

db.addUser("username", "password"[, readOnly])

然而,我一直无法找到一个坚实的例子来证明需要用刺绣来做这件事。 更重要的是,我需要能够从命令行中添加一个用户,使用某种贝壳脚本。

如果有人能

(a) 指出一些与蒙哥华数据库和海洋数据库一起使用笔记本的确凿例子;

(b) 我如何从指挥线上做到这一点?

最佳回答

Mongo s cli 告诉您自己如何使用 js 文件

$ mongo --help
MongoDB shell version: 2.0.3
usage: mongo [options] [db address] [file names (ending in .js)]
...

<强 > usage:mongo [选项][db地址] [文件名称(以.js结尾)]

例如:

$ echo  db.addUser("guest", "passwordForGuest", true);  > file.js
$ mongo mydb file.js
MongoDB shell version: 2.0.3
connecting to: mydb
{ "n" : 0, "connectionId" : 1, "err" : null, "ok" : 1 }
{
    "user" : "guest",
    "readOnly" : true,
    "pwd" : "b90ba46d452e5b5ecec64cb64ac5fd90",
    "_id" : ObjectId("4fbea2b013aacb728754fe10")
}

Udpate:
db.addUser deprecated since 2.6
https://docs.mongodb.com/v2.6/reference/method/db.addUser/

使用 db. createUser 代替:

// file.js
db.createUser(
  {
    user: "guest",
    pwd: "passwordForGuest",
    roles: [ { role: "read", db: "mydb" } ]
  }
)

$mongo mydb 文件.js

问题回答

以下是更简单和优雅的解决办法:

echo  db.addUser("<username>", "<password>");  | mongo <database>

使用MongoDB测试 2.4.8

I know AD7six has already given an issue but when I try, I obtain a user with a database authentication test .
So I added one command before create User to choose this authentication database.

db = db.getSiblingDB( myDataBase );
db.createUser(
 {
   user: "myUser",
   pwd: "myPwd",
   roles: [
    { role: "readWrite", db: "myDataBase" }
   ]
 }
);

The authentication database and the database used by the user can be different. The command "getSiblingDB(dataBase)" (Javascript) is an alternative to "use dataBase" (Shell)





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签