iamshaunjp / complete-mongodb

All the course file for the Complete MongoDB tutorial series on the Net Ninja YouTube channel.
97 stars 45 forks source link

error MongoServerSelectionError: connect ECONNREFUSED ::1:27017 #5

Open JasonRhoads opened 1 year ago

JasonRhoads commented 1 year ago

Is anyone else getting this error? I have not been able to find any solution.

MongoServerSelectionError: connect ECONNREFUSED ::1:27017 at Timeout._onTimeout (C:\Users\Jason\Desktop\MondoDB_tutorial\node_modules\mongodb\lib\sdam\topology.js:285:38) at listOnTimeout (node:internal/timers:564:17) at process.processTimers (node:internal/timers:507:7) { reason: TopologyDescription { type: 'Unknown', servers: Map(1) { 'localhost: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) {} }

The server is running and if I try to connect directly to the database it gives me this: It looks like you are trying to access MongoDB over HTTP on the native driver port.

if I load up mongosh it does connect. PS C:\Program Files\MongoDB\Server\6.0\bin> mongosh Current Mongosh Log ID: 63d41526edf7a86f071180ca Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.2 Using MongoDB: 6.0.4 Using Mongosh: 1.6.2

JasonRhoads commented 1 year ago

Found the answer in youtube comment section.

use for your connection string in db.js MongoClient.connect('mongodb://127.0.0.1:27017/bookstore')

Syeda-k commented 1 year ago

I faced the same error .By using the following code the error solved. const MongoClient = require('mongodb').MongoClient;

const uri = 'mongodb://127.0.0.1:27017/directConnection=true&serverSelectionTimeoutMS=20000' const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true }); client.connect(err => { const collection = client.db("BookStore").collection("Author"); // perform actions on the collection object client.close(); });

sudosf commented 1 year ago

Might be late but this helped me:

instead of writing URL as: url = "mongodb://127.0.0.1:27017/books"

try: url = "mongodb://0.0.0.0:27017/books"

From this thread: https://stackoverflow.com/questions/46523321/mongoerror-connect-econnrefused-127-0-0-127017