0
0
RabbitmqDebug / FixBeginner · 3 min read

How to Fix Connection Refused Error in RabbitMQ

A connection refused error in RabbitMQ usually means the server is not reachable on the expected port. To fix it, ensure the RabbitMQ service is running, the port (default 5672) is open, and your client uses the correct host and port settings.
🔍

Why This Happens

This error happens when your application tries to connect to RabbitMQ but cannot reach the server. Common reasons include the RabbitMQ service not running, the port being blocked by a firewall, or incorrect connection settings like wrong host or port.

python
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672))
channel = connection.channel()
Output
pika.exceptions.AMQPConnectionError: Connection refused
🔧

The Fix

First, check if RabbitMQ is running with sudo systemctl status rabbitmq-server. If not running, start it with sudo systemctl start rabbitmq-server. Next, verify the port 5672 is open using sudo netstat -tlnp | grep 5672. Also, confirm your client uses the correct host and port. If connecting remotely, ensure firewall allows port 5672.

bash
sudo systemctl start rabbitmq-server
sudo netstat -tlnp | grep 5672
Output
tcp 0 0 0.0.0.0:5672 0.0.0.0:* LISTEN 1234/beam.smp
🛡️

Prevention

To avoid this error, always verify RabbitMQ service status before connecting. Use monitoring tools or scripts to alert if RabbitMQ stops. Keep firewall rules updated to allow RabbitMQ ports. Use configuration files or environment variables to manage connection settings consistently.

⚠️

Related Errors

Other similar errors include TimeoutError when RabbitMQ is slow to respond, or Authentication failure if credentials are wrong. Fix these by checking network latency and verifying username/password in RabbitMQ.

Key Takeaways

Ensure RabbitMQ service is running before connecting.
Verify port 5672 is open and accessible.
Use correct host and port in client connection settings.
Keep firewall rules updated to allow RabbitMQ traffic.
Monitor RabbitMQ service to catch downtime early.