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

user3405291 picture user3405291 · Nov 6, 2017 · Viewed 14.1k times · Source

I start a KeystoneJS project following this method. But after entering the project root directory and running node keystone.js I receive the error:

------------------------------------------------
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 }

I researched on this error but I couldn't resolve it. I'm on OpenSUSE, and was working with nodejs8 package.


When I start the mongodb on systemd by running:

$ sudo systemctl start mongodb.service

... I receive the above error.


But when I start the mongodb by running:

$ sudo mongod

... I don't receive any error and keystonejs works fine, I'm not sure why!


When I start mongodb by running $ sudo mongod -f /etc/mongodb.conf I receive the above error. But When I start mongodb by running $ sudo mongod, I don't receive any error. Therefore, looks like the problem cause is within the /etc/mongodb.conf file which is as follows:

# 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:

Answer

ByteMaster picture ByteMaster · Dec 25, 2017

In your config you're using authorization for accessing Mongo

# Security settings.
security:
  authorization: enabled 

At first you need to create user - open mongo console (mongo) Start mongod without config

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

connect to the instance using console

mongo --port 27017

in mongo console enter next commands

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

Now you can access mongo using as a service with your config

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

So you need to grant access to mongo by creating new user with ReadWrite permission

use myAppDb #its name of your Database
db.createUser(
  {
   user: "myAppDbUser", #desired username
   pwd: "myApp123", #desired password
   roles: [ "readWrite"]
  })

And try to connect using this credentials mongo --port 27017 -u "myAppDbUser" -p "myApp123" --authenticationDatabase "myAppDb"

and use this user for keystone config to connect to mongo, for example

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

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