The traceback indicates a pika.exceptions.ProbableAuthenticationError: ConnectionClosedByBroker: (403) 'ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.'. This means your RabbitMQ broker is refusing the connection because it's not configured to accept connections using the PLAIN authentication mechanism, or the credentials you're providing are incorrect.
Here's a breakdown of the problem and how to fix it, along with explanations:
Understanding the Error
ConnectionClosedByBroker: RabbitMQ actively closed the connection.
(403) ACCESS_REFUSED: This is a standard HTTP 403 Forbidden error, indicating that the client (your consumer.py script) doesn't have permission to access the resource (RabbitMQ).
Login was refused using authentication mechanism PLAIN: RabbitMQ is using the PLAIN authentication method, and your client is either not providing credentials or providing incorrect credentials.
For details see the broker logfile: The RabbitMQ broker's log file will contain more specific information about why the authentication failed.
Solutions
-
Authentication Configuration (Most Likely Solution)
-
RabbitMQ Configuration: The most common reason for this error is that your RabbitMQ server is configured to require authentication, and you haven't set up authentication correctly. You need to configure RabbitMQ to use a suitable authentication method (like PLAIN, SASL_PASSWORD, or TLS/SSL).
-
PLAIN Authentication (Simple, but less secure): If you intend to use PLAIN authentication, you need to provide a username and password when creating the connection. However, PLAIN is generally discouraged for production environments due to security concerns.
-
SASL_PASSWORD (Recommended): This is a more secure option. You'll need to configure RabbitMQ to use SASL_PASSWORD and provide a username and password in your client code.
-
TLS/SSL (Most Secure): This is the most secure option, encrypting all communication between the client and server. Requires configuring RabbitMQ for TLS/SSL and providing the necessary certificates in your client code.
-
Check RabbitMQ Broker Logs
-
The error message explicitly tells you to check the RabbitMQ broker log file. The location of the log file depends on your RabbitMQ installation and configuration. Common locations include:
/var/log/rabbitmq/rabbit@<hostname>.log (Linux)
C:\Program Files\RabbitMQ\log\rabbitmq.log (Windows)
-
The log file will provide more detailed information about the authentication failure, including the exact reason why the login was refused.
-
Verify Credentials (If using PLAIN or SASL_PASSWORD)
- If you're using
PLAIN or SASL_PASSWORD, double-check that the username and password you're providing are correct and that the user has the necessary permissions to access the queue.
Example Code (with PLAIN Authentication - Use with Caution)
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
try:
channel.basic_queue_declare(queue='task_queue', durable=True)
print("Queue 'task_queue' declared")
except pika.exceptions.ConfirmationError as e:
print(f"Error declaring queue: {e}")
connection.close()
exit(1)
def callback(ch, method, properties, body):
print(f" [x] Получена задача: {body.decode()}")
time.sleep(body.count(b'.')) # Имитация сложной работы
print(" [x] Задача выполнена")
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_consume(queue='task_queue', on_message_callback=callback, auto_ack=False)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
# Close the connection when the consumer is done
connection.close()
Important Considerations:
- Security: Avoid using
PLAIN authentication in production. Use SASL_PASSWORD or TLS/SSL instead.
- RabbitMQ Version: The authentication mechanisms and configuration options may vary slightly depending on your RabbitMQ version.
- Firewall: Ensure that your firewall isn't blocking connections to the RabbitMQ server on the appropriate port (usually 5672 for unencrypted connections or 5671 for TLS/SSL).
How to proceed:
- Check RabbitMQ Logs: This is the first and most important step. The logs will tell you exactly why the authentication is failing.
- Configure Authentication: Based on the log information, configure RabbitMQ to use a suitable authentication method.
- Update Client Code: If you're using
PLAIN or SASL_PASSWORD, update your consumer.py script to provide the correct credentials.
- Test: After making changes, restart the RabbitMQ server and try running your
consumer.py script again.