Connect ECONNREFUSED 127.0.0.1:27017 in Mongodb Compass

I restarted my computer to try to update it, and before restarting, I was also working on another project. Now, my project with a node.js backend is giving me this error:

reason: TopologyDescription {
    type: 'Single',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map(1) {
      'localhost:27017' => ServerDescription {
        address: 'localhost:27017',
        error: Error: connect ECONNREFUSED 127.0.0.1:27017
            at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1132:16) {
          name: 'MongoNetworkError'
        },
        roundTripTime: -1,
        lastUpdateTime: 58154302,
        lastWriteDate: null,
        opTime: null,
        type: 'Unknown',
        topologyVersion: undefined,
        minWireVersion: 0,
        maxWireVersion: 0,
        hosts: [],
        passives: [],
        arbiters: [],
        tags: []
      }
    },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  }

and my mongodb compass is giving me: connect ECONNREFUSED 127.0.0.1:27017 . My operating system is macOS Big Sur.

In a post on stackoverflow, someone suggested restarting the mongod process and connecting again. I tried following those instructionsmac-mongodb , but when I go to 'From a new terminal, issue the following: mongosh ’ my terminal gave me command not found: mongosh . I also tried testing it with PATH="/usr/local/opt/mongodb-community@4.4/bin" mongo MongoDB shell version v4.4.13 and I still got Error: couldn't connect to server 127.0.0.1:27017, with connection refused.

Does anyone know how to fix this? I would really appreciate any help or advice. Thank you

I am not an expert, just another user, but I believe this set of steps could help you

Upon start it seems to me that Mongodb server generates a lock that blocks another process to take that port (this may be fully false).

Test this ls /tmp/mongodb-27017*, because it is a temporary file you can very well remove it.

Then restart the server, and see if it connects.

1 Like

Means that NO mongod instance is running at the given host 127.0.0.1 and port 27017. Yes the solution is to start mongod.

Never do that unless you know absolutely what you are doing.

Executing the command mongosh or mongo is not how you start mongod. Since you seem uncertain about the difference between the mongod server and the mongosh/mongo client, you might one to take the course M103: Basic Cluster Administration | MongoDB University.

3 Likes

Why not? Likely the process shut down unexpectedly on the update and if the process is down I see no trouble doing it…also bc OP is testing in localhost…:ok_man:

You seem to know what you are doing.

The original poster do not seem to know, so he asked a question.

One day he may delete the file to solve another issue. Restart the server and corrupts its data.

That is why I wrote,

If you know what you are doing then yes go ahead and delete it.

1 Like

Was anyone able to give a straight answer on this? I have the exact same problem

The straight answer is

1 Like

no it’s not, this is no answer

try to see the exact problem else we’ll go around and around. I even went to compass and reconnect and it’s refusing

if I start mongod in anyway I have tried i got always denials like this one:

