I’m using Celery with MongoDB as the result backend, but I’m encountering an authentication issue when running Celery tasks. The error message I’m seeing is:
pymongo.errors.OperationFailure: Authentication failed., full error: {'ok': 0.0, 'errmsg': 'Authentication failed.', 'code': 18, 'codeName': 'AuthenticationFailed'}
However, I can successfully connect to MongoDB using the same credentials in a standalone script:
from pymongo import MongoClient
client = MongoClient('mongodb://admin:[email protected]/celery_db')
try:
client.admin.command('ismaster')
print("MongoDB connection successful")
except Exception as e:
print(f"MongoDB connection error: {e}")
This script outputs “MongoDB connection successful”, confirming that the credentials and connection string are correct.
Here is my celery.py
configuration:
from celery import Celery
app = Celery('tasks',
broker='amqp://admin:[email protected]:5672//',
backend='mongodb://admin:[email protected]/celery_db')
app.conf.update(
task_serializer='json',
accept_content=['json'],
result_serializer='json',
timezone='Europe/Oslo',
enable_utc=True,
)
@app.task
def add(x, y):
return x + y
I run the Celery worker with the following command:
celery -A celery worker -l info
What could be causing this discrepancy between the standalone script and the Celery worker’s ability to authenticate with MongoDB, and how can I resolve it?
- Using MongoDB as Celery Result Backend:
- Configured Celery to use MongoDB as the result backend.
- Encountered an authentication error.
- Standalone MongoDB Connection:
- Ran a standalone script to connect to MongoDB using the same connection string and credentials.
- The connection was successful, indicating the credentials and connection string are correct.
I expected the Celery worker to connect to MongoDB successfully using the provided credentials and connection string, just as the standalone script does. However, the worker continues to fail with an authentication error.