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


当前回答

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

编辑mongo设置文件;

sudo nano /etc/mongod.conf

加一行:

security.authorization : enabled

重新启动服务

sudo service mongod restart

问候

其他回答

连接mongoDB的最佳实践如下:

初始安装完成后, 使用管理 然后执行以下脚本创建admin用户

    db.createUser(
     {
         user: "YourUserName",
         pwd: "YourPassword",
         roles: [
                   { role: "userAdminAnyDatabase", db: "admin" },
                   { role: "readWriteAnyDatabase", db: "admin" },
                   { role: "dbAdminAnyDatabase", db: "admin" },
                   { role: "clusterAdmin", db: "admin" }
                ]
     })

下面的脚本将为DB创建admin用户。

登录到db。管理使用 mongo -u YourUserName -p YourPassword admin 登录后,重复1 ~ 3可创建N个具有相同或不同管理凭证的数据库。

这允许您为在MongoDB中创建的不同集合创建不同的用户和密码

首先在终端上运行mongoDB

mongod

现在运行mongo shell use下面的命令

    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/

这些步骤对我很有效:

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 20.04和mongodb enterprise 4.4.2所做的:

在终端中输入mongo启动mongo shell。 使用管理数据库:

use admin

创建一个新用户并分配你想要的角色:

use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: passwordPrompt(), // or cleartext password
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
  }
)

退出mongo,在etc/mongo .conf中添加如下一行:

security:
    authorization: enabled

重启mongodb服务器

(可选)6。如果你想让你的用户拥有root权限,你可以在创建用户时指定:

db.createUser(
  {
    user: "myUserAdmin",
    pwd: passwordPrompt(), // or cleartext password
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
  }
)

或者您可以使用以下命令更改用户角色:

db.grantRolesToUser('admin', [{ role: 'root', db: 'admin' }])

创建新用户后,请不要忘记授予该用户读/写/root权限。你可以试试

cmd: db。grantRolesToUser('yourNewUsername',[{role: "root", db: "admin"}])