我想为我的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

用户名和密码将以同样的方式工作在mongodb和mongoexport。


当前回答

Mongodb 4.4.13社区

1. 创建数据库用户

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

1.2验证工作

> db.auth('myUserAdmin','abc123')

< { ok: 1 }

如果失败了

> db.auth('myUserAdmin','amongus')

MongoServerError: Authentication failed.

2. 修改/etc/mongod.conf

nano /etc/mongod.conf

变化:

#security:

to:

security:
  authorization: enabled

3.重启mongod service

sudo service mongod restart

这对我来说很管用。

其他回答

很多重复的答案,但我认为他们忽略了一个重要的注意:

即使正确启用了身份验证,您也可以在没有用户名/密码的情况下连接到Mongo数据库!

但是,您只能执行无害的命令,如db.help()、db.getMongo()、db.listCommands()等。

$ mongo 
MongoDB shell version v4.4.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f662858b-8658-4e33-a735-120e3639c131") }
MongoDB server version: 4.4.3
mongos> db.getMongo()
connection to 127.0.0.1:27017
mongos> db
test
mongos> db.version()
4.4.3
mongos> db.runCommand({connectionStatus : 1})
{
        "authInfo" : {
                "authenticatedUsers" : [ ],
                "authenticatedUserRoles" : [ ]
        },
        "ok" : 1,
        "operationTime" : Timestamp(1618996970, 2),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1618996970, 2),
                "signature" : {
                        "hash" : BinData(0,"Kre9jvnJvsW+OVCl1QC+eKSBbbY="),
                        "keyId" : NumberLong("6944343118355365892")
                }
        }
}

这些步骤对我很有效:

write mongod --port 27017 on cmd then connect to mongo shell : mongo --port 27017 create the user admin : use admin db.createUser( { user: "myUserAdmin", pwd: "abc123", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } ) disconnect mongo shell restart the mongodb : mongod --auth --port 27017 start mongo shell : mongo --port 27017 -u "myUserAdmin" -p "abc123" --authenticationDatabase "admin" To authenticate after connecting, Connect the mongo shell to the mongod: mongo --port 27017 switch to the authentication database : use admin db.auth("myUserAdmin", "abc123"

这是我在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

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

编辑mongo设置文件;

sudo nano /etc/mongod.conf

加一行:

security.authorization : enabled

重新启动服务

sudo service mongod restart

问候

Mongodb 4.4.13社区

1. 创建数据库用户

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

1.2验证工作

> db.auth('myUserAdmin','abc123')

< { ok: 1 }

如果失败了

> db.auth('myUserAdmin','amongus')

MongoServerError: Authentication failed.

2. 修改/etc/mongod.conf

nano /etc/mongod.conf

变化:

#security:

to:

security:
  authorization: enabled

3.重启mongod service

sudo service mongod restart

这对我来说很管用。