如何使用用户名和密码保护 MongoDB

我想为我的 MongoDB 实例设置用户名和密码身份验证,以便任何远程访问都会询问用户名和密码。我尝试了 MongoDB 网站上的教程并做了以下操作:

use admin
db.addUser('theadmin', '12345');
db.auth('theadmin','12345');

之后,我退出并再次运行 mongo。而且我不需要密码来访问它。即使我远程连接到数据库,也不会提示我输入用户名和密码。


更新这是我最终使用的解决方案

1) At the mongo command line, set the administrator:

    use admin;
    db.addUser('admin','123456');

2) Shutdown the server and exit

    db.shutdownServer();
    exit

3) Restart mongod with --auth

  $ sudo ./mongodb/bin/mongod --auth --dbpath /mnt/db/

4) Run mongo again in 2 ways:

   i) run mongo first then login:

        $ ./mongodb/bin/mongo localhost:27017
        use admin
        db.auth('admin','123456');

  ii) run & login to mongo in command line.

        $ ./mongodb/bin/mongo localhost:27017/admin -u admin -p 123456

mongodumpmongoexport 的用户名和密码的工作方式相同。

stack overflow How to secure MongoDB with username and password
原文答案
author avatar

接受的答案

设置用户后,您需要使用 mongod 选项启动 --auth

从 MongoDB 站点:

使用 --auth 选项运行数据库(mongod 进程)以启用安全性。您必须在使用 --auth 启动服务器之前将用户添加到 admin db,或者从 localhost 界面添加第一个用户。

MongoDB Authentication


答案:

作者头像

哇,这里有很多复杂/令人困惑的答案。

这是 v3.4

简短的答案。

1.在没有访问控制的情况下启动mongoDB。

Mongod -DBPATH /DATA /DB

2.连接到实例。

蒙哥

3.创建用户。

使用Some_db db.createuser({user:“ mynormaluser”,pwd:“ xyz123”,角色: [ { role: "readWrite", db: "some_db" }, { role: "read", db: "some_other_db" } ] } )
  1. Stop the MongoDB instance and start it again with access control.

    mongod --auth --dbpath /data/db

  2. Connect and authenticate as the user.

    use some_db db.auth("myNormalUser", "xyz123") db.foo.insert({x:1}) use some_other_db db.foo.find({})

Long answer: Read this if you want to properly understand.

It's really simple. I'll dumb the following down https://docs.mongodb.com/manual/tutorial/enable-authentication/

