MongoError: not authorized on to execute command { find: "app_updates", filter: { key: "0.0.1-admins" }, limit: 1, batchSize: 1, singleBatch: true }

MongoError: not authorized on to execute command { find: "app_updates", filter: { key: "0.0.1-admins" }, limit: 1, batchSize: 1, singleBatch: true }

我在 this method 之后开始了一个 KeystoneJS 项目。但是进入项目根目录后 运行 node keystone.js 我收到错误:

------------------------------------------------
An error occurred applying updates, bailing on Keystone init.

Error details:
MongoError: not authorized on <KeystoneJS project name> to execute command { find: "app_updates", filter: { key: "0.0.1-admins" }, limit: 1, batchSize: 1, singleBatch: true }

我研究过这个错误,但无法解决。我在 OpenSUSE 上,正在使用 nodejs8 包。


当我通过 运行 在 systemd 上启动 mongodb 时:

$ sudo systemctl start mongodb.service

...我收到上述错误。


但是当我通过 运行 启动 mongodb 时:

$ sudo mongod

...我没有收到任何错误并且 keystonejs 工作正常,我不确定为什么!


当我通过 运行 $ sudo mongod -f /etc/mongodb.conf 启动 mongodb 时,我收到上述错误。但是当我通过 运行 $ sudo mongod 启动 mongodb 时,我没有收到任何错误。因此,看起来问题的原因是在 /etc/mongodb.conf 文件中,如下所示:

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  #dbPath: /data/db/
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# Where and how to log messages.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
  logRotate: reopen

# What type of connections to allow.
net:
  port: 27017
  bindIp: 127.0.0.1
  ipv6: true
  http:
    enabled: false
    JSONPEnabled: false
    RESTInterfaceEnabled: false


# How to manage mongod.
processManagement:
  fork: true

# Security settings.
security:
  authorization: enabled

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

在您的配置中,您使用授权访问 Mongo

# Security settings.
security:
  authorization: enabled 

首先你需要创建用户 - 打开mongo控制台(mongo) 在没有配置的情况下启动 mongod

mongod --port 27017 --dbpath /data/db1

使用控制台连接到实例

mongo --port 27017

在 mongo 控制台中输入下一个命令

use admin
db.createUser(
  {
    user: "superAdmin",
    pwd: "admin123",
    roles: [ { role: "root", db: "admin" } ]
  })

现在您可以访问 mongo,将其作为服务与您的配置一起使用

mongo --port 27017 -u "superAdmin" -p "admin123" --authenticationDatabase "admin"

因此您需要通过创建具有读写权限

的新用户来授予对 mongo 的访问权限
use myAppDb #its name of your Database
db.createUser(
  {
   user: "myAppDbUser", #desired username
   pwd: "myApp123", #desired password
   roles: [ "readWrite"]
  })

并尝试使用此凭据进行连接 mongo --port 27017 -u "myAppDbUser" -p "myApp123" --authenticationDatabase "myAppDb"

并使用此用户进行 keystone 配置以连接到 mongo,例如

mongodb://myAppDbUser:myApp123@localhost:27017/myAppDb

(通过 https://medium.com/@raj_adroit/mongodb-enable-authentication-enable-access-control-e8a75a26d332