authentication - 如何使用用户名和密码保护 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 的用户名和密码的工作方式相同。

最佳答案

你需要在设置用户后使用--auth选项启动mongod

来自 MongoDB 站点:

Run the database (mongod process) with the --auth option to enable security. You must either have added a user to the admin db before starting the server with --auth, or add the first user from the localhost interface.

MongoDB Authentication

https://stackoverflow.com/questions/4881208/

相关文章:

mongodb - 如何在 Mongo 中查询 "is not null"?

mongodb - 在 MongoDB 中实现数据版本控制的方法

mongodb - 向 MongoDB 集合中的每个文档添加新字段

mongodb - 使用另一个字段的值更新 MongoDB 字段

mongodb - 仅检索 MongoDB 集合中对象数组中的查询元素

mongodb - 查找两个日期之间的对象 MongoDB

mongodb - 获取集合中所有键的名称

mongodb - 如何导出 MongoDB 中的所有集合?

javascript - 如何在 Mongoose 中更新/插入文档?

mongodb - 如何从 MongoDB 获取随机记录?