==> Successfully started mongodb-community (label: homebrew.mxcl.mongodb-commu
Mac-do-Rateiro:Mongodb pedro$ mongo
MongoDB shell version v5.0.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn’t connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:372:17
@(connect):2:6
exception: connect failed
exiting with code 1
Mac-do-Rateiro:Mongodb pedro$ sudo mongo
Password:
MongoDB shell version v5.0.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn’t connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:372:17
@(connect):2:6
exception: connect failed
exiting with code 1
Mac-do-Rateiro:Mongodb pedro$ mongod
{“t”:{"$date":“2022-06-12T14:30:39.923+01:00”},“s”:“I”, “c”:“CONTROL”, “id”:23285, “ctx”:"-",“msg”:“Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols ‘none’”}
{“t”:{"$date":“2022-06-12T14:30:39.923+01:00”},“s”:“I”, “c”:“NETWORK”, “id”:4915701, “ctx”:"-",“msg”:“Initialized wire specification”,“attr”:{“spec”:{“incomingExternalClient”:{“minWireVersion”:0,“maxWireVersion”:13},“incomingInternalClient”:{“minWireVersion”:0,“maxWireVersion”:13},“outgoing”:{“minWireVersion”:0,“maxWireVersion”:13},“isInternalClient”:true}}}
{“t”:{"$date":“2022-06-12T14:30:39.924+01:00”},“s”:“W”, “c”:“ASIO”, “id”:22601, “ctx”:“main”,“msg”:“No TransportLayer configured during NetworkInterface startup”}
{“t”:{"$date":“2022-06-12T14:30:39.924+01:00”},“s”:“I”, “c”:“NETWORK”, “id”:4648602, “ctx”:“main”,“msg”:“Implicit TCP FastOpen in use.”}
{“t”:{"$date":“2022-06-12T14:30:39.926+01:00”},“s”:“W”, “c”:“ASIO”, “id”:22601, “ctx”:“main”,“msg”:“No TransportLayer configured during NetworkInterface startup”}
{“t”:{"$date":“2022-06-12T14:30:39.926+01:00”},“s”:“I”, “c”:“REPL”, “id”:5123008, “ctx”:“main”,“msg”:“Successfully registered PrimaryOnlyService”,“attr”:{“service”:“TenantMigrationDonorService”,“ns”:“config.tenantMigrationDonors”}}
{“t”:{"$date":“2022-06-12T14:30:39.926+01:00”},“s”:“I”, “c”:“REPL”, “id”:5123008, “ctx”:“main”,“msg”:“Successfully registered PrimaryOnlyService”,“attr”:{“service”:“TenantMigrationRecipientService”,“ns”:“config.tenantMigrationRecipients”}}
{“t”:{"$date":“2022-06-12T14:30:39.926+01:00”},“s”:“I”, “c”:“CONTROL”, “id”:5945603, “ctx”:“main”,“msg”:“Multi threading initialized”}
{“t”:{"$date":“2022-06-12T14:30:39.926+01:00”},“s”:“I”, “c”:“CONTROL”, “id”:4615611, “ctx”:“initandlisten”,“msg”:“MongoDB starting”,“attr”:{“pid”:4972,“port”:27017,“dbPath”:"/data/db",“architecture”:“64-bit”,“host”:“Mac-do-Rateiro”}}
{“t”:{"$date":“2022-06-12T14:30:39.926+01:00”},“s”:“I”, “c”:“CONTROL”, “id”:23403, “ctx”:“initandlisten”,“msg”:“Build Info”,“attr”:{“buildInfo”:{“version”:“5.0.6”,“gitVersion”:“212a8dbb47f07427dae194a9c75baec1d81d9259”,“modules”:[],“allocator”:“system”,“environment”:{“distarch”:“x86_64”,“target_arch”:“x86_64”}}}}
{“t”:{"$date":“2022-06-12T14:30:39.926+01:00”},“s”:“I”, “c”:“CONTROL”, “id”:51765, “ctx”:“initandlisten”,“msg”:“Operating System”,“attr”:{“os”:{“name”:“Mac OS X”,“version”:“20.6.0”}}}
{“t”:{"$date":“2022-06-12T14:30:39.926+01:00”},“s”:“I”, “c”:“CONTROL”, “id”:21951, “ctx”:“initandlisten”,“msg”:“Options set by command line”,“attr”:{“options”:{}}}
{“t”:{"$date":“2022-06-12T14:30:39.937+01:00”},“s”:“E”, “c”:“NETWORK”, “id”:23024, “ctx”:“initandlisten”,“msg”:“Failed to unlink socket file”,“attr”:{“path”:"/tmp/mongodb-27017.sock",“error”:“Permission denied”}}
{“t”:{"$date":“2022-06-12T14:30:39.937+01:00”},“s”:“F”, “c”:"-", “id”:23091, “ctx”:“initandlisten”,“msg”:“Fatal assertion”,“attr”:{“msgid”:40486,“file”:“src/mongo/transport/transport_layer_asio.cpp”,“line”:989}}
{“t”:{"$date":“2022-06-12T14:30:39.937+01:00”},“s”:“F”, “c”:"-", “id”:23092, “ctx”:“initandlisten”,“msg”:"\n\n***aborting after fassert() failure\n\n"}

or

Mac-do-Rateiro:Mongodb pedro$ mongosh

Current Mongosh Log ID: 62a5eaac9f0a57c1d0ed9dd0

Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.4.2

MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017

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.

Well at least this is more info

Steve, the first time i did it was mongod and it always get the same answer. This is a little bit more complicated for sure.

Now, your point on mongod started by root can be a logical cause but I am not sure to manually delete the “offending file” as you said, which leads to my question : what do you mean on the shutdown cleanup and how delete a “what is” offending file

The offending file is the file mentionned in the error message you get when starting mongod.

When mongod terminates gracefully, the socket file above and some other lock files that are used to communicate or ensure the integrity of data directories are deleted so that the next startup can proceed. When mongod terminate abruptly, it cannot delete such files and locks.

ok, good, so, how should I manually delete this file without making any mistake?

Check owner/permissions on this file

ls -lrt /tmp/mongodb-27017.sock

Shutdown all mongods if any running and then remove this file

If it is owned by root you have to use sudo rm
After removing the file start mongod with sysctl as steevejSteeve Juneau suggested

1 Like

it worked after doing this:
sudo rm -rf /tmp/mongodb-27017.sock
(delete the file)

brew services restart mongodb-community@5.0
(restart services)

brew services list
(to check services and went “green” started)

mongo
(to start mongo, as well as connect with the compass, everything went ok after this)

thank you all for the patience, hope it won’t happen more often

5 Likes

well for me, all i had to do is search for ‘services’ on my windows. scrolled down to MongoDB services and started it and went back to reconnect it on compass. it worked

6 Likes

solution is as steevej said, to start monogDB first, and you can do so sudo systemctl start mongod if you’re using ubuntu/linux, for windows give it a google search, or better yet look into mongoDB docs https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/

i just faced this and was able to fix this by starting mongoDB, happy learning :slight_smile:

I was getting the same error and I followed your solution and it worked for me as well.
Thanks :slight_smile:

open the setup there is an option to repair in it
that worked for me. i have mongodb v 6

1 Like

Hello Pedro, I’m having the same issue but I dont see such file at all in the tmp folder on our server (Ubuntu 20.04)

there might be some different since I used a MAcOs terminal, but you have to check which file while you list it in the terminal. might not be the same tmp as mine, can be any other that you need to delete

the main problem is that you have a tmp file that is giving some sort of error and your folder can’t just delete it.

Can you put here your log? with the error so we can check which is the file that is giving the error?