If you want to learn more about what the roles actually do read more here: [https://docs.mongodb.com/manual/reference/built-in-roles/](https://docs.mongodb.com/manual/reference/built-in-roles/)

1.在没有访问控制的情况下启动mongoDB。

Mongod -DBPATH /DATA /DB

2.连接到实例。

蒙哥

3.创建用户管理员。 The following creates a user administrator in the admin authentication database. The user is a dbowner over the some_db database and NOT over the admin database, this is important to remember.

使用Admin db.Createuser({用户:“ mydbowner”,pwd:“ abc123”,角色: [ { role: "dbOwner", db: "some_db" } ] } )

Or if you want to create an admin which is admin over any database:

use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: "abc123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)
  1. Stop the MongoDB instance and start it again with access control.

    mongod --auth --dbpath /data/db

  2. Connect and authenticate as the user administrator towards the admin authentication database, NOT towards the some_db authentication database. The user administrator was created in theadminauthentication database, the user does not exist in thesome_dbauthentication database.

    use admin db.auth("myDbOwner", "abc123")

You are now authenticated as adbOwnerover thesome_dbdatabase. So now if you wish to read/write/do stuff directly towards thesome_dbdatabase you can change to it.

use some_db
//...do stuff like db.foo.insert({x:1})
// remember that the user administrator had dbOwner rights so the user may write/read, if you create a user with userAdmin they will not be able to read/write for example.

More on roles: https://docs.mongodb.com/manual/reference/built-in-roles/

If you wish to make additional users which aren't user administrators and which are just normal users continue reading below.

6.创建普通用户。 This user will be created in the some_db authentication database down below.

使用Some_db db.createuser({user:“ mynormaluser”,pwd:“ xyz123”,角色:[{realwrite:“ readwrite”,db:“ some_db”},{proam {real:“ read:read”,db:db:db:“ some_other_db”}] })

7.退出Mongo Shell,重新连接,作为用户身份验证。

使用some_db db.auth(“ mynormaluser”,“ xyz123”)db.foo.insert({x:1})使用some_other_dother_db db.foo.fine({})

最后但并非最不重要的是,由于用户未正确发布有关 --auth 标志的命令,如果您不希望将其设置为标志,则可以在MongoDB的配置文件中设置此值。

作者头像

首先,取消注释 mongod 配置文件中以 #auth=true 开头的行(默认路径 /etc/mongod.conf )。这将为 mongodb 启用身份验证。

然后,重启 mongodb: sudo service mongod restart

作者头像

此答案适用于 Mongo 3.2.1 Reference

1号航站楼:

$ mongod --auth

2号航站楼:

db.createUser({user:"admin_name", pwd:"1234",roles:["readWrite","dbAdmin"]})

如果你想添加没有角色(可选):

db.createUser({user:"admin_name", pwd:"1234", roles:[]})

检查是否经过身份验证:

db.auth("admin_name", "1234")

它应该给你:

1

别的 :

Error: Authentication failed.
0
作者头像

这是添加用户的javascript代码。

  1. mongod 开始 --auth = true

  2. 从 mongo shell 访问 admin 数据库并传递 javascript 文件。

    蒙戈管理员“文件名.js”

    “文件名.js”

    // Adding admin user
    db.addUser("admin_username", " admin_password");
    // Authenticate admin user
    db.auth("admin_username ", " admin_password ");
    // use  database code from java script
    db = db.getSiblingDB("newDatabase");
    // Adding newDatabase database user  
    db.addUser("database_username ", " database_ password ");
    

3.现在用户添加完成,我们可以验证从mongo shell访问数据库

作者头像

https://docs.mongodb.com/manual/reference/configuration-options/#security.authorization

编辑mongo设置文件;

sudo nano /etc/mongod.conf

添加行:

security.authorization : enabled

重启服务

sudo service mongod restart

问候

作者头像

首先使用在终端上运行 mongoDB

mongod

现在运行 mongo shell 使用以下命令

    use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: "abc123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

使用访问控制重新启动 MongoDB 实例。

mongod --auth

现在使用命令行从命令行验证自己

mongo --port 27017 -u "myUserAdmin" -p "abc123" --authenticationDatabase "admin"

我从

https://docs.mongodb.com/manual/tutorial/enable-authentication/

作者头像

您可以更改 /etc/mongod.conf

#security:

security:
    authorization: "enabled"

那么 sudo service mongod restart

作者头像

这是我在 Ubuntu 18.04 上所做的:

$ sudo apt install mongodb
$ mongo
> show dbs
> use admin
> db.createUser({  user: "root",  pwd: "rootpw",  roles: [ "root" ]  })  // root user can do anything
> use lefa
> db.lefa.save( {name:"test"} )
> db.lefa.find()
> show dbs
> db.createUser({  user: "lefa",  pwd: "lefapw",  roles: [ { role: "dbOwner", db: "lefa" } ]  }) // admin of a db
> exit
$ sudo vim /etc/mongodb.conf
auth = true
$ sudo systemctl restart mongodb
$ mongo -u "root" -p "rootpw" --authenticationDatabase  "admin"
> use admin
> exit
$ mongo -u "lefa" -p "lefapw" --authenticationDatabase  "lefa"
> use lefa
> exit
作者头像

您需要切换到您希望用户使用的数据库(不是管理员数据库)...

use mydatabase

有关更多帮助,请参阅此帖子... https://web.archive.org/web/20140316031938/http://learnmongo.com/posts/quick-tip-mongodb-users/