简体   繁体   English

NodeJS 无法连接到服务器 ECONNREFUSED 上的 MongoDB

[英]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.当我尝试使用node Connection.js运行脚本时,脚本运行,但我没有在控制台中收到successfully connected to the database的响应,并且我没有收到任何错误。 It's just looks like the script runs but nothing happens.看起来脚本运行但没有任何反应。

On VPS I'm using Ubuntu 18.04.在 VPS 上,我使用的是 Ubuntu 18.04。 I allowed the IP address and port from my local machine.我允许来自本地计算机的 IP 地址和端口。

Any ideas what's wrong with this one, I was trying to find anything related this topic but unsuccessful任何想法有什么问题,我试图找到与这个主题相关的任何东西,但没有成功

Connection.js连接.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编辑1

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我编辑了代码,我 100% 确定我允许在我的 ubuntu 服务器port 22上,但我仍然无法使用 Node 连接到我的 VPS

I receive this error - (node:10115) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED我收到此错误 - (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.用户名和密码正确,DB名也正确,ip地址和端口也正确。 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.我检查了大量资源来解决这个问题,然后将我的 NodeJS 脚本从本地机器连接到 VPS 服务器。

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?我有点确定您的 VPS 必须将端口 22 绑定到 openssh-server ...您确定您已经能够正确启动 mongod 并且它正在侦听端口 22?

BTW, all services under 1024 are kind of standardized you should always try to use ports above 1024.顺便说一句,1024 以下的所有服务都是标准化的,您应该始终尝试使用 1024 以上的端口。

Could you share with us the configuration of your mongod?你能和我们分享一下你的mongod的配置吗?

There seems to be some issues with the configuration on server.服务器上的配置似乎存在一些问题。 Please go through these checklist for issues.请通过这些检查清单 go 解决问题。

  • 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. MongoDb 默认运行在端口号 27017 上,端口号 22 为 SSH 保留,请确保您连接到正确的端口号。
  • Please make sure that you MongoDB server instance is listening to all network interfaces, IP addresses if you are connecting to server via network.如果您通过网络连接到服务器,请确保您的 MongoDB 服务器实例正在侦听所有网络接口,IP 地址。 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.检查您的 mongod.conf(或 mongodb.conf)文件( bind_ip = 127.0.0.1仅用于本地, bind_ip = 0.0.0.0用于侦听来自所有网络接口的流量。)了解更多详细信息。
  • 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.如果您通过 SSH 连接,则需要 ubuntu 操作系统上的用户名和密码,并直接与 mongodb 连接需要在 mongo 服务器实例上定义的用户名密码。
  • Please check that authorization settings is enabled on your mongo instance mongod.conf(or mongodb.conf) file.请检查您的 mongo 实例 mongod.conf(或 mongodb.conf)文件是否启用了授权设置。
  • Please make sure that the user you created have access on the the database ( CryptoCurrencies the one you are connecting to)请确保您创建的用户有权访问数据库(您要连接的CryptoCurrencies

Check this link on how to configure auth on MongoDB server instance https://medium.com/mongoaudit/how-to-enable-authentication-on-mongodb-b9e8a924efac查看此链接,了解如何在 MongoDB 服务器实例https://medium.com/mongoaudit/how-to-enable-authentication-on-mongodb-b9e8a924efac上配置身份验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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