Connect to MongoDB with node.js

Hi,

i have a problem to make a connection to mongodb.
I have installed MongoDB on a external Ubuntu Server with node.js and connected to the server over VSCode SSH Connection on the Terminal.
I want to test if i can connect to mongodb, but it gives an error in the shell.

My Server Code:

const express = require('express');
const fs = require("fs");
const https = require("https");
const app = express();

var mongoose = require('mongoose');

const uri = "mongodb://root:<MyPassword>@<MyServerAdres>/tutorial:27017"

async function connect() {
  try {
    await mongoose.connect(uri);
    console.log("connected to MongoDB");
  } catch(error) {
    console.error(error);
  }
}

connect();

Im getting this error in the Terminal:

MongooseServerSelectionError: connect ECONNREFUSED 192.XXX.XXX.X:27017
    at _handleConnectionErrors (/root/PWA/api/node_modules/mongoose/lib/connection.js:792:11)
    at NativeConnection.openUri (/root/PWA/api/node_modules/mongoose/lib/connection.js:767:11)
    at async connect (/root/PWA/api/test.js:15:5) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) { '<MyServerAdres>:27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: null,
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined
}

My MongoDB is active and running. and in the mongodb.log i can see the entry “[initandlisten] waiting for connections on port 27017”

Maybe, my URI is wrong? I tried to connect with the Mongodb Compas to test the connection, but it gives me also an error.

ECONNREFUSED means no server is listening at the given address and port.

Assuming you are using the same installation as your previous post

you are doing the same error as before. You are using the default port 27017 rather than the port used by the server, that is the one specified in the configuration file. From your previous post this port is 10000.

But since you

the cause might be different. In your other post, you also mentioned using 2 machines. May be the mongo log message you see is from a machine different from the machine with the host name tutorial.

Basically, ECONNREFUSED means wrong address, wrong port or no listener. You seem to have a listener on the correct port, so the address must be wrong.

1 Like

I have changed the port numver after we have solved the problem to the standard Port.

Do i need to write the collection in the URI, which i want to connected:
like …/tutorial:27017???

Another question.
Which username and password should i use in the URI String´?
The Access Data from my external Server, where i have installed MongoDB and Node.js or has MongoDB own Access Data, which should be defined in any config from mongodb?

That part is mongoose specific. From what I understand, since you are using schema to crud the data you do not have to do it.

The URI is the one used to access MongoDB so it has to be the username and password defined in MongoDB.

1 Like