ECONNREFUSED mongodb connection_error ai_generated true

MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017

ID: mongodb/connection-refused

Also available as: JSON · Markdown
90%Fix Rate
92%Confidence
3Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7 active

Root Cause

Cannot connect to MongoDB. Service not running, wrong host/port, or firewall blocking.

generic

Workarounds

  1. 95% success Start the MongoDB service
    sudo systemctl start mongod && sudo systemctl enable mongod

    Sources: https://www.mongodb.com/docs/manual/

  2. 90% success Check if MongoDB is listening on the expected port
    ss -tlnp | grep 27017; sudo journalctl -u mongod --tail=20
  3. 85% success Check bindIp in mongod.conf for remote connections
    bindIp: 0.0.0.0  # in /etc/mongod.conf; default is 127.0.0.1 only

Dead Ends

Common approaches that don't work:

  1. Reinstall MongoDB to fix connection issues 82% fail

    Connection refused means the service is not listening, not that the installation is broken

  2. Use localhost instead of 127.0.0.1 or vice versa 72% fail

    Both resolve to the same address. The issue is the service not running, not the address format.