简体   繁体   中英

NodeJS cannot connect to MongoDB on server ECONNREFUSED

I have a basic script which is test for connecting to database.

When I'm trying to run the script with node Connection.js the script's run, but I don't receive the response in console that successfully connected to the database and as well with that I don't receive any error. It's just looks like the script runs but nothing happens.

On VPS I'm using Ubuntu 18.04. I allowed the IP address and port from my local machine.

Any ideas what's wrong with this one, I was trying to find anything related this topic but unsuccessful

Connection.js

    const mongoose = require('mongoose');
var express = require('express');
const router = express.Router();

// this is our MongoDB database
const dbRoute =
    'mongodb://username:password@ip.adress:22/CryptoCurrencies';

mongoose.Promise = global.Promise;

// connects our back end code with the database
mongoose.connect(dbRoute, {
    useNewUrlParser: true,
    useUnifiedTopology: true
});

let db = mongoose.connection;

db.once('open', () => console.log('connected to the database'));

EDIT1

I edited the code and I 100% sure that I allow on my ubuntu server port 22 but I still can't connect to my VPS with Node

I receive this error - (node:10115) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED

Any ideas on how I can fix this one? Username and password are correct, DB name is also correct, ip address and port as well. Currently I totally don't understand what is wrong. I check tons of resources to solve that and just connect my NodeJS script from local machine to VPS server.

I'm kind of sure your VPS must have the port 22 bound to openssh-server... Are you sure you've been able to start mongod correctly and it's listening on port 22?

BTW, all services under 1024 are kind of standardized you should always try to use ports above 1024.

Could you share with us the configuration of your mongod?

There seems to be some issues with the configuration on server. Please go through these checklist for issues.

  • MongoDb by default run on port no 27017, and port number 22 is reserved for SSH, please make sure you are connecting to right port number.
  • Please make sure that you MongoDB server instance is listening to all network interfaces, IP addresses if you are connecting to server via network. Check your mongod.conf (or mongodb.conf) file ( bind_ip = 127.0.0.1 for local only, bind_ip = 0.0.0.0 to listen traffic from all network interfaces.) for more details.
  • If you are connecting via SSH it require username and password of user on ubuntu OS & connecting directly with mongodb require the username name password for user defined on mongo server instance.
  • Please check that authorization settings is enabled on your mongo instance mongod.conf(or mongodb.conf) file.
  • Please make sure that the user you created have access on the the database ( CryptoCurrencies the one you are connecting to)

Check this link on how to configure auth on MongoDB server instance https://medium.com/mongoaudit/how-to-enable-authentication-on-mongodb-b9e8a924efac

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM