MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017

Hi, could you help me?
I keep getting this error when I launch node app.js.
Mongo shell connects, service MongoDB running (win11), me desperate.

MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
    at Timeout._onTimeout (/.../node_modules/mongodb/lib/sdam/topology.js:293:38)
    at listOnTimeout (node:internal/timers:557:17)
    at processTimers (node:internal/timers:500:7) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) { '127.0.0.1:27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: null,
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined,
  [Symbol(errorLabels)]: Set(0) {}
}

This is the code, tested fine on other machines:

const { MongoClient } = require("mongodb");

const uri = "mongodb://127.0.0.1:27017/"
const client = new MongoClient(uri);
async function run() {
  try {
    const database = client.db('sample_mflix');
    const movies = database.collection('movies');
    // Query for a movie that has the title 'Back to the Future'
    const query = { title: 'Back to the Future' };
    const movie = await movies.findOne(query);
    console.log(movie);
  } finally {
    // Ensures that the client will close when you finish/error
    await client.close();
  }
}
run().catch(console.dir);

My config file looks like this, tried all possible combinations of paths and optional

storage:
  dbPath: C:\data\db
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path:  C:\Program Files\MongoDB\Server\6.0\log\mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1
  ipv6: true
1 Like

Is your service up and running,?
Show snapshot of your Windows service and shell connection you connected successfully

Issue could be bindIp parameter
Check this link for Ipv6

This are the settings, I turned back on the bindIpAll option

net:
  port: 27017
  bindIp: 127.0.0.1
  ipv6: true
  bindIpAll: true

I checked firewall and created rules for mongod/mongos, set the network to private, flushed the DNS.
I have no idea what to do

You cannot use both bindIp & bindIp_all at same time
Use only one of them in your cfg file
Try 0.0.0.0 for bind_ip (not adviced on prod) and see if it works

Also check this for exact format for Ipv6 with localhost

Thank you Ramachandra, but still nothing, I had the configuration suggested in the post you linked, now I have it like this, still nothing.

net:
  port: 27017
  ipv6: true
  bindIpAll: true

I switched to node 19 (from 16), same with both versions. MongoDB v 6.0.2

Is your IP whitelisted?
Did you try without Ipv6(use ipv4)
or
Don’t use bindipall and try other format given for ipv6 with localhost,::
Is your localhost mapped to 127.0.0.1?
Is the machine where your node.app runs same as db server?

My IP is whitelisted
My localhost goes like this in the hosts file

image

I tried all the options and combinations, still nothing.
Mongod runs as window service, but I also tried stopping the service and launching a mongod instance on the ubuntu terminal (what I use normally) and I have the same results. Mongosh connects but node app doesn’t.

If mongosh connect then your application should connect if you run your application on the same machine as mongosh. The following might indicates that you are using 2 machines or a VM:

Is the ubuntu terminal running on the same Windows where mongod is running as a service? When you connect with mongosh, do you connect from the same Windows machine where mongod is running? Is the app running on the same machine as the mongosh that can connect?

You shared a couple of version of your configuration file. Please share the latest version. Have you restarted mongod after changing the configuration file?

Yeah same machine. I run mongosh on WSL terminal and it connects (with windows service running AND if I stop that service and run an instance of mongod on WSL).

The latest configuration file. Mind that I’ve tried many combinations, this is from a MacOS where I tested and it worked fine.

image

I am not clear
How can Macos have Windows directory paths?
I have suggested in above posts to use 127.0.0.1,::1
Let’s wait for Steeves reply

The directory path for mac is different of course, but that’s not the point. Can it be something about windows security or similar?

Share the content of the log.

I would try without ipv6 true and without ::1 for bindIp, just to simplify things.

{"t":{"$date":"2022-10-27T15:44:46.274+02: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-10-27T15:44:47.112+02:00"},"s":"I",  "c":"NETWORK",  "id":4915701, "ctx":"thread1","msg":"Initialized wire specification","attr":{"spec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":17},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":17},"outgoing":{"minWireVersion":6,"maxWireVersion":17},"isInternalClient":true}}}
{"t":{"$date":"2022-10-27T15:44:47.113+02:00"},"s":"I",  "c":"NETWORK",  "id":4648602, "ctx":"thread1","msg":"Implicit TCP FastOpen in use."}
{"t":{"$date":"2022-10-27T15:44:47.115+02:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"thread1","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationDonorService","namespace":"config.tenantMigrationDonors"}}
{"t":{"$date":"2022-10-27T15:44:47.122+02:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"thread1","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationRecipientService","namespace":"config.tenantMigrationRecipients"}}
{"t":{"$date":"2022-10-27T15:44:47.122+02:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"thread1","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"ShardSplitDonorService","namespace":"config.tenantSplitDonors"}}
{"t":{"$date":"2022-10-27T15:44:47.122+02:00"},"s":"I",  "c":"CONTROL",  "id":5945603, "ctx":"thread1","msg":"Multi threading initialized"}
{"t":{"$date":"2022-10-27T15:44:47.122+02:00"},"s":"I",  "c":"CONTROL",  "id":23316,   "ctx":"thread1","msg":"Trying to start Windows service","attr":{"serviceName":"MongoDB"}}
{"t":{"$date":"2022-10-27T15:44:47.123+02:00"},"s":"I",  "c":"CONTROL",  "id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":15020,"port":27017,"dbPath":"C:/data/db","architecture":"64-bit","host":"LAPTOP-RJ5HAO5L"}}
{"t":{"$date":"2022-10-27T15:44:47.123+02:00"},"s":"I",  "c":"CONTROL",  "id":23398,   "ctx":"initandlisten","msg":"Target operating system minimum version","attr":{"targetMinOS":"Windows 7/Windows Server 2008 R2"}}
{"t":{"$date":"2022-10-27T15:44:47.123+02:00"},"s":"I",  "c":"CONTROL",  "id":23403,   "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"6.0.2","gitVersion":"94fb7dfc8b974f1f5343e7ea394d0d9deedba50e","modules":[],"allocator":"tcmalloc","environment":{"distmod":"windows","distarch":"x86_64","target_arch":"x86_64"}}}}
{"t":{"$date":"2022-10-27T15:44:47.123+02:00"},"s":"I",  "c":"CONTROL",  "id":51765,   "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Microsoft Windows 10","version":"10.0 (build 22621)"}}}
{"t":{"$date":"2022-10-27T15:44:47.123+02:00"},"s":"I",  "c":"CONTROL",  "id":21951,   "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{"config":"C:\\Program Files\\MongoDB\\Server\\6.0\\bin\\mongod.cfg","net":{"bindIp":"127.0.0.1"},"service":true,"storage":{"dbPath":"C:\\data\\db"},"systemLog":{"destination":"file","logAppend":true,"path":"C:\\Program Files\\MongoDB\\Server\\6.0\\log\\mongod.log"}}}}
{"t":{"$date":"2022-10-27T15:44:47.124+02:00"},"s":"I",  "c":"STORAGE",  "id":22270,   "ctx":"initandlisten","msg":"Storage engine to use detected by data files","attr":{"dbpath":"C:/data/db","storageEngine":"wiredTiger"}}
{"t":{"$date":"2022-10-27T15:44:47.124+02:00"},"s":"I",  "c":"STORAGE",  "id":22315,   "ctx":"initandlisten","msg":"Opening WiredTiger","attr":{"config":"create,cache_size=7294M,session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,remove=true,path=journal,compressor=snappy),builtin_extension_config=(zstd=(compression_level=6)),file_manager=(close_idle_time=600,close_scan_interval=10,close_handle_minimum=2000),statistics_log=(wait=0),json_output=(error,message),verbose=[recovery_progress:1,checkpoint_progress:1,compact_progress:1,backup:0,checkpoint:0,compact:0,evict:0,history_store:0,recovery:0,rts:0,salvage:0,tiered:0,timestamp:0,transaction:0,verify:0,log:0],"}}
{"t":{"$date":"2022-10-27T15:44:47.369+02:00"},"s":"I",  "c":"STORAGE",  "id":4795906, "ctx":"initandlisten","msg":"WiredTiger opened","attr":{"durationMillis":245}}
{"t":{"$date":"2022-10-27T15:44:47.369+02:00"},"s":"I",  "c":"RECOVERY", "id":23987,   "ctx":"initandlisten","msg":"WiredTiger recoveryTimestamp","attr":{"recoveryTimestamp":{"$timestamp":{"t":0,"i":0}}}}
{"t":{"$date":"2022-10-27T15:44:47.372+02:00"},"s":"W",  "c":"CONTROL",  "id":22120,   "ctx":"initandlisten","msg":"Access control is not enabled for the database. Read and write access to data and configuration is unrestricted","tags":["startupWarnings"]}
{"t":{"$date":"2022-10-27T15:44:47.374+02:00"},"s":"I",  "c":"NETWORK",  "id":4915702, "ctx":"initandlisten","msg":"Updated wire specification","attr":{"oldSpec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":17},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":17},"outgoing":{"minWireVersion":6,"maxWireVersion":17},"isInternalClient":true},"newSpec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":17},"incomingInternalClient":{"minWireVersion":17,"maxWireVersion":17},"outgoing":{"minWireVersion":17,"maxWireVersion":17},"isInternalClient":true}}}
{"t":{"$date":"2022-10-27T15:44:47.375+02:00"},"s":"I",  "c":"REPL",     "id":5853300, "ctx":"initandlisten","msg":"current featureCompatibilityVersion value","attr":{"featureCompatibilityVersion":"6.0","context":"startup"}}
{"t":{"$date":"2022-10-27T15:44:47.375+02:00"},"s":"I",  "c":"STORAGE",  "id":5071100, "ctx":"initandlisten","msg":"Clearing temp directory"}
{"t":{"$date":"2022-10-27T15:44:47.375+02:00"},"s":"I",  "c":"CONTROL",  "id":20536,   "ctx":"initandlisten","msg":"Flow Control is enabled on this deployment"}
{"t":{"$date":"2022-10-27T15:44:47.639+02:00"},"s":"I",  "c":"FTDC",     "id":20625,   "ctx":"initandlisten","msg":"Initializing full-time diagnostic data capture","attr":{"dataDirectory":"C:/data/db/diagnostic.data"}}
{"t":{"$date":"2022-10-27T15:44:47.642+02:00"},"s":"I",  "c":"REPL",     "id":6015317, "ctx":"initandlisten","msg":"Setting new configuration state","attr":{"newState":"ConfigReplicationDisabled","oldState":"ConfigPreStart"}}
{"t":{"$date":"2022-10-27T15:44:47.642+02:00"},"s":"I",  "c":"STORAGE",  "id":22262,   "ctx":"initandlisten","msg":"Timestamp monitor starting"}
{"t":{"$date":"2022-10-27T15:44:47.644+02:00"},"s":"I",  "c":"NETWORK",  "id":23015,   "ctx":"listener","msg":"Listening on","attr":{"address":"127.0.0.1"}}
{"t":{"$date":"2022-10-27T15:44:47.644+02:00"},"s":"I",  "c":"NETWORK",  "id":23016,   "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}
{"t":{"$date":"2022-10-27T15:44:47.644+02:00"},"s":"I",  "c":"CONTROL",  "id":20555,   "ctx":"initandlisten","msg":"Service running"}

Any success?
It says waiting for connections but SSL:off
Did you try 0.0.0.0 inplace of localhost in your connect string.It is suggested as one of the fix in a forum thread

Yes I tried it all. I think I’ll give up, thanks for your help anyways

Please share a screenshot that shows exactly how you start your application.

If mongosh can connect, your application should connect.

Are you using docker or some other container to start the application?

I do not know exactly which commands are available inside WSL terminal, but the output of the following (if available)

ss -tlnp
ps -aef | grep [m]ongo

I’m not using a container or docker I think

image

this is what I get running those commands

what is the second command trying to do?