0

I have two databases (namely, test and celery). When a celery task is triggered, it will first update the "test" db with results and then update the "celery" db with success/failure message. I have secured these DBs with credentials and followed this method: https://medium.com/@matteocontrini/how-to-setup-auth-in-mongodb-3-0-properly-86b60aeef7e8

Now, when I run my task, it gives me this error:

pymongo.errors.OperationFailure: not authorized on celery to execute command { createIndexes: "celery_taskmeta", indexes: [ { key: { date_done: 1 }, background: "true", name: "date_done_1" } ] }

I am using: mongodb: 3.4 mongoengine: 0.16 django 2.1 and python3

mongoengine.connect(db="test",host=config.host,username="admin_1",
password="admin1234",authentication_source="admin")

CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = "mongodb"
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

Apart from the method mentioned in the link above, I have tried creating an admin user in admin DB like this:

use admin
db.createUser(
 {
   user: "admin_1",
   pwd: "admin1234",
   roles: [ 
{ 
role: "dbOwner",
db: "test" 
},

{ 
role: "userAdminAnyDatabase",
db: "admin" 
},

{ 
role: "dbOwner",
db: "celery" 
}
]
 }
)

It gives the same error!

7wick
  • 411
  • 4
  • 17

1 Answers1

0

The error comes from celery, it can't create an index on the collection because its not authenticated.

You are connecting mongoengine properly but in the snippet provided, you are not providing the username/password in the configuration of celery so it can't authenticate (mongoengine being a distinct library, it isn't sharing its connection implicitly with celery).

From what I see in the docs, you can provide the mongodb URI as followed:

CELERY_RESULT_BACKEND = "mongodb://{user}:{password}@127.0.0.1:27017/"

By default celery will use the database "celery" so you don't have to specify it

bagerard
  • 5,681
  • 3
  • 24
  • 48