繁体   English   中英

MongoDB:服务器有启动警告“未为数据库启用访问控制”

[英]MongoDB: Server has startup warnings ''Access control is not enabled for the database''

我今天首先安装了 MongoDB 3.4.1。 但是当我启动它并使用 MongoDB shell 时,它给了我以下这些警告:

C:\Users\hs>"C:\Program Files\MongoDB\Server\3.4\bin\mongo.exe
MongoDB shell version v3.4.1
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.1
Server has startup warnings:
2017-01-12T21:19:46.941+0800 I CONTROL  [initandlisten]
2017-01-12T21:19:46.942+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-01-12T21:19:46.942+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-01-12T21:19:46.942+0800 I CONTROL  [initandlisten]

我的电脑是 Microsoft Windows [ version 10.0.14393 ]。

MongoDB v3.4

您需要执行以下操作来创建安全数据库:

确保启动该进程的用户具有权限并且目录存在(在本例中为/data/db )。

1)在没有访问控制的情况下启动MongoDB。

mongod --port 27017 --dbpath /data/db

2) 连接到实例。

mongo --port 27017

3)创建用户管理员(在admin认证数据库中)

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

4) 用访问控制重启MongoDB实例。

mongod --auth --port 27017 --dbpath /data/db

5) 以用户管理员身份进行连接和验证。

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

6) 根据您的部署的需要(例如在测试认证数据库中)创建额外的用户。

use test
db.createUser(
  {
    user: "myTester",
    pwd: "xyz123",
    roles: [ { role: "readWrite", db: "test" },
             { role: "read", db: "reporting" } ]
  }
)

7) 以 myTester 身份连接并进行身份验证。

mongo --port 27017 -u "myTester" -p "xyz123" --authenticationDatabase "test"

我基本上只是在这里解释了官方文档的简短版本:https ://docs.mongodb.com/master/tutorial/enable-authentication/

天哪,多么好的天然气厂,最佳答案!

您需要做的就是:

  1. 编辑您的配置,例如C:\Program Files\MongoDB\Server\4.4\bin\mongodb.cfg
  2. security: authorization:设置为enabled ,如图; 请注意,此子条目可能会完全丢失。 然后添加它。
  3. 从 Windows服务控制面板重新启动MongoDB Server服务。

在此处输入图像描述

显然,如果在此之后设置基于读/读/写角色的策略,那将更有意义。

参考: https://docs.mongodb.com/manual/tutorial/configure-scram-client-authentication/

我刚刚使用 phpunit 对此进行了测试,按预期工作。

您可以创建管理员用户或其他角色 在 mongo shell 上运行它。

db.createUser({user: "username", pwd: "password", roles: ["dbAdmin"]})

如果您收到 SCRAM-SHA-256 错误,您可以设置 SHA-1 机制。

db.createUser({user: "username", pwd: "password", roles: ["dbAdmin"], mechanisms: ["SCRAM-SHA-1"]})

您需要删除旧的 db 文件夹并重新创建新文件夹。 它将解决您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM