Connect ECONNREFUSED 127.0.0.1:27017 in Mongodb Compass

Well, I think it is and what you posted just demontrates it.

  1. You try to start mongo twice before trying to start mongod. So you get the error because mongod is not running.

  2. You then try to start mongod, but it fails because some permission denied error.

  3. You then try to connect with mongosh and still get the error. Of course you get the error mongod was not started. see #2 above.

The process mongod must be running before you try to connect.

As to why mongod does not start, the it looks like mongod was started by root (or a user different from your current user) and wad not terminated correctly since the shutdown cleanup usually done has not been completed.

Try to manually delete the offending file and try to start mongod again. You should use systemctl or something lioe that to start mongod rather than manually as it behaves better when the computer shuts down.

2 Likes