【问题标题】:MongoError: not authorized on to execute command { find: "app_updates", filter: { key: "0.0.1-admins" }, limit: 1, batchSize: 1, singleBatch: true }MongoError: 未授权执行命令 { find: "app_updates", filter: { key: "0.0.1-admins" }, limit: 1, batchSize: 1, singleBatch: true }
【发布时间】:2018-04-18 05:29:01
【问题描述】:

我在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:

【问题讨论】:

    标签: mongodb opensuse keystonejs


    【解决方案1】:

    在您的配置中,您正在使用授权访问 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

    【讨论】:

    • 从 4.2 版开始,您可以甚至应该使用函数 passwordPrompt(),即类似这样的东西:db.createUser({user:"superuser", pwd:passwordPrompt(), roles:["root"]})。这被认为比输入屏幕上可见的密码更安全。请注意,密码提示仅显示一次。它不会要求您重新输入密码。如果您选择use admin,则不需要db: "admin"
    猜你喜欢
    • 2019-07-25
    • 2020-02-02
    • 1970-01-01
    • 2017-05-31
    • 2018-02-06
    • 2019-05-30
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    相关资源
    最近更新 更多